tomatediabolik
tomatediabolik

Reputation: 111

Fatal error using intent inside a ListView

I'm trying to open an activity depending on which item in ListView I chose.

Add click to my ListView :

private void addClickList() {
        myList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Intent intent = new Intent(FirstActi.this, SecondActi.class);
                startActivity(intent);
            }

        });
}

This is my android manifest for these two activities :

        <activity
            android:name=".FirstActi"
            android:label="@string/firstActi">
            <intent-filter>
                <action android:name="com.example.applicationname.FirstActi" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter> 
        </activity>

        <activity
            android:name=".SecondActi"
            android:label="@string/secondActi" >
        </activity>

This is the output from the logcat :

05-20 17:28:58.251: E/AndroidRuntime(27073): FATAL EXCEPTION: main
05-20 17:28:58.251: E/AndroidRuntime(27073): Process: com.example.applicationname, PID: 27073
05-20 17:28:58.251: E/AndroidRuntime(27073): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.applicationname/com.example.applicationname.FirstActi}: java.lang.InstantiationException: can't instantiate class com.example.arduinodivecompanion.SecondActi; no empty constructor
05-20 17:28:58.251: E/AndroidRuntime(27073):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
05-20 17:28:58.251: E/AndroidRuntime(27073):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-20 17:28:58.251: E/AndroidRuntime(27073):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-20 17:28:58.251: E/AndroidRuntime(27073):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-20 17:28:58.251: E/AndroidRuntime(27073):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-20 17:28:58.251: E/AndroidRuntime(27073):    at android.os.Looper.loop(Looper.java:136)
05-20 17:28:58.251: E/AndroidRuntime(27073):    at android.app.ActivityThread.main(ActivityThread.java:5017)
05-20 17:28:58.251: E/AndroidRuntime(27073):    at java.lang.reflect.Method.invokeNative(Native Method)
05-20 17:28:58.251: E/AndroidRuntime(27073):    at java.lang.reflect.Method.invoke(Method.java:515)
05-20 17:28:58.251: E/AndroidRuntime(27073):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-20 17:28:58.251: E/AndroidRuntime(27073):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-20 17:28:58.251: E/AndroidRuntime(27073):    at dalvik.system.NativeStart.main(Native Method)
05-20 17:28:58.251: E/AndroidRuntime(27073): Caused by: java.lang.InstantiationException: can't instantiate class com.example.applicationname.SecondActi; no empty constructor
05-20 17:28:58.251: E/AndroidRuntime(27073):    at java.lang.Class.newInstanceImpl(Native Method)
05-20 17:28:58.251: E/AndroidRuntime(27073):    at java.lang.Class.newInstance(Class.java:1208)
05-20 17:28:58.251: E/AndroidRuntime(27073):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
05-20 17:28:58.251: E/AndroidRuntime(27073):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
05-20 17:28:58.251: E/AndroidRuntime(27073):    ... 11 more

I think that maybe it's a problem with the intent filter

Upvotes: 0

Views: 89

Answers (2)

Gabe Sechan
Gabe Sechan

Reputation: 93726

Nothing to do with intent filters. Read the nested exception:

Caused by: java.lang.InstantiationException: can't instantiate class com.example.applicationname.SecondActi; no empty constructor

Create the constructor it's looking for

Upvotes: 0

Praful Bhatnagar
Praful Bhatnagar

Reputation: 7435

As per android guidelines you should not create constructor in the Activity classes since android OS creates object of Activity classes and it use the default empty constructor of the class to create object. You should not be creating object of Activity directly.

Looks like you have created constructor in your Activity. Remove the constructor from your Activities and use the Lifecycle callbacks.

Upvotes: 1

Related Questions