需要帮助我的碎片

时间:2013-09-30 14:43:23

标签: java android android-fragments android-fragmentactivity

我一直在使用片段标签,我还没弄清楚我的代码有什么问题。因为它没有错误但是当我在模拟器上运行它总是崩溃请帮助我tnx

我的主要活动代码:

 public class MainActivity extends SherlockFragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar actionbar = getSupportActionBar();
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionbar.setTitle("Dancing Fountain Master Control");

    ActionBar.Tab Frag1Tab = actionbar.newTab().setText("Song List");
    ActionBar.Tab Frag2Tab = actionbar.newTab().setText("About Us");

    Fragment Fragment1 = new Fragment_1();
    Fragment Fragment2 = new Fragment_2();

    Frag1Tab.setTabListener(new MyTabListener(Fragment1));
    Frag2Tab.setTabListener(new MyTabListener(Fragment2));

    actionbar.addTab(Frag1Tab);
    actionbar.addTab(Frag2Tab);
}

class MyTabListener implements ActionBar.TabListener {
    public Fragment fragment;

    public MyTabListener(Fragment fragment){
        this.fragment = fragment;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        ft.replace(R.id.fragment_container, fragment);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

  }

}

activity_main.xml中

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

  </RelativeLayout>

Fragment_1

public class Fragment_1 extends SherlockFragment implements OnClickListener {

    Button button1;
    Button button2;
    Button button3;
    Button button4;
    Button button5;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_1, container, false);

        button1 = (Button) v.findViewById(R.id.bttn1);
        button1.setOnClickListener(this);
        button2 = (Button) v.findViewById(R.id.bttn2);
        button2.setOnClickListener(this);
        button3 = (Button) v.findViewById(R.id.bttn3);
        button3.setOnClickListener(this);
        button4 = (Button) v.findViewById(R.id.bttn4);
        button4.setOnClickListener(this);
        button5 = (Button) v.findViewById(R.id.bttn5);
        button5.setOnClickListener(this);
        return v;

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        int id = v.getId();
        if(id == R.id.bttn1){
            Intent intent = new Intent(v.getContext(), Rock.class);
            startActivityForResult(intent, 0);
        }

        else if(id == R.id.bttn2){
            Intent intent = new Intent(v.getContext(), Punk.class);
            startActivityForResult(intent, 0);
        }
        else if(id == R.id.bttn3){
            Intent intent = new Intent(v.getContext(), Classical.class);
            startActivityForResult(intent, 0);
        }
        else if(id == R.id.bttn4){
            Intent intent = new Intent(v.getContext(), OPM.class);
            startActivityForResult(intent, 0);
        }
        else if(id == R.id.bttn5){
            Intent intent = new Intent(v.getContext(), Pop.class);
            startActivityForResult(intent, 0);
        }

    }

}

和我的Logcat

09-30 22:17:45.680: E/AndroidRuntime(934): FATAL EXCEPTION: main
09-30 22:17:45.680: E/AndroidRuntime(934): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ph.timskie.dancingfountain/ph.timskie.dancingfountain.MainActivity}: java.lang.ClassNotFoundException: ph.timskie.dancingfountain.MainActivity
09-30 22:17:45.680: E/AndroidRuntime(934):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1883)
09-30 22:17:45.680: E/AndroidRuntime(934):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1984)
09-30 22:17:45.680: E/AndroidRuntime(934):  at android.app.ActivityThread.access$600(ActivityThread.java:126)
09-30 22:17:45.680: E/AndroidRuntime(934):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1150)
09-30 22:17:45.680: E/AndroidRuntime(934):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 22:17:45.680: E/AndroidRuntime(934):  at android.os.Looper.loop(Looper.java:137)
09-30 22:17:45.680: E/AndroidRuntime(934):  at android.app.ActivityThread.main(ActivityThread.java:4456)
09-30 22:17:45.680: E/AndroidRuntime(934):  at java.lang.reflect.Method.invokeNative(Native Method)
09-30 22:17:45.680: E/AndroidRuntime(934):  at java.lang.reflect.Method.invoke(Method.java:511)
09-30 22:17:45.680: E/AndroidRuntime(934):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
09-30 22:17:45.680: E/AndroidRuntime(934):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
09-30 22:17:45.680: E/AndroidRuntime(934):  at dalvik.system.NativeStart.main(Native Method)
09-30 22:17:45.680: E/AndroidRuntime(934): Caused by: java.lang.ClassNotFoundException: ph.timskie.dancingfountain.MainActivity
09-30 22:17:45.680: E/AndroidRuntime(934):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
09-30 22:17:45.680: E/AndroidRuntime(934):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
09-30 22:17:45.680: E/AndroidRuntime(934):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
09-30 22:17:45.680: E/AndroidRuntime(934):  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
09-30 22:17:45.680: E/AndroidRuntime(934):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1874)
09-30 22:17:45.680: E/AndroidRuntime(934):  ... 11 more

Androidmanifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ph.timskie.dancingfountain"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="ph.timskie.dancingfountain.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=".Rock"></activity>
        <activity android:name=".Punk"></activity>
        <activity android:name=".Classical"></activity>
        <activity android:name=".OPM"></activity>
        <activity android:name=".Pop"></activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

您需要将MainActivity添加到Android清单文件中。

        <activity
        android:name="ph.timskie.dancingfountain.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>

编辑:

以下是使用ActionBarSherlock的应用程序中的AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Sherlock.Light" >
    <activity
        android:name=".ERGProActivity"
        android:screenOrientation="portrait"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>       
    </activity>
</application>

确保您使用的是ActionBarSherlock主题。