gpt4 book ai didi

java - 在 Eclipse AVD 中,我得到 "Unfortunately,Test1 has stopped working"

转载 作者:行者123 更新时间:2023-12-02 00:28:08 25 4
gpt4 key购买 nike

我确实查看了我可以得到的所有建议,但这些更正已经存在于我的代码中..请看看这个。基本上,我试图在单击按钮后从主 Activity 中调用另一个 Activity 。

//Here's the code of my main activity named Test1Activity:
public class Test1Activity extends Activity {

private int clicked=0;
public static final String pass="com.sanjay.test1._clicked";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1=(Button) findViewById(R.id.button1);
Button button2=(Button) findViewById(R.id.button2);

button1.setId(1);
button2.setId(2);

button1.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
int id = v.getId();
clicked=id;

}
});
button1.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
int id = v.getId();
clicked=id;
}
});Intent i = new Intent(this,Sanjay.class);i.putExtra(pass,clicked);startActivity(i);
}

}

//Now my other Activity code : 
public class Sanjay extends Activity {

/** Called when the activity is first created. */
int a;
TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);

text=(TextView) findViewById(R.id.text);
a=getIntent().getExtras().getInt(Test1Activity.pass);

text.setText(a);
// TODO Auto-generated method stub
}
}


//And my xml file of the second activity layout resource.
//I am not adding the main.xml as the program runs fine if I pass the startactivity(i) and the intent lines as comments.
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

最佳答案

您不能使用 int 调用 setText,因为在本例中,它期望 int 是资源的 ID。

相反,将其设为字符串:

text.setText("" + a);

关于java - 在 Eclipse AVD 中,我得到 "Unfortunately,Test1 has stopped working",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9657821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com