【问题标题】:Runtime Exception unable to start MainActivity运行时异常无法启动 MainActivity
【发布时间】:2014-02-23 12:16:41
【问题描述】:

我正在尝试在我刚购买的装有 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

【问题讨论】:

  • 发布您的布局和 manifest.xml 文件
  • 您是否针对不同的屏幕尺寸使用多个布局文件?
  • AndroidManifest 没有发布 - 两秒钟!不,我不是,我不确定 Google 地图会不会有很大的问题?
  • 同时导入 com.google.android.gms.maps.SupportMapFragment;进入你的活动

标签: java android eclipse exception runtime


【解决方案1】:

我认为你的

 Location myLocation = locationManager.getLastKnownLocation(provider); 

返回 null 的行。

请查看this答案。这可能会有所帮助

【讨论】:

  • 你能解释一下为什么返回 null 吗?
  • 它为空,因为如果手机尚未获得定位修复,则可能没有最后一个已知位置。
  • 经过几个小时和几个错误,你是正确的!谢谢!
【解决方案2】:

这里,如果您在清单文件中以最低 SDK 版本 为目标,则不能使用 MapFragment。所以必须确保您的最低 SDK 版本高于或等于 12。如果不是,则在您的 xml 文件中更改

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();

此外,如果您在旧设备(Honeycomb 之前)上使用片段,则应始终从 FragmentActivity 扩展您的 Activity

还有import android.support.v4.app.FragmentActivity,有关更多信息,请访问我的这个answer

【讨论】:

  • 需要导入support-v4库
  • 这有帮助,但现在我收到更多错误:pastebin.com/hjEiuEby
  • 还需要将 google_play_services.lib 导入到您的项目中,还需要将 INTERNET 权限添加到您的 manifest.xml 文件中。
  • 我已经完成了你所说的一切,但仍然出现错误:pastebin.com/DrxBCWSp
【解决方案3】:

如果我没记错的话,应该在扩展FragmentFragmentActivity 的Activity 中使用片段。 map可能有问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2015-01-29
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多