Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我无法通过按钮使该类打开另一个类/xml_Java_Android_Xml - Fatal编程技术网

Java 我无法通过按钮使该类打开另一个类/xml

Java 我无法通过按钮使该类打开另一个类/xml,java,android,xml,Java,Android,Xml,每次我尝试测试时,当我点击“下一步”按钮(butHOURS按钮)时,应用程序就会崩溃。我尝试过重做XML,查看代码好几次,但我都弄不明白。任何帮助都将不胜感激。这也是5个系列中的第二个类,第一个(主)类可以很好地切换到此class/xml布局 package com.pewpew.studentadvisor; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import

每次我尝试测试时,当我点击“下一步”按钮(butHOURS按钮)时,应用程序就会崩溃。我尝试过重做XML,查看代码好几次,但我都弄不明白。任何帮助都将不胜感激。这也是5个系列中的第二个类,第一个(主)类可以很好地切换到此class/xml布局

package com.pewpew.studentadvisor;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;

public class Main2 extends Activity 
{

public Button butHOURS;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);

    RadioButton hours1 = (RadioButton) findViewById(R.id.hours1);
    RadioButton hours2 = (RadioButton) findViewById(R.id.hours2);
    RadioButton hours3 = (RadioButton) findViewById(R.id.hours3);
    RadioButton hours4 = (RadioButton) findViewById(R.id.hours4);

    //Calculation based on ENROLLED question
    if (hours1.isChecked()){Main.calculation = Main.calculation + 1;}
    else{ if (hours2.isChecked())   {Main.calculation = Main.calculation + 2;   }
    else{ if (hours3.isChecked()) {Main.calculation = Main.calculation + 3; }
    else{ if (hours4.isChecked())   {Main.calculation = Main.calculation + 4;   }
    else{Main.calculation = 0.0;    }}}}


    //Get button to do button stuff like go to the next page
    Button butHOURS = (Button) findViewById(R.id.butHOURS);
    butHOURS.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            Intent i = new Intent(Main2.this, Main3.class);
            startActivity(i);
        }
    });

}

}
这是坠机日志

04-13 19:43:54.820: D/AndroidRuntime(26281): Shutting down VM
04-13 19:43:54.820: W/dalvikvm(26281): threadid=1: thread exiting with uncaught exception (group=0x41548360)
04-13 19:43:54.830: E/AndroidRuntime(26281): FATAL EXCEPTION: main
04-13 19:43:54.830: E/AndroidRuntime(26281): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.pewpew.studentadvisor/com.pewpew.studentadvisor.Main3}; have you declared this activity in your AndroidManifest.xml?
04-13 19:43:54.830: E/AndroidRuntime(26281):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1556)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1431)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at android.app.Activity.startActivityForResult(Activity.java:3417)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at android.app.Activity.startActivityForResult(Activity.java:3378)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at android.app.Activity.startActivity(Activity.java:3588)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at android.app.Activity.startActivity(Activity.java:3556)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at com.pewpew.studentadvisor.Main2$1.onClick(Main2.java:43)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at android.view.View.performClick(View.java:4192)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at android.view.View$PerformClick.run(View.java:17254)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at android.os.Handler.handleCallback(Handler.java:615)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at android.os.Looper.loop(Looper.java:137)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at android.app.ActivityThread.main(ActivityThread.java:4950)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at java.lang.reflect.Method.invokeNative(Native Method)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at java.lang.reflect.Method.invoke(Method.java:511)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
04-13 19:43:54.830: E/AndroidRuntime(26281):    at dalvik.system.NativeStart.main(Native Method)

对于后来发现这个的人来说,我的错误是我的AndroidManifest中有一个输入错误。>_ 您已经创建了一个变量:

public Button butHOURS;
然后,应按如下方式使用此变量:

butHOURS = (Button) findViewById(R.id.butHOURS);  
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.pewpew.studentadvisor.MainActivity"
        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="com.pewpew.studentadvisor.Main3"
        android:label="@string/app_name" /> 
    // other activities
</application> 
您的变量在代码中是无用的。然而,真正的问题是(正如您在日志中看到的):

您应该在清单中声明
Main3
活动,如下所示:

butHOURS = (Button) findViewById(R.id.butHOURS);  
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.pewpew.studentadvisor.MainActivity"
        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="com.pewpew.studentadvisor.Main3"
        android:label="@string/app_name" /> 
    // other activities
</application> 

//其他活动
应用程序中的每个活动都需要如上所述在清单文件中声明。

只需删除属性“public Button butHOURS;”


tks问题在您的清单文件中。您应该像这样声明一个新活动:

在您的

请发布logcat中的详细异常。确定将在一分钟内编辑您的问题可能是您没有在清单文件中声明下一个活动。我认为我做得正确。。。我也应该发布清单文件吗?删除了它,但它仍然崩溃。很抱歉,我刚才写错了按钮,但是小时是对的,我删除了变量“public button butHOURS;”,它仍然崩溃。事实上,我不知道如果在查找IdSee我的更新答案时使用它,我不需要声明它。你没有在你的舱单上申报活动哦天哪。。。菲罗,谢谢。我在清单中是这样写的:“真的很高兴能帮助你。对不起,我只是想告诉你我打错了,当我去输入你写的东西时发现了。”。非常感谢!