2013-09-30 99 views
0

我一直在使用片段標籤,我還沒弄清楚我的代碼有什麼問題。因爲它沒有錯誤,但是當我在模擬器上運行它,它總是死機,請幫助我TNX需要幫助我的片段

我的MainActivity代碼:

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

    } 

} 

and my 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

您需要將您的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> 

編輯:

下面是一個使用從應用工作AndroidManifest ActionBarSherlock:

<?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> 

確保您使用的是ActionBarSh erlock主題。

+0

它已被添加 – Tim

+0

請確保您的包信息是正確的:ph.timskie.dancingfountain 檢查拼寫錯誤,確保它匹配您的java文件中列出的內容。 – wyoskibum

+0

拼寫沒有問題 – Tim