简体   繁体   中英

handelling multiple views in android

I have used multiple activities to handle mutiple views in android . I found that some where in the blog but I'm lost inside it.

I'm not able to switch between 2 views , my code is as follows:

main file

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>

another activity:

 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>

manifest :

<?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>

whenever I click on button2 it shows me error the application has stopped unexpectedly .
Anything I missed above. I'm very new to android programming.

Problem in Below line in MultiViews2 Class

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

Instead Of

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

Well considering your views are confusing and I cant say which one is main.xml and which one is main2.xml - the error is in one of the activities, both of them if you see have button 1 being referenced.

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

So obviously for one it should be

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

make that change and it should work. And for ease of understanding change view 1 to correspond to button 1 and similarly for 2. Else you'll run into more such problems

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM