【问题标题】:In eclipse AVD,I get "Unfortunately,Test1 has stopped working"在 Eclipse AVD 中,我得到“不幸的是,Test1 已停止工作”
【发布时间】:2012-03-11 18:45:11
【问题描述】:

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

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

【问题讨论】:

    标签: java android android-intent android-emulator


    【解决方案1】:

    您不能使用 int 调用 setText,因为在这种情况下它期望 int 是资源的 ID。

    改为字符串:

    text.setText("" + a);
    

    【讨论】:

    • 顺便说一句,逻辑有问题吗?导致它不起作用。之前它在 AVD 上显示 0 作为输出。然后我删除了点击的初始化。现在即使在单击按钮后,AVD 也会直接将我带到第二个活动,但结果仍然显示为 0。@Binyamin Sharet
    • 您将启动新活动的代码放在 onCreate 方法中,而不是在侦听器中,因此在 onCreate 结束时,它会在您按下任何按钮之前启动新活动。
    • 你能告诉它怎么做吗?如果这太多了,只需举个例子或链接就足够了。@Binyamin Sharet
    【解决方案2】:

    您的代码中有一些错误。

    1) 您仅将 onclicklistener 注册到 button1。 (两次)

    2) 你把你的意图放在按钮的 OnClickListener() 之外。你应该把它放在 OnClickListener();

    试试这个方法:

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

    【讨论】:

    • 那是我不想要的。创建许多对象会减慢应用程序的速度。我正在考虑 16 个按钮,创建 16 个对象肯定会减慢它的速度,因此我使用了变量 clicked 以避免创建对象。您上面给出的代码给出了 Intent Constructor 未定义的错误。 @Aki143S
    【解决方案3】:

    如果你想在你的活动中实现 OnClickListener,你可以使用这个。

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 2014-01-03
      • 2016-08-05
      • 1970-01-01
      相关资源
      最近更新 更多