在我的应用程序中,我想自定义我的操作栏但是当我尝试做同样的Java NullpointerException被我的应用程序抛出。在我的应用程序中我也使用导航抽屉,我必须在主要活动中添加片段,所以在某些我的片段中我不想显示操作栏但是当我调用actionbar.hide()时它会抛出JNE。这是代码:
public class MainActivity extends AppCompatActivity {
private static String TAG = MainActivity.class.getSimpleName();
Boolean flag = false;
ListView mDrawerList;
RelativeLayout mDrawerPane;
private DrawerLayout mDrawerLayout;
public static FragmentManager fragmentManager;
ArrayList<NavItem> mNavItems = new ArrayList<NavItem>();
static GoogleMap mMap;
FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
if (findViewById(R.id.mainContent) != null) {
if (savedInstanceState != null) {
return;
}
fragmentManager = getFragmentManager();
HomeFragment homefragment = new HomeFragment();
fragmentManager.beginTransaction().add(R.id.mainContent, homefragment).commit();
flag = true;
}
mNavItems.add(new NavItem("Profile",R.drawable.ace));
mNavItems.add(new NavItem("Book A Truck",R.drawable.ace));
mNavItems.add(new NavItem("My Bookings",R.drawable.ace));
mNavItems.add(new NavItem("Farecards",R.drawable.ace));
mNavItems.add(new NavItem("Call Support",R.drawable.ace));
mNavItems.add(new NavItem("About",R.drawable.ace));
// DrawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
// Populate the Navigtion Drawer with options
mDrawerPane = (RelativeLayout) findViewById(R.id.drawerPane);
mDrawerList = (ListView) findViewById(R.id.navList);
DrawerListAdapter adapter = new DrawerListAdapter(this, mNavItems);
mDrawerList.setAdapter(adapter);
// Drawer Item click listeners
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 4){
String number = "tel:9643173925";
Intent telephonecall = new Intent(Intent.ACTION_CALL,Uri.parse(number));
startActivity(telephonecall);
mDrawerLayout.closeDrawer(mDrawerPane);
} else if (position == 1 && flag) {
mDrawerLayout.closeDrawer(mDrawerPane);
} else {
selectItemFromDrawer(position);
}
}
});
}
private void selectItemFromDrawer(int position) {
Fragment fragment = new HomeFragment();
Class fragmentClass = null;
switch (position) {
case 0:
fragmentClass = ProfileFragment.class;
flag = false;
break;
case 1:
fragmentClass = HomeFragment.class;
flag = true;
break;
case 2:
fragmentClass = MybookingFragment.class;
flag =false;
break;
case 3:
fragmentClass = FarecardFragment.class;
flag = false;
break;
case 5:
fragmentClass = AboutFragment.class;
flag = false;
break;
default:
fragmentClass = HomeFragment.class;
flag = true;
break;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
String backstackname = fragmentClass.getName();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.mainContent, fragment).addToBackStack(null).commit();
mDrawerLayout.closeDrawer(mDrawerPane);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的清单文件代码在这里:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="in.moovo.route.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="in.moovo.route.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.gcm" />
</intent-filter>
</receiver>
<!-- <service -->
<!-- android:name="in.moovo.MyGcmListenerService" -->
<!-- android:exported="false" > -->
<!-- <intent-filter> -->
<!-- <action android:name="com.google.android.c2dm.intent.RECEIVE" /> -->
<!-- </intent-filter> -->
<!-- </service> -->
<!-- <service -->
<!-- android:name="in.moovo.MyInstanceIDListenerService" -->
<!-- android:exported="false"> -->
<!-- <intent-filter> -->
<!-- <action android:name="com.google.android.gms.iid.InstanceID"/> -->
<!-- </intent-filter> -->
<!-- </service> -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".Googlemap"
android:label="@string/title_activity_googlemap" >
<!-- <intent-filter> -->
<!-- <action android:name="android.intent.action.MAIN" /> -->
<!-- <category android:name="android.intent.category.LAUNCHER" /> -->
<!-- </intent-filter> -->
</activity>
<activity
android:name=".Confirm"
android:label="@string/title_activity_confirm" >
</activity>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".Moovo"
android:label="@string/title_activity_moovo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
错误日志给出了这个:
java.lang.RuntimeException: Unable to start activity ComponentInfo{in.moovo.route/in.moovo.route.Moovo}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at in.moovo.route.Moovo.onCreate(Moovo.java:41)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
我的style.xml文件:
resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<!--Action bar style-->
<item name="android:actionBarStyle">@style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">@drawable/actionbartheme</item>
<!-- Support library compatibility -->
<item name="background">@drawable/actionbartheme</item>
<item name="height">50dp</item>
</style>
<style name="MyTheme" parent="@android:style/Theme.Dialog">
<item name="android:alertDialogStyle">@style/CustomAlertDialogStyle</item>
<item name="android:textColorPrimary">#a91c44</item>
</style>
<style name="CustomAlertDialogStyle">
<item name="android:bottomBright">#a91c44</item>
</style>
答案 0 :(得分:0)
您的Activity
(Moovo
)会继承AndroidManifest
中指定的应用主题。您的主题是Theme.AppCompat.Light.DarkActionBar
的父级。此主题会移除原始android.app.ActionBar
,这就是getActionBar()
返回null
。
你应该做以下两件事之一:
让Activity
班Moovo
延长AppCompatActivity
并使用getSupportActionBar()
代替getActionBar()
。
您的更改将如下所示:
...
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
...
public class Moovo extends AppCompatActivity {
...
@Override
public void onCreate(Bundle savedInstanceState) {
...
ActionBar actionBar = getSupportActionBar();
...
不要使用基于AppCompat的主题。所有AppCompat主题都会删除原始ActionBar
,并将其替换为android.support.v7.app.ActionBar
以便向后兼容。