简体   繁体   中英

In eclipse AVD,I get “Unfortunately,Test1 has stopped working”

I did look at all the suggestions which I could get my hand on but those corrections were already there in my code..please have a look at this. Basically I am trying to call another activity from the main activity after the click of an button.

//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>

You cannot call setText with an int, as in this case it expects the int to be an ID of a resource.

Instead, make it a string:

text.setText("" + a);

There was some errors in your code.

1) You were registering the onclicklisteners to button1 only. (two times)

2) You put your intent outside the OnClickListener() of the button. You should put it inside the OnClickListener();

Try This Way:

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;      
Intent i = new Intent(this,Sanjay.class);
i.putExtra(pass,clicked);
startActivity(i);      
}
});

button2.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);      
}
});
}

You can use this one if you want to implement the OnClickListener in your activity.

Try This One:

public class Test1Activity extends Activity implements View.OnCLickListener(){
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.setOnClickListener(this);
button2.setOnClickListener(this);    
}

@Override
OnClick(View v)
{
  switch(v.getId())
  {
  case R.id.button1:
   //clicked = v.getId();
   //   OR
   //clicked = 1
   Intent i = new Intent(Test1Activity.this,Sanjay.class);
   i.putExtra(pass,clicked);
   startActivity(i);      
   break;

  case R.id.button2:
   //clicked = v.getId();
   //   OR
   //clicked = 2
   Intent i = new Intent(Test1Activity.this,Sanjay.class);
   i.putExtra(pass,clicked);
   startActivity(i);        
  }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM