我直到现在才遇到过这个问题,因为我刚刚将我的项目更新为API21(来自API20),我在行NullPointerException
上得到#62 #63 #64 #65 #66 #67
。你可以帮帮我吗?
这是我的build.gradle
:
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "active_news.activenews"
minSdkVersion 10
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.squareup.picasso:picasso:2.1.1'
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
compile 'com.afollestad:material-dialogs:0.5.1'
MainActivity
:
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener, android.app.ActionBar.TabListener {
CollectionPagerAdapter mCollectionPagerAdapter;
ViewPager mViewPager;
SharedPreferences mPrefs;
final String welcomeScreenShownPref = "welcomeScreenShown";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(true);
setProgressBarIndeterminateVisibility(false);
setContentView(R.layout.activity_main);
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);
if (!welcomeScreenShown) {
MaterialDialogCompat.Builder dialogBuilder = new MaterialDialogCompat.Builder(this);
dialogBuilder.setMessage(getString(R.string.Dialog_message));
dialogBuilder.setTitle(getString(R.string.Dialog_title));
dialogBuilder.setNegativeButton(getString(R.string.start), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialogBuilder.create().show();
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(welcomeScreenShownPref, true);
editor.commit(); // Very important to save the preference
}
mCollectionPagerAdapter = new CollectionPagerAdapter(
getSupportFragmentManager());
final android.app.ActionBar actionBar = getActionBar();
assert actionBar != null;
actionBar.setDisplayShowTitleEnabled(true); //line#62
actionBar.setDisplayUseLogoEnabled(false); //line#63
actionBar.setHomeButtonEnabled(false); //line#64
actionBar.setIcon(R.drawable.ic_action_icon3); //line#65
actionBar.setTitle(R.string.app_name2); //line#66
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); //line#67
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCollectionPagerAdapter);
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(mViewPager);
tabs.setTextColor(getResources().getColor(android.R.color.white));
tabs.setTextSize((int) getResources().getDimension(R.dimen.tab_text));
}
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabSelected(android.app.ActionBar.Tab tab, android.app.FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.app.ActionBar.Tab tab, android.app.FragmentTransaction ft) {
}
@Override
public void onTabReselected(android.app.ActionBar.Tab tab, android.app.FragmentTransaction ft) {
}
public class CollectionPagerAdapter extends FragmentPagerAdapter {
//final int NUM_ITEMS = 11; // number of tabs
public CollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position)
{
switch (position) {
case 0:
return new TAB1();
case 1:
return new TAB2();
case 2:
return new TAB3();
case 3:
return new TAB4();
case 4:
return new TAB5();
case 5:
return new TAB6();
case 6:
return new TAB7();
case 7:
return new TAB8();
}
return null;
}
@Override
public int getCount()
{
return 8;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position)
{
case 0:
return getString(R.string.label1);
case 1:
return getString(R.string.label2);
case 2:
return getString(R.string.label3);
case 3:
return getString(R.string.label4);
case 4:
return getString(R.string.label5);
case 5:
return getString(R.string.label6);
case 6:
return getString(R.string.label8);
case 7:
return getString(R.string.label10);
//case 8:
// return getString(R.string.label9);
//case 9:
// return getString(R.string.label7);
}
return null;
}
}
@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 item selection
int i = item.getItemId();
if (i == R.id.actiorn) {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
MainActivity.this.startActivity(intent);
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
这是我的logcat(它在第62行显示NullPointerException):
12-30 01:00:24.249 25790-25790/active_news.activenews E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{active_news.activenews/active_news.activenews.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2304)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)
at android.app.ActivityThread.access$600(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5191)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at active_news.activenews.MainActivity.onCreate(MainActivity.java:62)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)
at android.app.ActivityThread.access$600(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5191)
at java.lang.reflect.Method.invokeNative(Native Method)
答案 0 :(得分:0)
你正在使用AppCompat库,所以我想你必须调用getSupportActionBar()而不是getActionBar()
编辑:正如我之前所说,如果您将min SDK更新为11,那么您不需要使用getSupportActionBar。如果getActionBar方法中仍然存在错误,那么请确保您没有使用主题android:theme =" @ style / Theme.NoTitle"在您的应用程序清单上您需要在主题中定义一个带有标题的操作栏,例如android:theme =" @android:style / Theme.WithActionBar"。或者使用getWindow()。requestFeature(Window.FEATURE_ACTION_BAR);在setContentView之后。有许多解决方案:getActionBar returns null