0

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

  • 1
    Looks like you have created constructor in your `Activity`, You should not create constructor in the `Activity` classes. Use the Lifecycle callbacks.. – Praful Bhatnagar May 20 '14 at 15:38
  • http://stackoverflow.com/questions/18939249/android-intentservice-cant-instantiate-class-no-empty-constructor – zgc7009 May 20 '14 at 15:38

2 Answers2

1

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.

Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
0

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

Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Yeah but the problem is that I already tried to add an empty constructor to `SecondActi` like this : `public SecondActi(){ super(); }` but then it only switch to an activity with nothing on it – tomatediabolik May 20 '14 at 15:46