android - 强制关闭启动 Intent ,android list 中提到了该 Activity

标签 android button android-intent onclicklistener forceclose

当我按下链接到这个类的按钮时,我得到了一个强制关闭。这是类(class):

public class Introduction extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.introduction);
        Button start = (Button) findViewById(R.id.introstartbutton);
        start.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(v.getContext(), QuestionOne.class);
                startActivity(i);
            }
        });
    }
}

这是 LogCat

09-19 20:40:21.870: E/AndroidRuntime(28122): FATAL EXCEPTION: main
09-19 20:40:21.870: E/AndroidRuntime(28122): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mikenning.foreveralonequiz/com.mikenning.foreveralonequiz.QuestionOne}: java.lang.InstantiationException: can't instantiate class com.mikenning.foreveralonequiz.QuestionOne; no empty constructor
09-19 20:40:21.870: E/AndroidRuntime(28122):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2099)
09-19 20:40:21.870: E/AndroidRuntime(28122):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
09-19 20:40:21.870: E/AndroidRuntime(28122):    at android.app.ActivityThread.access$600(ActivityThread.java:142)
09-19 20:40:21.870: E/AndroidRuntime(28122):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
09-19 20:40:21.870: E/AndroidRuntime(28122):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 20:40:21.870: E/AndroidRuntime(28122):    at android.os.Looper.loop(Looper.java:137)
09-19 20:40:21.870: E/AndroidRuntime(28122):    at android.app.ActivityThread.main(ActivityThread.java:4931)
09-19 20:40:21.870: E/AndroidRuntime(28122):    at java.lang.reflect.Method.invokeNative(Native Method)
09-19 20:40:21.870: E/AndroidRuntime(28122):    at java.lang.reflect.Method.invoke(Method.java:511)
09-19 20:40:21.870: E/AndroidRuntime(28122):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-19 20:40:21.870: E/AndroidRuntime(28122):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-19 20:40:21.870: E/AndroidRuntime(28122):    at dalvik.system.NativeStart.main(Native Method)
09-19 20:40:21.870: E/AndroidRuntime(28122): Caused by: java.lang.InstantiationException: can't instantiate class com.mikenning.foreveralonequiz.QuestionOne; no empty constructor
09-19 20:40:21.870: E/AndroidRuntime(28122):    at java.lang.Class.newInstanceImpl(Native Method)
09-19 20:40:21.870: E/AndroidRuntime(28122):    at java.lang.Class.newInstance(Class.java:1319)
09-19 20:40:21.870: E/AndroidRuntime(28122):    at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
09-19 20:40:21.870: E/AndroidRuntime(28122):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2090)
09-19 20:40:21.870: E/AndroidRuntime(28122):    ... 11 more

这是 Android list

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Light.NoTitleBar" >
        <activity
            android:name=".Introduction"
            android:label="@string/title_activity_introduction" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".QuestionOne"
            android:label="@string/title_activity_introduction" >
        </activity>
        <activity
            android:name=".Result"
            android:label="@string/title_activity_introduction" >
        </activity>
    </application>

我希望这也能解决 future 的任何问题。我也用谷歌搜索了很多,但除了关键字外,我无法正确理解 logcat。

提前致谢:)

编辑:这是 QuestionOne.java

public class QuestionOne extends Results {

    public QuestionOne(int score) {
        super(score);
        // TODO Auto-generated constructor stub
        score = super.score;
    }

    RadioButton answer1, answer2, answer3;
    Button oneNext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.one);

        RadioGroup answerGroup = (RadioGroup) findViewById(R.id.oneradiogroup);
        final int index = answerGroup.indexOfChild(findViewById(answerGroup.getCheckedRadioButtonId()));

        Button oneNext = (Button) findViewById(R.id.onenext);

        oneNext.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent questionTwo = new Intent("com.mikenning.foreveralonequiz.QuestionTwo");
                switch(index) {
                case 1 : score = score + 10; // y is the score of the second answer
                startActivity(questionTwo);
                break;
                case 2 : score = score + 20; // z is the score of the third answer
                startActivity(questionTwo);
                break;
                }

            }
        });

    }

}

这是结果类:

public class Results extends Activity {

    public int score;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        TextView textScore = (TextView) findViewById(R.id.textView1);
        textScore.setText("Your score is " + score);
    }

    public int getScore() {
        return score;
    }

    public Results(int score) {
        this.score = 0;
    }

}

最佳答案

解决方案就是错误所说的:

can't instantiate class com.mikenning.foreveralonequiz.QuestionOne; no empty constructor

因为您还没有发布您的QuestionOne 类(class),所以我无法给您一个准确的代码。但是,我怀疑你有这样的事情:

public class QuestionOne extends Activity {
    public QuestionOne(someType someParam) {
        // ...
    }
    // ...
}

但是,您必须创建一个没有 参数的构造函数。即,public QuestionOne() {

如果您需要将数据传递给您的第二个Activity,您将需要使用Intent。额外费用。

关于android - 强制关闭启动 Intent ,android list 中提到了该 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12501980/

相关文章:

android - 在Android中,如何在不拉伸(stretch)的情况下将图像放在按钮的中心?

android - 在广播接收器中检测呼出调用结束状态

android - 如何在 android 中显示我的应用程序的费率对话框

android - java.lang.NoClassDefFoundError : Failed resolution of: Landroid/support/v4/util/ArrayMap; 错误

Android 通知管理器和时间戳

ruby - 如何在 Sinatra 中制作一个发布按钮

android - 状态按钮选择器android

android - 在我的广播接收器的 onreceive() 方法中,如何判断 Intent 的来源

android - adb.exe 不可执行。 ANDROID_HOME 未设置

android - 如何在加载包含多个以编程方式创建的 View 的新 View 时显示进度对话框?