0

我已经使用多个活动来处理 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编程很陌生。

4

2 回答 2

2

MultiViews2 类中下一行的问题

Button next = (Button) findViewById(R.id.button2); 

代替

Button next = (Button) findViewById(R.id.button1);  
于 2012-06-19T09:10:24.090 回答
2

好吧,考虑到您的观点令人困惑,我不能说哪个是 main.xml,哪个是 main2.xml - 如果您看到引用了按钮 1,则错误出在其中一个活动中。

 Button next = (Button) findViewById(R.id.button1); 

所以很明显,它应该是

  Button next = (Button) findViewById(R.id.button2); 

做出改变,它应该工作。并且为了便于理解,将视图 1 更改为对应于按钮 1,类似地更改为 2。否则,您将遇到更多此类问题

于 2012-06-19T09:12:41.813 回答