1

当我按下链接到这个类的按钮时,我会强制关闭。这是课程:

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

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

我希望这也能解决任何未来的问题。我也用谷歌搜索了很多,但除了关键字之外,我无法正确理解 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;
    }

}
4

1 回答 1

1

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

无法实例化 com.mikenning.foreveralonequiz.QuestionOne 类;没有空的构造函数

由于您尚未发布QuestionOne课程,因此我无法给您确切的代码。但是,我怀疑你有这样的事情:

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

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

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

于 2012-09-19T20:02:44.463 回答