此代码用于制作标签:
import android.app.ActionBar;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.media.Image;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.widget.FrameLayout;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionBar;
ViewPager viewPager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager= (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
actionBar=getActionBar();//Here is error Null Pointer
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(true);
ActionBar.Tab Tab1=actionBar.newTab();
Tab1.setText("Text");
Tab1.setTabListener(this);
ActionBar.Tab Tab2=actionBar.newTab();
Tab2.setText("Image");
Tab2.setTabListener(this);
ActionBar.Tab Tab3=actionBar.newTab();
Tab3.setText("About Us");
Tab3.setTabListener(this);
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
actionBar.addTab(Tab3);
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
class MyAdapter extends FragmentPagerAdapter
{
public MyAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
if(position==0)
{
fragment=new FragmentAFragment();
}
if(position==1)
{
fragment=new FragmentBFragment();
}
if(position==2)
{
fragment=new FragmentCFragment();
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
}
我还使用了ActionBarActivity
和getSupportActivity
,但还有另一个不兼容错误。
这是 LogCat 输出:
09-12 22:23:10.286 3822-3822/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
09-12 22:23:10.325 3822-3829/? I/art﹕ Debugger is no longer active
09-12 22:23:10.470 3822-3822/? D/AndroidRuntime﹕ Shutting down VM
09-12 22:23:10.471 3822-3822/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.santosh.encode, PID: 3822
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.santosh.encode/com.example.santosh.encode.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference
at com.example.santosh.encode.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.Zyg
oteInit.main(ZygoteInit.java:698)
09-12 22:23:11.142 3822-3834/? I/art﹕ Background partial concurrent mark sweep GC freed 369(79KB) AllocSpace objects, 0(0B) LOS objects, 53% free, 442KB/954KB, paused 8.882ms total 35.959ms
这是我的android manifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.santosh.encode" >
<application
android:allowBackup="true"
android:icon="@mipmap/app"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".FragmentA"
android:label="@string/title_activity_fragment" >
</activity>
<activity
android:name=".FragmentB"
android:label="@string/title_activity_fragment_b" >
</activity>
<activity
android:name=".FragmentC"
android:label="@string/title_activity_fragment_c" >
</activity>
</application>
</manifest>
现在有些编辑已完成,但仍面临问题。
package com.example.santosh.encode;
import com.example.santosh.encode.Base64;
import android.app.ActionBar;
import android.app.ActionBar;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.media.Image;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Window;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionBar;
ViewPager viewPager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
actionBar=getActionBar();
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
viewPager= (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
actionBar.setDisplayShowTitleEnabled(true);
ActionBar.Tab Tab1=actionBar.newTab();
Tab1.setText("Text");
Tab1.setTabListener(this);
ActionBar.Tab Tab2=actionBar.newTab();
Tab2.setText("Image");
Tab2.setTabListener(this);
ActionBar.Tab Tab3=actionBar.newTab();
Tab3.setText("About Us");
Tab3.setTabListener(this);
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
actionBar.addTab(Tab3);
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
class MyAdapter extends FragmentPagerAdapter
{
public MyAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
if(position==0)
{
fragment=new FragmentAFragment();
}
if(position==1)
{
fragment=new FragmentBFragment();
}
if(position==2)
{
fragment=new FragmentCFragment();
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
}
现在LogCat显示.....................
09-13 01:06:13.967 5526-5526/com.example.santosh.encode I/art﹕ Not late-enabling -Xcheck:jni (already on)
09-13 01:06:14.086 5526-5526/com.example.santosh.encode D/AndroidRuntime﹕ Shutting down VM
09-13 01:06:14.087 5526-5526/com.example.santosh.encode E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.santosh.encode, PID: 5526
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.santosh.encode/com.example.santosh.encode.MainActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:302)
at com.example.santosh.encode.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
09-13 01:06:14.112 5526-5533/com.example.santosh.encode I/art﹕ Debugger is no longer active
09-13 01:06:14.124 5526-5533/com.example.santosh.encode W/art﹕ Suspending all threads took: 12.092ms
答案 0 :(得分:0)
我认为这是问题所在.. 如果你使用SUPPORTLIBRARY►android.support.v7.app.ActionBar
你必须得到ActionBar的支持。
所以,改变它
actionBar=getActionBar();
到
actionBar = getSupportActionBar();
答案 1 :(得分:0)
将导入更改为:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
并且做:
actionBar = getSupportActionBar();