简体   繁体   English

运行时异常无法启动MainActivity

[英]Runtime Exception unable to start MainActivity

I'm trying to test my Android app on a new phone I've just purchased with Android 4.2. 我正在尝试在刚用Android 4.2购买的新手机上测试我的Android应用程序。 It runs every other app I test, and this particular app also works on other devices. 它运行我测试的所有其他应用程序,并且该特定应用程序还可以在其他设备上运行。 When I try to run the code it's giving me the following errors: 当我尝试运行代码时,出现以下错误:

> 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

Now I've checked line 127 and even removing that and leaving an empty line will still cause this error relating to line 127. 现在,我已经检查了第127行,甚至将其删除并留空行仍会导致与第127行有关的此错误。

My MainActivity.class code: 我的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;
        }
    }

}

I find it strange how it works on one device and then crashes on another. 我发现它在一个设备上如何工作然后在另一个设备上崩溃而感到奇怪。

Thanks for any help! 谢谢你的帮助! :) :)

EDIT: 编辑:

activity_layout.xml file: 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 file: AndroidManifest.xml文件:

Not sure why, but it isn't liking the indents. 不知道为什么,但是不喜欢缩进。

So sorry for the link but: 非常抱歉,但是:

http://pastebin.com/kd4EpP4W http://pastebin.com/kd4EpP4W

I think your 我觉得你

 Location myLocation = locationManager.getLastKnownLocation(provider); 

line returning null. 行返回null。

Please check this answer. 请检查答案。 It might be helpful 可能会有帮助

Here, If you are targeting your minimum SDK version < 12 in your manifest file then you can't use MapFragment . 在这里,如果您要在清单文件中定位最低SDK版本<12 ,则不能使用MapFragment So must be sure that your minimum SDK version is above or equals to 12. And if it is not then change in your xml file from 因此,必须确保您的最低SDK版本高于或等于12。如果不是,则请从

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

to

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

And also in your activity change from 而且您的活动从

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

to

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

And also If you're using fragments on older devices (pre Honeycomb) you should always extend your Activity from FragmentActivity . 而且,如果您在较旧的设备(蜂窝前)上使用片段,则应始终从FragmentActivity扩展Activity

Also import android.support.v4.app.FragmentActivity and for more information on this go to my this answer 还要import android.support.v4.app.FragmentActivity ,有关更多信息,请转到我的答案

If I don't mistake, fragments should be used in Activity which extends either Fragment or FragmentActivity . 如果我没记错的话,应该在Activity中使用Fragment ,它可以扩展FragmentFragmentActivity Probably something is wrong with map map可能出了点问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MainActivity上的空指针异常错误,无法启动整个应用程序 - Null Pointer Exception Error on MainActivity that unable to start the whole app 运行时异常:无法启动活动android listview - Runtime Exception: Unable to start activity android listview java.lang.runtime异常:无法启动组件 - java.lang.runtime Exception : unable to start component 无法使用Mainactivity和Navigation抽屉启动活动ComponentInfo? - Unable to start activity ComponentInfo with Mainactivity and Navigation drawer? 不启动 mainactivity - does not start mainactivity 从MainActivity开始片段 - Start fragment from MainActivity Java运行时异常:无法创建锁管理器 - Java Runtime Exception: Unable to create lock manager 无法启动MainActivity-您需要在此活动中使用Theme.AppCompat主题(或后代) - Unable to start MainActivity - You need to use a Theme.AppCompat theme (or descendant) with this activity RuntimeException:无法启动活动(MainActivity无法转换为android.view.View $ OnClickListener) - RuntimeException: Unable to start activity(MainActivity cannot be cast to android.view.View$OnClickListener) 无法启动活动 ComponentInfo{com.example.admin.testapplication/com.example.admin.testapplication.MainActivity} - Unable to start activity ComponentInfo{com.example.admin.testapplication/com.example.admin.testapplication.MainActivity}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM