简体   繁体   中英

Force close on starting intent and the activity is mentioned in the android manifest

When I press the button linked to this class, I get a force close. Here's the 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);
            }
        });
    }
}

Here's the 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

Here's the Android Manifest

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

I'm hoping this'll sort out any future problems with this too. I've googled a far bit too but I can't make proper sense of the logcat except for the keywords.

Thanks in advance :)

EDIT: Here's 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;
                }

            }
        });

    }

}

Here's the Results class:

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

}

The solution is just what the error says:

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

Since you haven't posted your QuestionOne class, I can't give you an exact code. But, I would suspect you have something like this:

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

However, you must create a constructor which has no params. That is, public QuestionOne() { .

If you need to pass data to your second Activity , you will need to use Intent extras.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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