运行fragment和tablayout应用程序时,应用程序意外停止

时间:2014-07-22 18:20:09

标签: android

我是Android的初学者,在运行我的APP时只包含标签和片段。帮我解决这个错误The application has stopped unexpectedly. Please try again. Error 我有三个Fragment类和各自的xml文件。我正在使用ApI9。

我从youtube教程获得了这段代码。编译时没有错误。 我已经尝试了两种不同的方法来解决问题:

  1. 清洁项目。
  2. 重新启动。
  3. 我的MainActivity.java

    package com.example.slidenerdtab;
    
    import android.annotation.SuppressLint;
    import android.annotation.TargetApi;
    import android.app.ActionBar;
    import android.app.ActionBar.Tab;
    import android.app.ActionBar.TabListener;
    import android.app.FragmentTransaction;
    import android.os.Build;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.util.Log;
    
    
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)@SuppressLint("NewApi") public class MainActivity extends FragmentActivity implements TabListener {
        ActionBar actionBar;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            actionBar = getActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            ActionBar.Tab tab1 = actionBar.newTab();
            tab1.setText("TAB 1");
            tab1.setTabListener(this);
    
            ActionBar.Tab tab2 = actionBar.newTab();
            tab2.setText("TAB 2");
            tab2.setTabListener(this);
    
            ActionBar.Tab tab3 = actionBar.newTab();
            tab3.setText("TAB 3");
            tab3.setTabListener(this);
            actionBar.addTab(tab1);
            actionBar.addTab(tab2);
            actionBar.addTab(tab3);
    
        }
    
        @SuppressLint("NewApi")@Override
        public void onTabReselected(Tab tab, FragmentTransaction arg1) {
            Log.d("hai", "ontabReselected at" + "position" + tab.getPosition() + "name" + tab.getText());
    
        }
    
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)@Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            Log.d("hai", "ontabReselected at" + "position" + tab.getPosition() + "name" + tab.getText());
        }
    
        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            Log.d("hai", "ontabReselected at" + "position" + tab.getPosition() + "name" + tab.getText());
    
        }
    
    }
    

    的Manifest.xml

       <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.slidenerdtab"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="16" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.slidenerdtab.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name="com.example.slidenerdtab.FragmentActivity"
                android:label="@string/title_activity_fragment" >
            </activity>
            <activity
                android:name="com.example.slidenerdtab.FragmentB"
                android:label="@string/title_activity_fragment_b" >
            </activity>
            <activity
                android:name="com.example.slidenerdtab.FragmentC"
                android:label="@string/title_activity_fragment_c" >
            </activity>
        </application>
    
    </manifest>
    

    我的LOGCAT

    07-22 23:23:02.029: E/AndroidRuntime(276): FATAL EXCEPTION: main
    07-22 23:23:02.029: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.slidenerdtab/com.example.slidenerdtab.MainActivity}: java.lang.ClassNotFoundException: com.example.slidenerdtab.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.slidenerdtab-1.apk]
    07-22 23:23:02.029: E/AndroidRuntime(276):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
    07-22 23:23:02.029: E/AndroidRuntime(276):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    07-22 23:23:02.029: E/AndroidRuntime(276):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    07-22 23:23:02.029: E/AndroidRuntime(276):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    07-22 23:23:02.029: E/AndroidRuntime(276):  at android.os.Handler.dispatchMessage(Handler.java:99)
    07-22 23:23:02.029: E/AndroidRuntime(276):  at android.os.Looper.loop(Looper.java:123)
    07-22 23:23:02.029: E/AndroidRuntime(276):  at android.app.ActivityThread.main(ActivityThread.java:4627)
    07-22 23:23:02.029: E/AndroidRuntime(276):  at java.lang.reflect.Method.invokeNative(Native Method)
    07-22 23:23:02.029: E/AndroidRuntime(276):  at java.lang.reflect.Method.invoke(Method.java:521)
    07-22 23:23:02.029: E/AndroidRuntime(276):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    07-22 23:23:02.029: E/AndroidRuntime(276):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    07-22 23:23:02.029: E/AndroidRuntime(276):  at dalvik.system.NativeStart.main(Native Method)
    07-22 23:23:02.029: E/AndroidRuntime(276): Caused by: java.lang.ClassNotFoundException: com.example.slidenerdtab.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.slidenerdtab-1.apk]
    07-22 23:23:02.029: E/AndroidRuntime(276):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
    07-22 23:23:02.029: E/AndroidRuntime(276):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
    07-22 23:23:02.029: E/AndroidRuntime(276):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
    07-22 23:23:02.029: E/AndroidRuntime(276):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    07-22 23:23:02.029: E/AndroidRuntime(276):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
    07-22 23:23:02.029: E/AndroidRuntime(276):  ... 11 more
    07-22 23:23:02.050: W/ActivityManager(58):   Force finishing activity com.example.slidenerdtab/.MainActivity
    07-22 23:23:02.560: W/ActivityManager(58): Activity pause timeout for HistoryRecord{43e35c50 com.example.slidenerdtab/.MainActivity}
    07-22 23:23:02.580: I/ActivityManager(58): Displayed activity com.android.launcher/com.android.launcher2.Launcher: 25317 ms (total 25317 ms)
    07-22 23:23:12.662: W/ActivityManager(58): Activity destroy timeout for HistoryRecord{43e35c50 com.example.slidenerdtab/.MainActivity}
    07-22 23:23:12.700: D/KeyguardViewMediator(58): pokeWakelock(5000)
    07-22 23:23:13.029: D/KeyguardViewMediator(58): pokeWakelock(5000)
    07-22 23:23:13.429: W/WindowManager(58): No window to dispatch pointer action 1
    07-22 23:23:13.839: I/ARMAssembler(58): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x31a9d8:0x31aa94] in 605217 ns
    07-22 23:23:16.359: I/Process(276): Sending signal. PID: 276 SIG: 9
    07-22 23:23:16.379: I/ActivityManager(58): Process com.example.slidenerdtab (pid 276) has died.
    07-22 23:23:16.609: I/ARMAssembler(58): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x31b0d8:0x31b2a0] in 1528452 ns
    07-22 23:27:50.799: D/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol
    07-22 23:32:50.842: D/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol
    

1 个答案:

答案 0 :(得分:1)

转到Project属性。在android dependencies标签下的android private libraries类别中选择java build pathorder and export。然后清理项目一次并运行。