Android应用程序启动时崩溃-setOnClickListener
我对Android开发人员真的很陌生,但试图构建一个简单的计算器,问题是我无法加载按钮。我将其简化为一个按钮来更改文本视图,但它在启动时崩溃 如果我注释掉button1.setOnClickListener(这个);它将在手机(Nexus 4)上运行,但显然这意味着该按钮将无法工作 我在类中实现了onClickListener,因为我相信每个按钮都有一个新的监听器会更整洁、更容易——最终会有很多按钮 谁能看出我做错了什么吗? 谢谢 更新:这是完整的片段_main.xml。虽然我现在只引用一个按钮和一个文本视图Android应用程序启动时崩溃-setOnClickListener,android,Android,我对Android开发人员真的很陌生,但试图构建一个简单的计算器,问题是我无法加载按钮。我将其简化为一个按钮来更改文本视图,但它在启动时崩溃 如果我注释掉button1.setOnClickListener(这个);它将在手机(Nexus 4)上运行,但显然这意味着该按钮将无法工作 我在类中实现了onClickListener,因为我相信每个按钮都有一个新的监听器会更整洁、更容易——最终会有很多按钮 谁能看出我做错了什么吗? 谢谢 更新:这是完整的片段_main.xml。虽然我现在只引用一个按钮
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="com.example.newcalc.MainActivity$PlaceholderFragment" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/workings"
android:textSize="20sp" />
<TextView
android:id="@+id/display"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingBottom="20dp"
android:text="@string/display"
android:textSize="60sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_ClearAll" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_ClearCurrent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_MinusModifier" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_Memory" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_9" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_Divide" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_6" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_Multiply" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_Minus" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_Point" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_Equals" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_Plus" />
</LinearLayout>
</LinearLayout>
再次感谢像这样尝试
setOnClickListener
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textview1.setText("1");
}
});
试试这个
编辑
将fragment_main.xml中的整个代码复制到activity_main.xml。我认为这应该可以将
fragment_main.xml中的整个代码复制到activity_main.xml
中。我认为这应该可以我想问题是您试图从activity_main.xml访问,但它们位于fragment_main.xml中
setContentView(R.layout.activity_main);
button1 = (Button) findViewById (R.id.button_1);
textview1 = (TextView) findViewById (R.id.display);
请粘贴错误日志,并粘贴完整的布局。这是布局主活动还是片段主?发布logcat
输出。我测试了您的代码并使其成功运行。我唯一没有添加的是占位符片段。恐怕我还没有学会如何正确使用Logcat。大量的文本出现了。占位符片段代码在创建项目时未被触及,在删除它时仍会在我身上崩溃。如果您阅读他的代码,他已经实现了此方法。作者还写道:“我在课堂上实现了onClickListener,因为我相信每个按钮都有一个新的监听器会更整洁、更容易——最终会有很多按钮。”这显然不是问题所在将fragment\u main.xml
中的整个代码复制到activity\u main.xml
中。我认为这应该可以将布局复制到代码中吗?同样,我是android开发新手,但是fragment\u main不是要保持布局的独立吗?我说的是将整个fragment\u main.xml
文件复制到activity\u main.xml
中,而不是复制到MainActivity.java
中。谢谢@Lal,我误读了你的评论,并通过我读了。这确实是问题所在。再次感谢,宾果!就这样。很快地将它移动到活动区,使它能够运行和工作。非常感谢,我想我从来没有注意到这一点。现在是时候让我了解他们之间的不同了。。。实际上,将代码移到上面的最佳方法是什么?
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textview1.setText("1");
}
});
setContentView(R.layout.activity_main);
button1 = (Button) findViewById (R.id.button_1);
textview1 = (TextView) findViewById (R.id.display);