我确实看了所有可以得到的建议,但是我的代码中已经有了这些更正。请看看这个。 基本上我在点击按钮后尝试从主活动中调用另一个活动。
//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>
答案 0 :(得分:2)
您不能使用int调用setText
,因为在这种情况下,它期望int
是资源的ID。
相反,请将其设为字符串:
text.setText("" + a);
答案 1 :(得分:1)
您的代码中存在一些错误。
1)您只是将onclicklisteners注册到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);
}
});
}
答案 2 :(得分:0)
如果要在活动中实现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);
}
}
}