我初始化了一个按钮
button1= (Button)findViewById(R.id.button1);
我有OnClick()函数
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
My New Activity的名称是Manifest文件中的MainActivity2
<activity
android:name="com.example.intent.MainActivity2"
android:label="@string/title_activity_main_activity2" >
</activity>
但第二项活动从未启动
请帮忙。
活动1
public class MainActivity extends Activity implements OnClickListener {
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1= (Button)findViewById(R.id.button1);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
}
XML LAYOUT
<activity
android:name="com.example.intent.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.intent.MainActivity2"
android:label="@string/title_activity_main_activity2" >
</activity>
</application>
答案 0 :(得分:1)
您没有在按钮上注册onClick
事件。
来自:
public class MainActivity extends Activity implements OnClickListener {
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1= (Button)findViewById(R.id.button1);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
}
收件人:
public class MainActivity extends Activity implements OnClickListener {
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1= (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
}
答案 1 :(得分:0)
更改
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
到
button1.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
};
您应该将onClicklistener设置为按钮
答案 2 :(得分:0)
哥们你只需要从你的代码中改变一个单词..
public class MainActivity extends Activity implements OnClickListener {
Button button1;
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (R.id.button1) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1= (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
这肯定会奏效......