1

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

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

日志猫

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;
    }

}

片段二.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;
    }

}

片段三.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;
    }

}
4

1 回答 1

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()};
于 2013-09-07T22:01:00.100 回答