创建选项卡时出错
Error creating tabs
Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference
是什么意思?我尝试了很多解决方案,但没有人能解决我的问题。
Tab.java
package com.example.project.project;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class Tab extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position); }
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Tab selected", 2000).show();
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}};
//Add New Tabs
actionBar.addTab(actionBar.newTab().setText("Information").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Work Force").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Work Details").setTabListener(tabListener));
}
}
TabPagerAdapter.java
package com.example.project.project;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new Information();
case 1:
return new WorkDetailsTable();
}
return null ;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3; //No of Tabs you can give your number of tabs
}
}
Tab1.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.project.project" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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=".Tab">
</activity>
<activity android:name=".WorkDetailsTable" android:screenOrientation="landscape" />
<activity android:name="Information"/>
</application>
</manifest>
错误
10-04 11:11:07.492 2146-2146/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.project, PID: 2146
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project.project/com.example.project.project.Tab}: 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:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
而在我的 Mainfest.xml 中,编译器说
<activity android:name=".WorkDetailsTable" android:screenOrientation="landscape" />
<activity android:name="Information"/>
不能分配给 android.app.....
尝试替换此行:
actionBar = getActionBar();
有:
actionBar = ((AppCompatActivity) mActivity).getSupportActionBar();
并在 onCreate() 之上添加这个
Activity mActivity = (Activity) this;
然后 activity 开始:
<activity
android:name=".YourActivityClassName"
android:label="YourActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="package.name.YOURACTIVITYCLASSNAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
这是听众:
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@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) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Tab selected", 2000).show();
}
}
参考这个:FragmentActivity vs Activity
Also remember to use Activity if you are using android.app.Fragment; use FragmentActivity if you are using android.support.v4.app.Fragment. Never attach a android.support.v4.app.Fragment to an android.app.Activity, as this will cause an exception to be thrown.
尝试替换
actionBar = getActionBar();
与
actionBar = ((AppCompatActivity) activity).getSupportActionBar();
哪里
Activity activity = (Activity) this;
Actionbar has to be replaced with toolbar now, all you need to do is include a toolbar in your xml files. I used the appcompact library toolbar, for this you may have to add support library in your gradle file
compile 'com.android.support:support-v4:22.0.0'
compile 'com.android.support:appcompat-v7:22.0.0'
Add follwing toolbar to xml file
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark">
</android.support.v7.widget.Toolbar>
Don't forget to inherit your activity from Appcompactactivity, you may use your own toolbar, finally access the toolbaar in activity
Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference
是什么意思?我尝试了很多解决方案,但没有人能解决我的问题。
Tab.java
package com.example.project.project;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class Tab extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position); }
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Tab selected", 2000).show();
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}};
//Add New Tabs
actionBar.addTab(actionBar.newTab().setText("Information").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Work Force").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Work Details").setTabListener(tabListener));
}
}
TabPagerAdapter.java
package com.example.project.project;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new Information();
case 1:
return new WorkDetailsTable();
}
return null ;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3; //No of Tabs you can give your number of tabs
}
}
Tab1.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.project.project" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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=".Tab">
</activity>
<activity android:name=".WorkDetailsTable" android:screenOrientation="landscape" />
<activity android:name="Information"/>
</application>
</manifest>
错误
10-04 11:11:07.492 2146-2146/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.project, PID: 2146
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project.project/com.example.project.project.Tab}: 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:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
而在我的 Mainfest.xml 中,编译器说
<activity android:name=".WorkDetailsTable" android:screenOrientation="landscape" />
<activity android:name="Information"/>
不能分配给 android.app.....
尝试替换此行:
actionBar = getActionBar();
有:
actionBar = ((AppCompatActivity) mActivity).getSupportActionBar();
并在 onCreate() 之上添加这个
Activity mActivity = (Activity) this;
然后 activity 开始:
<activity
android:name=".YourActivityClassName"
android:label="YourActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="package.name.YOURACTIVITYCLASSNAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
这是听众:
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@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) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Tab selected", 2000).show();
}
}
参考这个:FragmentActivity vs Activity
Also remember to use Activity if you are using android.app.Fragment; use FragmentActivity if you are using android.support.v4.app.Fragment. Never attach a android.support.v4.app.Fragment to an android.app.Activity, as this will cause an exception to be thrown.
尝试替换
actionBar = getActionBar();
与
actionBar = ((AppCompatActivity) activity).getSupportActionBar();
哪里
Activity activity = (Activity) this;
Actionbar has to be replaced with toolbar now, all you need to do is include a toolbar in your xml files. I used the appcompact library toolbar, for this you may have to add support library in your gradle file
compile 'com.android.support:support-v4:22.0.0'
compile 'com.android.support:appcompat-v7:22.0.0'
Add follwing toolbar to xml file
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark">
</android.support.v7.widget.Toolbar>
Don't forget to inherit your activity from Appcompactactivity, you may use your own toolbar, finally access the toolbaar in activity
Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolbar);
setSupportActionBar(toolbar);