无法从导航抽屉启动活动

时间:2013-06-24 21:03:18

标签: android android-activity navigation drawer

我正在关注android导航抽屉指南,一切顺利,直到我尝试从列表中的项目启动活动。抽屉似乎工作正常,因为我创建的测试Toast消息在单击第一个项目时执行,但是当单击第二个项目时应用程序崩溃。

任何人都可以告诉我问题是什么,或者任何人都可以指出我正确的方向..

这是我的主要活动:

public class MainActivity extends Activity {

private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mGalaxyTitles;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTitle = mDrawerTitle = getTitle();
    mGalaxyTitles = getResources().getStringArray(R.array.list_items);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);


    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mGalaxyTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    if (savedInstanceState == null) {
        selectItem(0);}

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


    mDrawerToggle = new ActionBarDrawerToggle(
            this,                 
            mDrawerLayout,         
            R.drawable.ic_drawer,  
            R.string.hello_world,  
            R.string.hello_world  
            ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); 
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); 
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

  /** if (savedInstanceState == null) {
        selectItem(0);
    }**/
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);}


private class DrawerItemClickListener implements ListView.OnItemClickListener {

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

  switch(position) {
    case 0:
        Toast.makeText(getApplicationContext(), "Some Activities MAY take time to load. Please be Patient.", Toast.LENGTH_LONG).show();   
        //Intent a = new Intent(MainActivity.this, Dev_team.class);
            //startActivity(a);
    break;
    case 1:
        Toast.makeText(getApplicationContext(), "Some Activities MAY take time to load. Please be Patient.", Toast.LENGTH_LONG).show(); 
        Intent a = new Intent(getBaseContext(), dev.class);
         startActivity(a);
           break;
    default:
    }
    }
    }


public void selectItem(int position) {
        switch(position) {
    case 0:
        Toast.makeText(getApplicationContext(), "Some Activities MAY take time to load. Please be Patient.", Toast.LENGTH_LONG).show();   
        //Intent a = new Intent(MainActivity.this, Dev_team.class);
            //startActivity(a);
    break;
    case 1:
        setContentView(R.layout.dev_team);
           break;
    default:
        }
    }


@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}



@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

    mDrawerToggle.syncState();
}

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

    mDrawerToggle.onConfigurationChanged(newConfig);
}


/**public static class GalaxyFragment extends Fragment {
    public static final String ARG_Galaxy_NUMBER = "Galaxy_number";

    public GalaxyFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_Galaxy, container, false);
        int i = getArguments().getInt(ARG_Galaxy_NUMBER);
        String Galaxy = getResources().getStringArray(R.array.list_items)[i];

        int imageId = getResources().getIdentifier(Galaxy.toLowerCase(Locale.getDefault()),
                        "drawable", getActivity().getPackageName());
        ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
        getActivity().setTitle(Galaxy);
        return rootView;
    }**/
}

这是activity_main.xml

<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 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/hello_world"
    android:textSize="18sp" />
</RelativeLayout>
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

这是dev.java

public class dev extends MainActivity{
public void onCreate(Bundle savedInstanceState){
    setContentView(R.layout.dev_team);
}

}

根据Pavel Dudka的建议,这是logcat

    06-25 12:05:12.815: E/Trace(778): error opening trace file: No such file or directory (2)
06-25 12:05:13.736: D/dalvikvm(778): GC_FOR_ALLOC freed 100K, 8% free 2668K/2888K, paused 174ms, total 182ms
06-25 12:05:13.746: I/dalvikvm-heap(778): Grow heap (frag case) to 3.792MB for 1127536-byte allocation
06-25 12:05:13.865: D/dalvikvm(778): GC_FOR_ALLOC freed 2K, 6% free 3767K/3992K, paused 120ms, total 120ms
06-25 12:05:13.925: D/dalvikvm(778): GC_CONCURRENT freed 1K, 5% free 3800K/3992K, paused 12ms+12ms, total 54ms
06-25 12:05:14.296: I/Choreographer(778): Skipped 33 frames!  The application may be doing too much work on its main thread.
06-25 12:05:14.366: D/gralloc_goldfish(778): Emulator without GPU emulation detected.
06-25 12:05:24.766: D/AndroidRuntime(778): Shutting down VM
06-25 12:05:24.766: W/dalvikvm(778): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-25 12:05:24.786: E/AndroidRuntime(778): FATAL EXCEPTION: main
06-25 12:05:24.786: E/AndroidRuntime(778): android.content.ActivityNotFoundException: Unable to find explicit activity class    {com.example.navdraw/com.example.navdraw.dev}; have you declared this activity in your AndroidManifest.xml?
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.app.Activity.startActivityForResult(Activity.java:3370)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.app.Activity.startActivityForResult(Activity.java:3331)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.app.Activity.startActivity(Activity.java:3566)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.app.Activity.startActivity(Activity.java:3534)
06-25 12:05:24.786: E/AndroidRuntime(778):  at com.example.navdraw.MainActivity$DrawerItemClickListener.onItemClick (MainActivity.java:129)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.widget.AdapterView.performItemClick(AdapterView.java:298)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.widget.AbsListView$1.run(AbsListView.java:3423)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.os.Handler.handleCallback(Handler.java:725)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.os.Looper.loop(Looper.java:137)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-25 12:05:24.786: E/AndroidRuntime(778):  at java.lang.reflect.Method.invokeNative(Native Method)
06-25 12:05:24.786: E/AndroidRuntime(778):  at java.lang.reflect.Method.invoke(Method.java:511)
06-25 12:05:24.786: E/AndroidRuntime(778):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-25 12:05:24.786: E/AndroidRuntime(778):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-25 12:05:24.786: E/AndroidRuntime(778):  at dalvik.system.NativeStart.main(Native Method)

如果有什么显而易见的事我明白了,请告诉我,因为我对应用开发社区很新。

任何帮助都会得到极大的赞赏。

谢谢!

1 个答案:

答案 0 :(得分:2)

好吧,logcat说你还没有在Manifest文件中声明你的活动(你试图启动的活动)。必须在AndroidManifest.xml文件

中声明Android应用中的所有活动

您需要做的是将以下部分添加到您的清单(application标记内的某处):

<application ...>

    <activity
        android:name="com.example.navdraw.dev"
        android:label="label"/>

</application>

有关您可以找到的清单文件结构的更多信息here