这仅用于创建实验室布局。当我运行此停止运行并在模拟器屏幕上显示一个对话框时,应用程序意外停止。我无法通过阅读代码找到代码中的错误。我在清单中注册了所有活动。有人可以告诉我这段代码中的错误。
这是我的主要活动类 -
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;
}
}
答案 0 :(得分:0)
在您的清单文件中添加以下代码:
<activity android:name="com.example.tabwithswipe.MainActivity>
</activity>