eclipse中没有错误,但应用程序在完全加载之前崩溃了

时间:2014-07-20 15:31:00

标签: java android eclipse

我是Android应用程序制作新手,我正在使用eclipse。 Ecplise没有显示任何错误,但是,当我使用模拟器时,应用程序在完全加载之前崩溃。感谢您的帮助:)

我的.java文件     package com.example.praag_complete;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
private String[] mListItems;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private String mTitle;
private String mDrawerTitle;

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);


     mDrawerTitle = "Menu";

     mListItems = getResources().getStringArray(R.array.drawer_primary_array);
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
     mDrawerList = (ListView) findViewById(R.id.left_drawer);

     // Set the adapter for the listview
     mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item,mListItems));
     // Set the list's click listener        
     mDrawerList.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
            Toast.makeText(getApplicationContext(),
              "Click ListItem Number " + position, Toast.LENGTH_LONG).show();
            mTitle = "Section" + position;
          }
        });
    //openy closy thingy
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
     mDrawerToggle = new ActionBarDrawerToggle(
             this,                  /* host Activity */
             mDrawerLayout,         /* DrawerLayout object */
             R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
             R.string.drawer_open,  /* "open drawer" description */
             R.string.drawer_close  /* "close drawer" description */
             ) {

         /** Called when a drawer has settled in a completely closed state. */
         public void onDrawerClosed(View view) {
             super.onDrawerClosed(view);
             getActionBar().setTitle(mTitle);
         }

         /** Called when a drawer has settled in a completely open state. */
         public void onDrawerOpened(View drawerView) {
             super.onDrawerOpened(drawerView);
             getActionBar().setTitle(mDrawerTitle);
         }
     };

     // Set the drawer toggle as the DrawerListener
     mDrawerLayout.setDrawerListener(mDrawerToggle);

     getActionBar().setDisplayHomeAsUpEnabled(true);
     getActionBar().setHomeButtonEnabled(true);
 }

 @Override
 protected void onPostCreate(Bundle savedInstanceState) {
     super.onPostCreate(savedInstanceState);
     // Sync the toggle state after onRestoreInstanceState has occurred.
     mDrawerToggle.syncState();
 }

 @Override
 public void onConfigurationChanged(Configuration newConfig) {
     super.onConfigurationChanged(newConfig);
     mDrawerToggle.onConfigurationChanged(newConfig);
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
     // Pass the event to ActionBarDrawerToggle, if it returns
     // true, then it has handled the icon touch event
     if (mDrawerToggle.onOptionsItemSelected(item)) {
       return true;
     }
     // Handle your other action bar items...

     return super.onOptionsItemSelected(item);
 }   
}

和LogCat报告:

07-20 15:19:56.304: E/AndroidRuntime(874): FATAL EXCEPTION: main
07-20 15:19:56.304: E/AndroidRuntime(874): java.lang.IllegalStateException: ArrayAdapter requires                     the resource ID to be a TextView
07-20 15:19:56.304: E/AndroidRuntime(874):  at     android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
07-20 15:19:56.304: E/AndroidRuntime(874):  at     android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.widget.AbsListView.obtainView(AbsListView.java:2271)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.widget.ListView.makeAndAddView(ListView.java:1769)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.widget.ListView.fillDown(ListView.java:672)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.widget.ListView.fillFromTop(ListView.java:733)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.widget.ListView.layoutChildren(ListView.java:1622)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.widget.AbsListView.onLayout(AbsListView.java:2106)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.View.layout(View.java:13754)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.ViewGroup.layout(ViewGroup.java:4364)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:825)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.View.layout(View.java:13754)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.ViewGroup.layout(ViewGroup.java:4364)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.View.layout(View.java:13754)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.ViewGroup.layout(ViewGroup.java:4364)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.View.layout(View.java:13754)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.ViewGroup.layout(ViewGroup.java:4364)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.View.layout(View.java:13754)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.ViewGroup.layout(ViewGroup.java:4364)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1868)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1689)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.Choreographer.doFrame(Choreographer.java:525)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.os.Handler.handleCallback(Handler.java:615)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.os.Looper.loop(Looper.java:137)
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.app.ActivityThread.main(ActivityThread.java:4745)
07-20 15:19:56.304: E/AndroidRuntime(874):  at java.lang.reflect.Method.invokeNative(Native Method)
07-20 15:19:56.304: E/AndroidRuntime(874):  at java.lang.reflect.Method.invoke(Method.java:511)
07-20 15:19:56.304: E/AndroidRuntime(874):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-20 15:19:56.304: E/AndroidRuntime(874):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-20 15:19:56.304: E/AndroidRuntime(874):  at dalvik.system.NativeStart.main(Native Method)
07-20 15:19:56.304: E/AndroidRuntime(874): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
07-20 15:19:56.304: E/AndroidRuntime(874):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
07-20 15:19:56.304: E/AndroidRuntime(874):  ... 40 more
07-20 15:19:57.614: D/dalvikvm(874): GC_CONCURRENT freed 229K, 5% free 6229K/6535K, paused 398ms+49ms, total 1178ms
07-20 15:20:17.614: I/Process(874): Sending signal. PID: 874 SIG: 9

activity_main:

<android.support.v4.widget.DrawerLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/drawer_layout"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

drawer_list_item:

 <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight" >

<TextView
    android:text="@string/app_name"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

更改

mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item,mListItems));

mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item,R.id.the_id_of_a_textview_from_the_layout,mListItems));

document表示您必须使用单个TextView提供资源。这种布局是什么样的?

我猜你的R.layout.drawer_list_item应该非常相似。

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

/>

以下是医生说的话:

By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.