我无法让这个类通过按钮打开另一个类/ xml

时间:2014-04-13 23:34:55

标签: java android xml

每当我尝试测试时,当我点击“下一步”按钮(但是按下按钮)时应用程序崩溃。我已经尝试重做XML了,多次查看代码,但我无法弄明白。任何帮助都会非常感激。这也是5系列中的第二个类,第一个(主)类开关就这个类/ 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中有一个拼写错误。 > _<

3 个答案:

答案 0 :(得分:1)

只需删除属性" public Button butHOURS;"

TKS

答案 1 :(得分:0)

您已经创建了一个变量:

public Button butHOURS;

然后,您应该按如下方式使用此变量:

butHOURS = (Button) findViewById(R.id.butHOURS);  

你的代码中的变量是无用的。但是,真正的问题是(正如你在LogCat中看到的那样):

have you declared this activity in your AndroidManifest.xml?  

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

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

您的应用中的每个活动都需要在上面的Manifest文件中声明。

答案 2 :(得分:0)

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

<activity android:name=".Main3"></activity>在你的下面 <application> ... </aplication>