当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Having Error in Textview

问题描述:

I am unable to solve the error and i have spent more than hour to resolve it but can't identify.

mainactivity.java

 protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

mDrawerList = (ListView) findViewById(R.id.list_item);

mDrawerListItems = getResources().getStringArray(R.array.nav_drawer_labels);

mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.activity_list_item, mDrawerListItems));

mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

int editedPosition = position + 1;

Toast.makeText(MainActivity.this, "You Selected item" + editedPosition, Toast.LENGTH_SHORT).show();

mDrawerLayout.closeDrawer(mDrawerList);

}

});

mDrawerToggle = new ActionBarDrawerToggle(this,

mDrawerLayout,

toolbar,

R.string.drawer_open,

R.string.drawer_close) {

public void onDrawerClosed(View v) {

super.onDrawerClosed(v);

invalidateOptionsMenu();

syncState();

}

public void onDrawerOpened(View v) {

super.onDrawerOpened(v);

invalidateOptionsMenu();

syncState();

}

};

/*drawerFragment = (FragmentDrawer)

getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);

drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);

drawerFragment.setDrawerListener(this);*/

}

And i am getting an error in log

java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)

at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)

at android.widget.AbsListView.obtainView(AbsListView.java:2347)

at android.widget.ListView.makeAndAddView(ListView.java:1864)

at android.widget.ListView.fillDown(ListView.java:698)

at android.widget.ListView.fillFromTop(ListView.java:759)

at android.widget.ListView.layoutChildren(ListView.java:1673)

at android.widget.AbsListView.onLayout(AbsListView.java:2151)

at android.view.View.layout(View.java:15671)

at android.view.ViewGroup.layout(ViewGroup.java:5038)

at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:931)

at android.view.View.layout(View.java:15671)

at android.view.ViewGroup.layout(ViewGroup.java:5038)

at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)

at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)

at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)

at android.view.View.layout(View.java:15671)

at android.view.ViewGroup.layout(ViewGroup.java:5038)

at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)

at android.widget.FrameLayout.onLayout(FrameLayout.java:514)

at android.view.View.layout(View.java:15671)

at android.view.ViewGroup.layout(ViewGroup.java:5038)

at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)

at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)

at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)

at android.view.View.layout(View.java:15671)

at android.view.ViewGroup.layout(ViewGroup.java:5038)

at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)

at android.widget.FrameLayout.onLayout(FrameLayout.java:514)

at android.view.View.layout(View.java:15671)

at android.view.ViewGroup.layout(ViewGroup.java:5038)

at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)

at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)

at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)

at android.view.View.layout(View.java:15671)

at android.view.ViewGroup.layout(ViewGroup.java:5038)

at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)

at android.widget.FrameLayout.onLayout(FrameLayout.java:514)

at android.view.View.layout(View.java:15671)

at android.view.ViewGroup.layout(ViewGroup.java:5038)

at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)

at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)

at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)

at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)

at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)

at android.view.Choreographer.doCallbacks(Choreographer.java:580)

at android.view.Choreographer.doFrame(Choreographer.java:550)

at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)

at android.os.Handler.handleCallback(Handler.java:739)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:135)

at android.app.ActivityThread.main(ActivityThread.java:5257)

at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView

And i am using android Studio tool

Would be helpful if someone suggest me where i am wrong.

网友答案:

You go wrong over here

 mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.activity_list_item, mDrawerListItems));

Just change it

 mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.activity_list_item, mDrawerListItems));

Supply your R.layout.activity_list_item layout to ArrayAdapter

分享给朋友:
您可能感兴趣的文章:
随机阅读: