這只是用於創建實驗室佈局。當我運行這個停止運行並在模擬器屏幕上顯示一個對話框時,應用程序意外停止。通過閱讀代碼我無法在代碼中發現錯誤。我將所有活動註冊爲清單。有人可以告訴我這段代碼有什麼錯誤。
應用程序爲何意外停止?
在這裏,這是我的主要活動接收機類
package com.example.tabwithswipe;
import android.support.v4.app.Fragment;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
這裏是我的logcat:
03-12 14:37:48.851: I/dalvikvm(494): Failed resolving Lcom/example/tabwithswipe/MainActivity; interface 8 'Landroid/app/ActionBar$TabListener;'
03-12 14:37:48.902: W/dalvikvm(494): Link of class 'Lcom/example/tabwithswipe/MainActivity;' failed
03-12 14:37:48.902: D/AndroidRuntime(494): Shutting down VM
03-12 14:37:48.931: W/dalvikvm(494): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-12 14:37:48.992: E/AndroidRuntime(494): FATAL EXCEPTION: main
03-12 14:37:48.992: E/AndroidRuntime(494): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tabwithswipe/com.example.tabwithswipe.MainActivity}: java.lang.ClassNotFoundException: com.example.tabwithswipe.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.tabwithswipe-1.apk]
03-12 14:37:48.992: E/AndroidRuntime(494): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
03-12 14:37:48.992: E/AndroidRuntime(494): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-12 14:37:48.992: E/AndroidRuntime(494): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-12 14:37:48.992: E/AndroidRuntime(494): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-12 14:37:48.992: E/AndroidRuntime(494): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 14:37:48.992: E/AndroidRuntime(494): at android.os.Looper.loop(Looper.java:123)
03-12 14:37:48.992: E/AndroidRuntime(494): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-12 14:37:48.992: E/AndroidRuntime(494): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 14:37:48.992: E/AndroidRuntime(494): at java.lang.reflect.Method.invoke(Method.java:507)
03-12 14:37:48.992: E/AndroidRuntime(494): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-12 14:37:48.992: E/AndroidRuntime(494): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-12 14:37:48.992: E/AndroidRuntime(494): at dalvik.system.NativeStart.main(Native Method)
03-12 14:37:48.992: E/AndroidRuntime(494): Caused by: java.lang.ClassNotFoundException: com.example.tabwithswipe.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.tabwithswipe-1.apk]
03-12 14:37:48.992: E/AndroidRuntime(494): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
03-12 14:37:48.992: E/AndroidRuntime(494): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
03-12 14:37:48.992: E/AndroidRuntime(494): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
03-12 14:37:48.992: E/AndroidRuntime(494): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-12 14:37:48.992: E/AndroidRuntime(494): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
03-12 14:37:48.992: E/AndroidRuntime(494): ... 11 more
03-12 14:38:08.451: I/Process(494): Sending signal. PID: 494 SIG: 9`
這裏是TabPagerAdapter類:
package com.example.tabwithswipe;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int index) {
// TODO Auto-generated method stub
switch (index) {
case 0:
return new TopRatedFragment();
case 1:
return new GamesFragment();
case 2:
return new MoviesFragment();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
好像你已經忘了添加在清單 – waqaslam
退房http://stackoverflow.com/a/16596990/2001247您的活動,這是你的答案。 – ElDuderino