私は私の手を掴むことができるすべての提案を見ましたが、それらの修正はすでに私のコードにありました。 基本的には、ボタンをクリックした後、メインアクティビティから別のアクティビティを呼び出そうとしています。日食AVDでは、残念なことにTest1が動作を停止しました
//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>
おお、私の神は、そのような愚かなこと:
はこれを試してみてください。どうもありがとう! – Exorcist
Btw、ロジックに問題がありますか?それは動作しません。以前はAVDの出力として0を示していました。その後、私はクリックの初期化を取り除いた。そして、ボタンをクリックしても、AVDは直接2番目のアクティビティに移動し、結果を0として表示します。@Binyamin Sharet – Exorcist
リスナーではなく、onCreateメソッドで新しいアクティビティを開始するようにコードを記述します。 onCreateの最後で、任意のボタンを押す前に新しいアクティビティを起動します。 – MByD