我已经使用多个活动来处理 android 中的多个视图。我在博客中找到了一些地方,但我迷失在其中。
我无法在 2 个视图之间切换,我的代码如下:
主文件
public class MultiViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), MultiViews2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
xml:
<?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" >
<Button android:text="View 2"
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
另一项活动:
public class MultiViews2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), MultiViewActivity.class);
startActivityForResult(myIntent, 0);
}
});
}
}
xml:
<?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" >
<Button android:text="View 1"
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
显现 :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.multiview.org"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MultiViewActivity"
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=".MultiViews2"></activity>
</application>
</manifest>
每当我单击 button2 时,它都会显示错误the application has stopped unexpectedly
。
上面我错过的任何东西。我对android编程很陌生。