0

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.

GAMA
5,99215 gold badges82 silver badges129 bronze badges
asked Jun 19, 2012 at 9:06
1
  • Can you post the LogCat error also, so that we get to know the exact problem?? Commented Jun 19, 2012 at 9:08

2 Answers 2

2

Problem in Below line in MultiViews2 Class

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

Instead Of

Button next = (Button) findViewById(R.id.button1); 
answered Jun 19, 2012 at 9:10
Sign up to request clarification or add additional context in comments.

Comments

2

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

answered Jun 19, 2012 at 9:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.