简体   繁体   中英

Can't switch between activities

sorry for asking dumb questions, java and Android are both new to me ;)

My problem: I can't switch between two activities in a very simple app. I tried solutions described in similar topics but it didn't work.

So this is my 1st Activity (I didn't paste the imports):

public class OneActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void OnStart(){
    Button Btn = (Button)findViewById(R.id.btnNext);     
    Btn.setOnClickListener(new OnClickListener() {

        public void onClick(View Button) {

    Intent myIntent = new Intent(OneActivity.this, UserInput.class); 
    OneActivity.this.startActivity(myIntent);

        } 

});
}

}

The second Activity is very simple - it is just supposed to load a layout called userinput.xml:

public class UserInput extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.userinput);

}

}

The application part of the Manifest looks like following:

 <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <activity
        android:name=".OneActivity"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>


        <activity 
            android:name=".UserInput" 
            android:label="@string/app_name" />


    </activity>
</application>

When I run the app and click the button nothing happens. Where could be the problem?

// Alright, I have put the code into the onCreate() method so it now looks like following:

public class OneActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button Btn = (Button)findViewById(R.id.btnNext);     
    Btn.setOnClickListener(new OnClickListener() {

        public void onClick(View Button) {

    Intent myIntent = new Intent(OneActivity.this, UserInput.class); 
    OneActivity.this.startActivity(myIntent);

        } 

});
}

}

Now the app crashes (force close) anytime I click the Next button.

You define your onStart() function with a capital 'O'. That is why the function is never called.

Your onStart():

public void OnStart(){ ... }

How it should be:

// Note the lowercase 'o' in onStart
public void onStart(){ ... }

Also note that having an @Override above the function name when you want to override a method will help prevent making these mistakes, as Eclipse (or whatever IDE you use) will tell you that you are not actually overriding a function.

Write below code in onCreate() method:

Button Btn = (Button)findViewById(R.id.btnNext);     
    Btn.setOnClickListener(new OnClickListener() {

        public void onClick(View Button) {

    Intent myIntent = new Intent(OneActivity.this, UserInput.class); 
    startActivity(myIntent);

        } 

});

2nd correction for android manifest file:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <activity
        android:name=".OneActivity"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>                     <!-- closed here -->

        <activity 
            android:name=".UserInput" 
            android:label="@string/app_name" />



</application>

Check it following line

Intent myIntent = new Intent(OneAvtivity.this, SecondActivity.class); 
startActivity(myIntent);

Your Class name is UserIpnout and you write SecondActivity.class

Intent myIntent = new Intent(OneAvtivity.this, UserInput .class); 
startActivity(myIntent);

You can move all the code in onCreate() and simplify like this

startActivity(new Intent(this, UserInput.class));

But I don't understand why you start another activity like this

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