package com.example.actionbar;
// creating a simple 3 tabbed action bar
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
public class Main_Fragment extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_home);
ActionBar actionbar=getSupportActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setTitle("Tabbed layout");
ActionBar.Tab Fragtab1=actionbar.newTab().setText("Fragment1");
ActionBar.Tab Fragtab2=actionbar.newTab().setText("Fragment2");
ActionBar.Tab Fragtab3=actionbar.newTab().setText("Fragment3");
Fragment Fragment1 =new Fragment1();
Fragment Fragment2 =new Fragment2();
Fragment Fragment3 =new Fragment3();
Fragtab1.setTabListener(new MyTabListener(Fragment1));
Fragtab1.setTabListener(new MyTabListener(Fragment2));
Fragtab1.setTabListener(new MyTabListener(Fragment3));
actionbar.addTab(Fragtab1);
actionbar.addTab(Fragtab2);
actionbar.addTab(Fragtab3);
}
class MyTabListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabListener(Fragment fragment) {
// TODO Auto-generated constructor stub
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
}
无法弄清楚运行时错误..请帮忙 并且运行此代码会触发强制关闭..
下面的是错误的LOGCAT
:
09-30 10:28:24.329: E/AndroidRuntime(1610): FATAL EXCEPTION: main
09-30 10:28:24.329: E/AndroidRuntime(1610): java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable
09-30 10:28:24.329: E/AndroidRuntime(1610): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:98)
09-30 10:28:24.329: E/AndroidRuntime(1610): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
09-30 10:28:24.329: E/AndroidRuntime(1610): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
09-30 10:28:24.329: E/AndroidRuntime(1610): at com.example.actionbar.Main_Fragment.onCreate(Main_Fragment.java:21)
09-30 10:28:24.329: E/AndroidRuntime(1610): at android.app.Activity.performCreate(Activity.java:5133)
09-30 10:28:24.329: E/AndroidRuntime(1610): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-30 10:28:24.329: E/AndroidRuntime(1610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-30 10:28:24.329: E/AndroidRuntime(1610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-30 10:28:24.329: E/AndroidRuntime(1610): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-30 10:28:24.329: E/AndroidRuntime(1610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-30 10:28:24.329: E/AndroidRuntime(1610): at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 10:28:24.329: E/AndroidRuntime(1610): at android.os.Looper.loop(Looper.java:137)
09-30 10:28:24.329: E/AndroidRuntime(1610): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-30 10:28:24.329: E/AndroidRuntime(1610): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 10:28:24.329: E/AndroidRuntime(1610): at java.lang.reflect.Method.invoke(Method.java:525)
09-30 10:28:24.329: E/AndroidRuntime(1610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-30 10:28:24.329: E/AndroidRuntime(1610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-30 10:28:24.329: E/AndroidRuntime(1610): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
Main_Fragment第21行的代码是什么?
com.example.actionbar.Main_Fragment.onCreate(Main_Fragment.java:21)
另外,改变:
Fragtab1.setTabListener(new MyTabListener(Fragment1));
Fragtab1.setTabListener(new MyTabListener(Fragment2));
Fragtab1.setTabListener(new MyTabListener(Fragment3));
要:
Fragtab1.setTabListener(new MyTabListener(Fragment1));
Fragtab2.setTabListener(new MyTabListener(Fragment2));
Fragtab3.setTabListener(new MyTabListener(Fragment3));