有人可以告诉我,我错了。我有三个活动,我想连接togever。这段代码是我的第一个java文件和第一个xml文件。我认为我的onclick代码在某些地方不对。我的最终结果是所有3个活动连接3个图像按钮...谢谢
java 1.code 包my.hope;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.content.Intent;
public class NewhopeActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView myImage = (ImageView) findViewById(R.id.imageButton1);
myImage.setOnClickListener(new OnClickListener() {
intent intent = new intent(Newhopeactivity.this, Act2.class);
startActivity(intent);
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
);
}
}
xml.code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bt" />
<ImageButton
android:id="@id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:onClick="Act2"/>
答案 0 :(得分:1)
您必须将startActivity()放入实际的onClick()方法中。
myImage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
intent intent = new intent(Newhopeactivity.this, Act2.class);
startActivity(intent);
}
}
答案 1 :(得分:0)
更改
ImageView myImage = (ImageView) findViewById(R.id.imageButton1);
myImage.setOnClickListener(new OnClickListener() {
intent intent = new intent(Newhopeactivity.this, Act2.class);
startActivity(intent);
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
到
ImageView myImage = (ImageView) findViewById(R.id.imageButton1);
myImage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new intent(Newhopeactivity.this, Act2.class);
startActivity(intent);
}
}