当我在我的手机中运行我的应用程序时,它有Android版本4.2.2。它会正常工作,但当我再次在其他手机如4.1.2冰淇淋三明治中运行时,只有闪屏才能正常工作。 5秒后,应用程序已停止。
这是我的Logcat错误:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{dtournavigation.android/dtournavigation.android.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
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:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at dtournavigation.android.fragments.MainFragment.isGPSEnabled(MainFragment.java:2277)
at dtournavigation.android.fragments.MainFragment.onPrepareOptionsMenu(MainFragment.java:2164)
at android.support.v4.app.Fragment.performPrepareOptionsMenu(Fragment.java:1574)
at android.support.v4.app.FragmentManagerImpl.dispatchPrepareOptionsMenu(FragmentManager.java:1980)
at android.support.v4.app.FragmentActivity.onPreparePanel(FragmentActivity.java:475)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:471)
at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:817)
at android.app.Activity.invalidateOptionsMenu(Activity.java:2667)
at android.support.v4.app.ActivityCompatHoneycomb.invalidateOptionsMenu(ActivityCompatHoneycomb.java:29)
at android.support.v4.app.FragmentActivity.supportInvalidateOptionsMenu(FragmentActivity.java:633)
at android.support.v4.app.Fragment.setHasOptionsMenu(Fragment.java:781)
at dtournavigation.android.fragments.MainFragment.onCreate(MainFragment.java:325)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1455)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
at android.app.Activity.performStart(Activity.java:5173)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
... 11 more
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dtournavigation.android"
android:versionCode="8"
android:versionName="2.0.1" >
<uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="13"
android:maxSdkVersion="19"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<!-- Android Maps API V2 -->
<permission
android:name="edu.usf.cutr.opentripplanner.android.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="edu.usf.cutr.opentripplanner.android.permission.MAPS_RECEIVE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<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"/>
<!-- The following permission is not required to use
Google Maps Android API v2, but is recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true" />
<application
android:name="dtournavigation.android.OTPApp"
android:icon="@drawable/dtour_logo"
android:label="@string/app_name"
android:allowBackup="true">
<uses-library android:name="com.google.android.maps" />
<activity
android:name="dtournavigation.android.MyActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@style/MyTheme"
android:windowSoftInputMode="stateHidden"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="dtournavigation.android.MainActivity" />
</activity>
<activity android:name="dtournavigation.android.SettingsActivity"
android:theme="@style/MyTheme"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<service android:name="dtournavigation.android.NavigationService" />
<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="AIzaSyAoJe7kIY7m6EGyZ3cPt7JCiLDwh9DZ09w"/>
<activity android:name="dtournavigation.android.passtype.PassType"
android:theme="@style/MyTheme"></activity>
<activity android:name=".GlobalApplication"
android:theme="@style/MyTheme"></activity>
<activity android:name="dtournavigation.android.fragments.DirectionListFragments"
android:theme="@style/MyTheme"
android:screenOrientation="portrait">
</activity>
<activity android:name="dtournavigation.android.SplashScreen"
android:screenOrientation="portrait"
android:theme="@style/NoTitle">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="dtournavigation.android.SMS"
android:theme="@style/MyTheme"
android:screenOrientation="portrait">
</activity>
</application>
</manifest>
这是我的MyActivity.java文件
package dtournavigation.android;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.opentripplanner.v092snapshot.api.model.Itinerary;
import org.opentripplanner.v092snapshot.api.model.Leg;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import com.google.android.gms.maps.model.LatLng;
import dtournavigation.android.R;
import dtournavigation.android.fragments.DirectionListFragment;
import dtournavigation.android.fragments.MainFragment;
import dtournavigation.android.listeners.DateCompleteListener;
import dtournavigation.android.listeners.OnFragmentListener;
import dtournavigation.android.model.OTPBundle;
public class MyActivity extends FragmentActivity implements OnFragmentListener{
private List<Leg> currentItinerary = new ArrayList<Leg>();
private List<Itinerary> currentItineraryList = new ArrayList<Itinerary>();
private int currentItineraryIndex = -1;
private OTPBundle bundle = null;
private MainFragment mainFragment;
private String TAG = "OTP";
private String currentRequestString="";
private boolean isButtonStartLocation = false;
DateCompleteListener dateCompleteCallback;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
setContentView(R.layout.activity);
if (savedInstanceState != null) {
mainFragment = (MainFragment) getSupportFragmentManager().findFragmentByTag(OTPApp.TAG_FRAGMENT_MAIN_FRAGMENT);//recuperar o tag adecuado e pillar ese fragment
}
if(savedInstanceState==null){
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
mainFragment = new MainFragment();
fragmentTransaction.replace(R.id.mainFragment, mainFragment, OTPApp.TAG_FRAGMENT_MAIN_FRAGMENT);
fragmentTransaction.commit();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode) {
case OTPApp.SETTINGS_REQUEST_CODE:
if (resultCode == RESULT_OK) {
boolean shouldRefresh = data.getBooleanExtra(OTPApp.REFRESH_SERVER_RETURN_KEY, false);
boolean changedSelectedCustomServer = data.getBooleanExtra(OTPApp.CHANGED_SELECTED_CUSTOM_SERVER_RETURN_KEY, false);
boolean changedTileProvider = data.getBooleanExtra(OTPApp.CHANGED_MAP_TILE_PROVIDER_RETURN_KEY, false);
// Toast.makeText(this, "Should server list refresh? " + shouldRefresh, Toast.LENGTH_LONG).show();
if(shouldRefresh){
mainFragment.setNeedToRunAutoDetect(true);
mainFragment.setNeedToUpdateServersList(true);
}
if(changedSelectedCustomServer){
mainFragment.updateSelectedServer();
}
if(changedTileProvider){
mainFragment.updateOverlay(null);
}
break;
}
case OTPApp.CHOOSE_CONTACT_REQUEST_CODE:
if(resultCode == RESULT_OK){
// Log.v(TAG, "CHOOSE CONTACT RESULT OK");
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String address = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
mainFragment.setTextBoxLocation(address, isButtonStartLocation);
mainFragment.processAddress(isButtonStartLocation, address, false);
}
break;
}
}
}
@Override
protected void onDestroy() {
mainFragment = null;
Log.d(TAG, "Released mainFragment with map in MyActivity.onDestroy()");
super.onDestroy();
}
@Override
public void onItinerariesLoaded(List<Itinerary> itineraries) {
// TODO Auto-generated method stub
currentItineraryList.clear();
currentItineraryList.addAll(itineraries);
}
@Override
public void onItinerarySelected(int i) {
// TODO Auto-generated method stub
if(i >= currentItineraryList.size()) return;
currentItineraryIndex = i;
currentItinerary.clear();
currentItinerary.addAll(currentItineraryList.get(i).legs);
}
@Override
public List<Leg> getCurrentItinerary() {
// TODO Auto-generated method stub
return currentItinerary;
}
@Override
public void onSwitchedToDirectionFragment() {
// TODO Auto-generated method stub
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
Fragment directionFragment = new DirectionListFragment();
transaction.add(R.id.mainFragment, directionFragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.addToBackStack(null);
transaction.commit();
}
@Override
public OTPBundle getOTPBundle() {
// TODO Auto-generated method stub
return bundle;
}
@Override
public void setOTPBundle(OTPBundle b) {
// TODO Auto-generated method stub
this.bundle = b;
this.bundle.setCurrentItineraryIndex(currentItineraryIndex);
this.bundle.setItineraryList(currentItineraryList);
}
@Override
public void onSwitchedToMainFragment(Fragment f) {
// TODO Auto-generated method stub
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.remove(f);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fm.popBackStack();
transaction.commit();
mainFragment.showRouteOnMap(currentItinerary, true);
}
@Override
public void setCurrentRequestString(String url) {
currentRequestString = url;
}
@Override
public String getCurrentRequestString() {
return currentRequestString;
}
@Override
public void zoomToLocation(LatLng location){
mainFragment.zoomToLocation(location);
}
@Override
public List<Itinerary> getCurrentItineraryList() {
// TODO Auto-generated method stub
return currentItineraryList;
}
@Override
public int getCurrentItineraryIndex() {
// TODO Auto-generated method stub
return currentItineraryIndex;
}
/**
* @return the isButtonStartLocation
*/
public boolean isButtonStartLocation() {
return isButtonStartLocation;
}
/**
* @param isButtonStartLocation the isButtonStartLocation to set
*/
public void setButtonStartLocation(boolean isButtonStartLocation) {
this.isButtonStartLocation = isButtonStartLocation;
}
public void setDateCompleteCallback(DateCompleteListener callback){
this.dateCompleteCallback = callback;
}
public void onDateComplete(Date tripDate, boolean scheduleType) {
dateCompleteCallback.onDateComplete(tripDate, scheduleType);
}
}
Mainfragment.java文件:用于onCreateOptionsMenu。
@Override
public void onCreateOptionsMenu(Menu pMenu, MenuInflater inflater) {
// MenuInflater inflater = getMenuInflater();
super.onCreateOptionsMenu(pMenu, inflater);
inflater.inflate(R.menu.menu, pMenu);
mGPS = pMenu.getItem(0);
}
@Override
public void onPrepareOptionsMenu(final Menu pMenu) {
if (isGPSEnabled()) {
mGPS.setTitle(R.string.disable_gps);
} else {
mGPS.setTitle(R.string.enable_gps);
}
super.onPrepareOptionsMenu(pMenu);
}
private Boolean isGPSEnabled() {
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
有人可以帮我解决这个问题吗?感谢你们。
答案 0 :(得分:0)
您的locationManager对象可能为null尝试此
private Boolean isGPSEnabled() {
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}