应用程序正在崩溃为导航抽屉添加新碎片

时间:2015-11-28 09:44:48

标签: android android-fragments android-studio

添加导航栏活动片段时遇到问题。我使用Android Studio中提供的默认导航栏使用片段。最初,$conn = new PDO("mysql:host=$host;dbname=$db_name",$username,$password); $conn->query('SET NAMES UTF8'); $sql = "SELECT * FROM $tbl_name"; $q = $conn->prepare($sql); $q->execute(array($title)); $q->setFetchMode(PDO::FETCH_BOTH); $phpvar = ''; while($data = $q->fetchAll()){ $phpvar .= $data->names; $phpvar .= ','; } echo rtrim($phpvar, ","); 中的代码是(工作正常):

MainActivity.java

但是在修改它以在选择的导航抽屉项目上显示特定片段之后,我添加了以下代码而不是上面的代码&该应用崩溃。 :

@Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                .commit();
    }

其他信息: 我通过File-> New-> Fragment-> Fragment(Blank)创建了2个片段(MyFragment1& MyFragment2)。

没有语法错误

根据需要记录:

    @Override
   public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        Fragment fragment;
        FragmentManager fragmentManager = getFragmentManager();
        switch(position){
            default:
            case 0:
                fragment=new MyFragment1();
                break;
            case 1:
                fragment=new MyFragment2();
                break;


        }
        fragmentManager.beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
    }

11-28 15:12:17.137 26622-26622/com.example.harni_000.navigationdrawer D/dalvikvm﹕ Late-enabling CheckJNI 11-28 15:12:17.177 26622-26622/com.example.harni_000.navigationdrawer D/ActivityThread﹕ handleBindApplication:com.example.harni_000.navigationdrawer 11-28 15:12:17.177 26622-26622/com.example.harni_000.navigationdrawer D/ActivityThread﹕ setTargetHeapUtilization:0.75 11-28 15:12:17.177 26622-26622/com.example.harni_000.navigationdrawer D/ActivityThread﹕ setTargetHeapMinFree:2097152 11-28 15:12:17.197 26622-26622/com.example.harni_000.navigationdrawer W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 11-28 15:12:17.197 26622-26622/com.example.harni_000.navigationdrawer I/dalvikvm﹕ Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested 11-28 15:12:17.197 26622-26622/com.example.harni_000.navigationdrawer W/dalvikvm﹕ VFY: unable to resolve interface method 14530: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 11-28 15:12:17.197 26622-26622/com.example.harni_000.navigationdrawer D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002 11-28 15:12:17.197 26622-26622/com.example.harni_000.navigationdrawer I/dalvikvm﹕ Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode 11-28 15:12:17.197 26622-26622/com.example.harni_000.navigationdrawer W/dalvikvm﹕ VFY: unable to resolve interface method 14534: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 11-28 15:12:17.197 26622-26622/com.example.harni_000.navigationdrawer D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002 11-28 15:12:17.207 26622-26622/com.example.harni_000.navigationdrawer D/dalvikvm﹕ GetMethodID: not returning static method Landroid/os/Process;.getTotalMemory ()J 11-28 15:12:17.207 26622-26622/com.example.harni_000.navigationdrawer D/dalvikvm﹕ GetMethodID: not returning static method Landroid/os/Process;.getFreeMemory ()J 11-28 15:12:17.237 26622-26622/com.example.harni_000.navigationdrawer I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations 11-28 15:12:17.237 26622-26622/com.example.harni_000.navigationdrawer W/dalvikvm﹕ VFY: unable to resolve virtual method 416: Landroid/content/res/TypedArray;.getChangingConfigurations ()I 11-28 15:12:17.237 26622-26622/com.example.harni_000.navigationdrawer D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 11-28 15:12:17.237 26622-26622/com.example.harni_000.navigationdrawer I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType 11-28 15:12:17.237 26622-26622/com.example.harni_000.navigationdrawer W/dalvikvm﹕ VFY: unable to resolve virtual method 438: Landroid/content/res/TypedArray;.getType (I)I 11-28 15:12:17.237 26622-26622/com.example.harni_000.navigationdrawer D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 11-28 15:12:17.297 26622-26622/com.example.harni_000.navigationdrawer D/AndroidRuntime﹕ Shutting down VM 11-28 15:12:17.297 26622-26622/com.example.harni_000.navigationdrawer W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415f5d58) 11-28 15:12:17.297 26622-26622/com.example.harni_000.navigationdrawer E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.harni_000.navigationdrawer, PID: 26622 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.harni_000.navigationdrawer/com.example.harni_000.navigationdrawer.MainActivity}: java.lang.ClassCastException: com.example.harni_000.navigationdrawer.MainActivity@424062c8 must implement OnFragmentInteractionListener at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249) at android.app.ActivityThread.access$800(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5113) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassCastException: com.example.harni_000.navigationdrawer.MainActivity@424062c8 must implement OnFragmentInteractionListener at com.example.harni_000.navigationdrawer.MyFragment1.onAttach(MyFragment1.java:83) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:849) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) at android.app.BackStackRecord.run(BackStackRecord.java:690) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1449) at android.app.Activity.performStart(Activity.java:5257) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)             at android.app.ActivityThread.access$800(ActivityThread.java:141)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:136)             at android.app.ActivityThread.main(ActivityThread.java:5113)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:515)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)             at dalvik.system.NativeStart.main(Native Method) 的代码

MyFragment1.java

0 个答案:

没有答案