0

I'm programming my first android application, and I ran into a problem: The app shuts down after I try to open a new Activity.

Here's the activity code:

package com.WNF;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.ListActivity;
import android.content.Intent;


public class Actiemenu extends ListActivity{

String[] array = { "","henk","henk2" };

@Override 
public void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 
setContentView (R.layout.actiemenu);

setListAdapter(new ArrayAdapter<String> (this, R.layout.actiemenu, array));
    ListView lv = getListView();
    lv.setTextFilterEnabled(true);



lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

        if (position == 0) {
            Intent een = new Intent(Actiemenu.this, Acties.class);
            startActivity(een);
        }
        else if (position == 1) {
            Intent twee = new Intent(Actiemenu.this, Acties2.class);
            startActivity(twee);
        }
    }
});
getIntent();
}
}

And this is logcat's log :

06-13 06:50:13.920: E/AndroidRuntime(335): FATAL EXCEPTION: main
06-13 06:50:13.920: E/AndroidRuntime(335): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.AbsListView.obtainView(AbsListView.java:1430)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.ListView.makeAndAddView(ListView.java:1745)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.ListView.fillDown(ListView.java:670)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.ListView.fillFromTop(ListView.java:727)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.ListView.layoutChildren(ListView.java:1598)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.AbsListView.onLayout(AbsListView.java:1260)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.view.View.layout(View.java:7175)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.view.View.layout(View.java:7175)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.view.View.layout(View.java:7175)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.view.View.layout(View.java:7175)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.view.View.layout(View.java:7175)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.os.Looper.loop(Looper.java:123)
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-13 06:50:13.920: E/AndroidRuntime(335):  at java.lang.reflect.Method.invokeNative(Native Method)
06-13 06:50:13.920: E/AndroidRuntime(335):  at java.lang.reflect.Method.invoke(Method.java:507)
06-13 06:50:13.920: E/AndroidRuntime(335):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-13 06:50:13.920: E/AndroidRuntime(335):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-13 06:50:13.920: E/AndroidRuntime(335):  at dalvik.system.NativeStart.main(Native Method)
06-13 06:50:13.920: E/AndroidRuntime(335): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
06-13 06:50:13.920: E/AndroidRuntime(335):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
06-13 06:50:13.920: E/AndroidRuntime(335):  ... 30 more
3
  • 1
    Please provide R.layout.actiemenu xml ...... Commented Jun 13, 2012 at 7:12
  • setListAdapter(new ArrayAdapter<String> (this, R.layout.actiemenu, array)); ???????????? Commented Jun 13, 2012 at 7:13
  • why you are setting actiemenu.xml as source to ArrayAdapter AND as a main layout also... Commented Jun 13, 2012 at 7:14

2 Answers 2

2

Just look at your exception more carefully, you have the answer why your activity is throwing an exception : ArrayAdapter requires the resource ID to be a TextView. So you have to change your code to this : setListAdapter(new ArrayAdapter<String> (this, R.id.yourTextViewId, array));

Here you can find more information about ArrayAdapter.

Sign up to request clarification or add additional context in comments.

Comments

0

In android ArrayAdapter is predefined Adapter which has already defined it's get function as well . So that Adapter it requires some prededinfed id's like "text: is for text view. so it has provided some predefined XML layout. So either use them or ...

So if wants to use your own layout with own id's just make your custom adapter extending the ArrayAdapter and override the getview function...

How to use a custom ArrayAdapter in a separate class?

How to use ArrayAdapter<myClass>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.