简体   繁体   English

无法在活动之间切换

[英]Can't switch between activities

sorry for asking dumb questions, java and Android are both new to me ;) 很抱歉提出愚蠢的问题,java和Android都是我的新手;)

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: 第二个活动非常简单-仅加载一个名为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: //好吧,我已将代码放入onCreate()方法中,因此它现在看起来如下所示:

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'. 您用大写的“ O”定义onStart()函数。 That is why the function is never called. 这就是为什么永远不调用该函数的原因。

Your onStart(): 您的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. 还要注意,当您要覆盖方法时,在函数名称上方使用@Override可以帮助避免发生这些错误,因为Eclipse(或您使用的任何IDE)将告诉您实际上并没有覆盖函数。

Write below code in onCreate() method: 在onCreate()方法中编写以下代码:

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: Android清单文件的第二次更正:

<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 您的类名称为UserIpnout,您编写SecondActivity.class

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

You can move all the code in onCreate() and simplify like this 您可以在onCreate()移动所有代码,并像这样简化

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

But I don't understand why you start another activity like this 但我不明白为什么您要开始另一项活动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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