我正在創建具有可滑動視圖的Android選項卡布局,但出現一些錯誤。我不知道如何解決這些錯誤。具有可滑動視圖出錯的Android選項卡布局
代碼 -
@SuppressLint("NewApi")
public class Types extends FragmentActivity implements ActionBar.TabListener{
ImageView left, right;
Intent objIntent;
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "type1", "type2", "type3", "type4", "type5", "type6", "type7", "type8" };
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.types);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
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) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
protected void onPause() {
super.onPause();
objIntent = new Intent(Types.this, PlayAudio.class);
stopService(objIntent);
}
@Override
public void onBackPressed() {
super.onBackPressed();
objIntent = new Intent(Types.this, PlayAudio.class);
stopService(objIntent);
startActivity(new Intent (Types.this, MainActivity.class));
finish();
}
@Override
public void onResume()
{
super.onResume();
objIntent = new Intent(Types.this, PlayAudio.class);
startService(objIntent);
}
}
Logcat-
01-29 01:45:36.094: E/AndroidRuntime(1191): Caused by: java.lang.NullPointerException
01-29 01:45:36.094: E/AndroidRuntime(1191): at com.info.abc.Types.onCreate(Types.java:38)
01-29 01:45:36.094: E/AndroidRuntime(1191): at android.app.Activity.performCreate(Activity.java:5243)
01-29 01:45:36.094: E/AndroidRuntime(1191): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-29 01:45:36.094: E/AndroidRuntime(1191): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
01-29 01:45:36.094: E/AndroidRuntime(1191): ... 11 more
這是第38行:
actionBar.setHomeButtonEnabled(false);
導入文件 -
import android.support.v7.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
嘗試actionBar.setHomeButtonEnabled(真);並且如果您的活動沒有標題欄,則方法getActionBar()可以返回null。 –
我已經嘗試將它設置爲true,但仍然是相同的錯誤。 –
@JohnR:你在低版本的設備上測試它嗎? –