Java 当我使用xml对象时,我的应用程序崩溃
我宣布b1,b2,b3。。。在这上面。。。 如果(savedInstanceState==null){} } 当我删除OnclickListener时,我的应用程序工作正常,我吓坏了。差不多两天了,我试图找出我做错了什么,但对我来说一切似乎都是对的。当我引用edittext或textview(类似于.getText)时,不仅仅是onclicklistener。我只是不知道该怎么办。我的setContentView引用的是正确的活动,我对它进行了三次检查,如果有人知道我做错了什么,请帮助:s 这是我的全部xmlJava 当我使用xml对象时,我的应用程序崩溃,java,android,Java,Android,我宣布b1,b2,b3。。。在这上面。。。 如果(savedInstanceState==null){} } 当我删除OnclickListener时,我的应用程序工作正常,我吓坏了。差不多两天了,我试图找出我做错了什么,但对我来说一切似乎都是对的。当我引用edittext或textview(类似于.getText)时,不仅仅是onclicklistener。我只是不知道该怎么办。我的setContentView引用的是正确的活动,我对它进行了三次检查,如果有人知道我做错了什么,请帮助:s 这是
.
.
.
<LinearLayout
android:id="@+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/polje1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:hint=" "
android:height="30dp"
/>
<TextView
android:id="@+id/polje2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:hint=" "
android:height="30dp"
/>
<TextView
android:id="@+id/polje3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:hint=" "
android:height="30dp"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="3" >
<Button
android:id="@+id/broj1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:text="1"
android:textStyle="bold"
/>
<Button
android:id="@+id/broj2"
android:text="2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:textStyle="bold"
/>
<Button
android:id="@+id/broj3"
android:text="3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="right"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="3" >
<Button
android:id="@+id/broj4"
android:text="4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="right"
android:textStyle="bold"
/>
<Button
android:id="@+id/broj5"
android:text="5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:textStyle="bold"
/>
<Button
android:id="@+id/broj6"
android:text="6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="3" >
<Button
android:id="@+id/broj7"
android:text="7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="right"
android:textStyle="bold"
/>
<Button
android:id="@+id/broj8"
android:text="8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:textStyle="bold"
/>
<Button
android:id="@+id/broj9"
android:text="9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="3" >
<Button
android:id="@+id/broj0"
android:text="0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="right"
android:textStyle="bold"
/>
<Button
android:id="@+id/plus"
android:text="+"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:textStyle="bold"
/>
<Button
android:id="@+id/minus"
android:text="-"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="3" >
<Button
android:id="@+id/puta"
android:text="*"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="right"
android:textStyle="bold"
/>
<Button
android:id="@+id/djeljeno"
android:text="/"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:textStyle="bold"
/>
<Button
android:id="@+id/kvadrat"
android:text="x²"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="3" >
<Button
android:id="@+id/izracunaj"
android:text="IZRACUNAJ"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
。
.
.
您将代码封装在
if(savedInstanceState == null){
}
这不是在onCreate中声明变量的地方。这是您应该恢复活动状态的地方
如果不需要恢复状态,可以完全删除此检查。否则,将所有代码移出与恢复状态无关的块
编辑:
我看到您正在向活动中添加一个片段,但您似乎仍在活动中完成大部分任务。这就是你想做的吗 进行一些更改,首先查找savedInstanceState条件之外的视图:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.broj1);
TextView e1 = (TextView) findViewById(R.id.polje1);
// other views
}
更新:
删除此行:
getFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit();
另外,检查activity\u main是否为正确的布局,其中包含所有线性布局、按钮、文本视图等。您必须将对象引用(如按钮)放在您的视图之外,如果因为savedinstance它不为null,它将永远不会引用它 这段代码应该适合您,只需记住匹配主xml上的正确名称:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
Button b1 = (Button) findViewById(R.id.broj1);
TextView e1 = (TextView) findViewById(R.id.polje1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
e1.setText("Button 1 Clicked");
}
});
}
测试一下。日志显示了什么?请添加跟踪错误和xml文件。请向我们显示您的xml文件好吗?它可能有一些错误,但你可以只关注第一个按钮,因为所有其他对象的问题都是samei做的,但我的应用程序仍然崩溃发布你的stacktrace请抱歉,我是android编程新手,所以我不太清楚你在问什么?我只是想给我的按钮添加一些功能我可能错了,但我想你的目的不是添加一个占位符片段。尝试删除以下代码:`getFragmentManager().beginTransaction().add(R.id.container,new Placeholder Fragment()).commit()`当我删除那个部分并点击onclicklistener时,我的应用程序启动了,但它没有显示任何东西,只是显示了白色屏幕。我也尝试了这种方式,但当我这样做时,我的broj1clicked没有被识别,当我删除b1时,它正在运行。setOnClickListener(new onclicklistener(){@Override public void onClick(视图arg0){text1.setText(“1”);});但是,我的命令是crashesOMG,谢谢你:DD,我刚刚输入了fragment_main,现在它工作了。非常感谢你,我不敢相信我犯了那个错误:我知道!很高兴它现在工作了-而且,我尝试了你的布局,并且
weight
属性没有任何问题。它显示得很好!我做了,但是crashesi做了,我只是尝试了o当我按下按钮时,使其更改TextView的文本
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
Button b1 = (Button) findViewById(R.id.broj1);
TextView e1 = (TextView) findViewById(R.id.polje1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
e1.setText("Button 1 Clicked");
}
});
}