我的问题出在addTab()方法中,但我不知道如何解决它。
这是错误日志:
10-12 02:19:43.750: E/AndroidRuntime(22899): FATAL EXCEPTION: main
10-12 02:19:43.750: E/AndroidRuntime(22899): java.lang.RuntimeException: Unable to start activity ComponentInfo{yab.example.viewpager1/yab.example.viewpager1.MainActivity}: java.lang.IllegalStateException: Action Bar Tab must have a Callback
10-12 02:19:43.750: E/AndroidRuntime(22899): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121) 10-12 02:19:43.750: E/AndroidRuntime(22899): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
10-12 02:19:43.750: E/AndroidRuntime(22899): at android.app.ActivityThread.access$700(ActivityThread.java:140) 10-12 02:19:43.750: E/AndroidRuntime(22899): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
10-12 02:19:43.750: E/AndroidRuntime(22899): at android.os.Handler.dispatchMessage(Handler.java:99) 10-12 02:19:43.750: E/AndroidRuntime(22899): at android.os.Looper.loop(Looper.java:137)
10-12 02:19:43.750: E/AndroidRuntime(22899): at android.app.ActivityThread.main(ActivityThread.java:4944)
10-12 02:19:43.750: E/AndroidRuntime(22899): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 02:19:43.750: E/AndroidRuntime(22899): at java.lang.reflect.Method.invoke(Method.java:511)
10-12 02:19:43.750: E/AndroidRuntime(22899): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
10-12 02:19:43.750: E/AndroidRuntime(22899): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
10-12 02:19:43.750: E/AndroidRuntime(22899): at dalvik.system.NativeStart.main(Native Method)
10-12 02:19:43.750: E/AndroidRuntime(22899): Caused by: java.lang.IllegalStateException: Action Bar Tab must have a Callback
10-12 02:19:43.750: E/AndroidRuntime(22899): at com.android.internal.app.ActionBarImpl.configureTab(ActionBarImpl.java:488)
10-12 02:19:43.750: E/AndroidRuntime(22899): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:514)
10-12 02:19:43.750: E/AndroidRuntime(22899): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:502)
10-12 02:19:43.750: E/AndroidRuntime(22899): at yab.example.viewpager1.MainActivity.onCreate(MainActivity.java:39)
10-12 02:19:43.750: E/AndroidRuntime(22899): at android.app.Activity.performCreate(Activity.java:5188)
10-12 02:19:43.750: E/AndroidRuntime(22899): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
10-12 02:19:43.750: E/AndroidRuntime(22899): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2085)
10-12 02:19:43.750: E/AndroidRuntime(22899): ... 11 more
这是我的代码:
package com.example.viewpager3;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {
private ViewPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new
ViewPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mViewPager.setAdapter(mSectionsPagerAdapter);
actionBar.addTab(actionBar.newTab().setText(R.string.hello_world));
// for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Toast.makeText(this, "hello "+i, Toast.LENGTH_SHORT).show();
// //actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)));
// }
// mViewPager.setOnPageChangeListener(new
// ViewPager.SimpleOnPageChangeListener() {
// @Override
// public void onPageSelected(int position) {
// Log.i("test de problème", "problème");
// actionBar.setSelectedNavigationItem(position);
// Log.i("test de problème", "no problème");
//
// }
// });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
您尚未设置侦听器。
使用此:
ActionBar.Tab tab = actionBar.newTab();
tab.setText(R.string.hello_world);
tab.setTabListener(this)
actionBar.addTab(tab);