簡體   English   中英

運行時異常無法啟動MainActivity

[英]Runtime Exception unable to start MainActivity

我正在嘗試在剛用Android 4.2購買的新手機上測試我的Android應用程序。 它運行我測試的所有其他應用程序,並且該特定應用程序還可以在其他設備上運行。 當我嘗試運行代碼時,出現以下錯誤:

> 02-23 12:15:08.532: E/AndroidRuntime(5431): FATAL EXCEPTION: main
02-23 12:15:08.532: E/AndroidRuntime(5431): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.antiquity/com.example.antiquity.MainActivity}: java.lang.NullPointerException
02-23 12:15:08.532: E/AndroidRuntime(5431):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
02-23 12:15:08.532: E/AndroidRuntime(5431):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
02-23 12:15:08.532: E/AndroidRuntime(5431):     at android.app.ActivityThread.access$600(ActivityThread.java:162)
02-23 12:15:08.532: E/AndroidRuntime(5431):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
02-23 12:15:08.532: E/AndroidRuntime(5431):     at android.os.Handler.dispatchMessage(Handler.java:107)
02-23 12:15:08.532: E/AndroidRuntime(5431):     at android.os.Looper.loop(Looper.java:194)
02-23 12:15:08.532: E/AndroidRuntime(5431):     at android.app.ActivityThread.main(ActivityThread.java:5371)
02-23 12:15:08.532: E/AndroidRuntime(5431):     at java.lang.reflect.Method.invokeNative(Native Method)
02-23 12:15:08.532: E/AndroidRuntime(5431):     at java.lang.reflect.Method.invoke(Method.java:525)
02-23 12:15:08.532: E/AndroidRuntime(5431):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
02-23 12:15:08.532: E/AndroidRuntime(5431):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-23 12:15:08.532: E/AndroidRuntime(5431):     at dalvik.system.NativeStart.main(Native Method)
02-23 12:15:08.532: E/AndroidRuntime(5431): Caused by: java.lang.NullPointerException
02-23 12:15:08.532: E/AndroidRuntime(5431):     at com.example.antiquity.MainActivity.setUpLocation(MainActivity.java:127)
02-23 12:15:08.532: E/AndroidRuntime(5431):     at com.example.antiquity.MainActivity.onCreate(MainActivity.java:96)
02-23 12:15:08.532: E/AndroidRuntime(5431):     at android.app.Activity.performCreate(Activity.java:5139)
02-23 12:15:08.532: E/AndroidRuntime(5431):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1084)
02-23 12:15:08.532: E/AndroidRuntime(5431):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
02-23 12:15:08.532: E/AndroidRuntime(5431):     ... 11 more

現在,我已經檢查了第127行,甚至將其刪除並留空行仍會導致與第127行有關的此錯誤。

我的MainActivity.class代碼:

public class MainActivity extends Activity {

    private GoogleMap map;
    Intent data;    
    MonumentsDatabase monDatabase;
    int _id;
    int _lat;
    int _lng;
    String _title;
    LatLng MARKERS;
    LocationManager locationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
               Intent data = new Intent(MainActivity.this, DisplayData.class);
               startActivity(data);

            }
        });

        setUpLocation();

        locationManager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

        if ( !locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            disabledGPS();
        }       

        map.setMyLocationEnabled(true);
    }


    public void setUpLocation() {
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);     
        Criteria criteria = new Criteria();      
        String provider = locationManager.getBestProvider(criteria, true);
        Location myLocation = locationManager.getLastKnownLocation(provider);
        double latitude = myLocation.getLatitude();
        double longitude = myLocation.getLongitude();
        LatLng latLng = new LatLng(latitude, longitude);      
        map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        map.animateCamera(CameraUpdateFactory.zoomTo(40));      
    }


    private void disabledGPS() {
        final AlertDialog.Builder gpsDisabled = new AlertDialog.Builder(this);
        gpsDisabled.setMessage("This app requires GPS to be enabled. Do you wish to continue?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
        AlertDialog mainAlert = gpsDisabled.create();
        mainAlert.show();
    }

    /**
    public boolean onMarkerClick(Marker marker) {
        //Intent data = new Intent(this, DisplayData.class);
        // TODO Auto-generated method stub
        startActivity(data);
        return true;
    }
    */

    @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;
    }

    public void createDatabase() {
        monDatabase = new MonumentsDatabase(this);
        try {   
            monDatabase.createDatabase();
        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }
        try {
            monDatabase.openDatabase();
        }catch(SQLException sqle){ 
            throw sqle;
        }
    }

}

我發現它在一個設備上如何工作然后在另一個設備上崩潰而感到奇怪。

謝謝你的幫助! :)

編輯:

activity_layout.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"

tools:context=".MainActivity" >

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

AndroidManifest.xml文件:

不知道為什么,但是不喜歡縮進。

非常抱歉,但是:

http://pastebin.com/kd4EpP4W

我覺得你

 Location myLocation = locationManager.getLastKnownLocation(provider); 

行返回null。

請檢查答案。 可能會有幫助

在這里,如果您要在清單文件中定位最低SDK版本<12 ,則不能使用MapFragment 因此,必須確保您的最低SDK版本高於或等於12。如果不是,則請從

android:name="com.google.android.gms.maps.MapFragment"

android:name="com.google.android.gms.maps.SupportMapFragment"

而且您的活動從

 map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

 map=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

而且,如果您在較舊的設備(蜂窩前)上使用片段,則應始終從FragmentActivity擴展Activity

還要import android.support.v4.app.FragmentActivity ,有關更多信息,請轉到我的答案

如果我沒記錯的話,應該在Activity中使用Fragment ,它可以擴展FragmentFragmentActivity map可能出了點問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM