NavigationDrawer代码无法实例化片段

时间:2013-09-07 21:54:33

标签: java android android-fragments navigation-drawer

我从导航抽屉教程中获取了确切的代码,但是当我将代码放入我自己的项目中时,它会在运行时立即崩溃。但是当我从我下载的示例项目中运行它时,它完全正常:http://manishkpr.webheavens.com/android-navigation-drawer-example-using-fragments/

可能是构建路径,我该如何解决? 任何建议将不胜感激!

logcat的

09-07 17:46:34.102: D/ActivityThread(13870): setTargetHeapUtilization:0.25
09-07 17:46:34.102: D/ActivityThread(13870): setTargetHeapIdealFree:8388608
09-07 17:46:34.102: D/ActivityThread(13870): setTargetHeapConcurrentStart:2097152
09-07 17:46:34.212: D/AbsListView(13870): Get MotionRecognitionManager
09-07 17:46:34.222: W/dalvikvm(13870): threadid=1: thread exiting with uncaught         exception (group=0x41221438)
09-07 17:46:34.222: E/AndroidRuntime(13870): FATAL EXCEPTION: main
09-07 17:46:34.222: E/AndroidRuntime(13870): java.lang.RuntimeException: Unable to     start activity ComponentInfo{com.example.testnavdrawer/com.example.testnavdrawer.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.navigationdrawer.FragmentOne: make sure class name exists, is public, and has an empty constructor that is public
09-07 17:46:34.222: E/AndroidRuntime(13870):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at android.app.ActivityThread.access$700(ActivityThread.java:143)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at     android.os.Looper.loop(Looper.java:137)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at android.app.ActivityThread.main(ActivityThread.java:4961)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at java.lang.reflect.Method.invokeNative(Native Method)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at java.lang.reflect.Method.invoke(Method.java:511)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at dalvik.system.NativeStart.main(Native Method)
09-07 17:46:34.222: E/AndroidRuntime(13870): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.navigationdrawer.FragmentOne: make sure class name exists, is public, and has an empty constructor that is public
09-07 17:46:34.222: E/AndroidRuntime(13870):    at android.support.v4.app.Fragment.instantiate(Fragment.java:409)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at com.example.testnavdrawer.MainActivity.onCreate(MainActivity.java:46)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at android.app.Activity.performCreate(Activity.java:5179)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
09-07 17:46:34.222: E/AndroidRuntime(13870):    ... 11 more
09-07 17:46:34.222: E/AndroidRuntime(13870): Caused by: java.lang.ClassNotFoundException: com.example.navigationdrawer.FragmentOne
09-07 17:46:34.222: E/AndroidRuntime(13870):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
09-07 17:46:34.222: E/AndroidRuntime(13870):    at android.support.v4.app.Fragment.instantiate(Fragment.java:399)
09-07 17:46:34.222: E/AndroidRuntime(13870):    ... 16 more

MainActivity.java

package com.example.testnavdrawer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends FragmentActivity {
    final String[] data ={"one","two","three"};
    final String[] fragments ={
            "com.example.navigationdrawer.FragmentOne",
            "com.example.navigationdrawer.FragmentTwo",
            "com.example.navigationdrawer.FragmentThree"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);

         final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
         final ListView navList = (ListView) findViewById(R.id.drawer);
         navList.setAdapter(adapter);
         navList.setOnItemClickListener(new OnItemClickListener(){
                 @Override
                 public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
                         drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                                 @Override
                                 public void onDrawerClosed(View drawerView){
                                         super.onDrawerClosed(drawerView);
                                         FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
                                         tx.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[pos]));
                                         tx.commit();
                                 }
                         });
                         drawer.closeDrawer(navList);
                 }
         });
         FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
         tx.replace(R.id.main,Fragment.instantiate(MainActivity.this, fragments[0]));
         tx.commit();
    }

}

activity_main.xml中

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <FrameLayout
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <ListView
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#FFF"
        android:choiceMode="singleChoice"/>

</android.support.v4.widget.DrawerLayout>

FragmentOne.java

package com.example.testnavdrawer;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentOne extends Fragment {

    public static final Fragment newInstance(Context context) {
        FragmentOne f = new FragmentOne();

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, null);
        return root;
    }

}

FragmentTwo.java

package com.example.testnavdrawer;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentTwo extends Fragment {

    public static Fragment newInstance(Context context) {
        FragmentTwo f = new FragmentTwo();

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_two, null);
        return root;
    }

}

FragmentThree.java

package com.example.testnavdrawer;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentThree extends Fragment {

    public static Fragment newInstance(Context context) {
        FragmentThree f = new FragmentThree();

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_three, null);
        return root;
    }

}

1 个答案:

答案 0 :(得分:0)

代码中的错字。主要活动是:

final String[] fragments ={
        "com.example.navigationdrawer.FragmentOne",
        "com.example.navigationdrawer.FragmentTwo",
        "com.example.navigationdrawer.FragmentThree"};

但应该是这样的:

final String[] fragments ={
        "com.example.testnavdrawer.FragmentOne",
        "com.example.testnavdrawer.FragmentTwo",
        "com.example.testnavdrawer.FragmentThree"};

如果你这样做,你会添加一些类型的安全性:

final String[] fragments ={
        FragmentOne.class.getName(),
        FragmentTwo.class.getName(),
        FragmentThree.class.getName()};