Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行时异常无法启动MainActivity_Java_Android_Eclipse_Exception_Runtime - Fatal编程技术网

Java 运行时异常无法启动MainActivity

Java 运行时异常无法启动MainActivity,java,android,eclipse,exception,runtime,Java,Android,Eclipse,Exception,Runtime,我正试图在我刚用安卓4.2购买的新手机上测试我的安卓应用程序。它运行我测试过的每一个应用程序,这个特定的应用程序也可以在其他设备上运行。当我尝试运行代码时,它会给我以下错误: > 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 act

我正试图在我刚用安卓4.2购买的新手机上测试我的安卓应用程序。它运行我测试过的每一个应用程序,这个特定的应用程序也可以在其他设备上运行。当我尝试运行代码时,它会给我以下错误:

> 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文件:

<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" />
不知道为什么,但它不喜欢缩进

非常抱歉链接,但是:

我想你的

 Location myLocation = locationManager.getLastKnownLocation(provider); 
返回null的行


请核对答案。如果我没有弄错的话,片段应该用在扩展
Fragment
FragmentActivity
的活动中,这可能会有帮助。这里的
map

可能有问题,如果您将清单文件中的最低SDK版本<12作为目标,那么您就不能使用MapFragment。因此,必须确保您的最低SDK版本高于或等于12。如果不是,则在xml文件中从

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

而且在你的活动中从

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

此外,如果您在旧设备(蜂窝前)上使用碎片,您应该始终从
碎片活动
扩展
活动


另外
导入android.support.v4.app.FragmentActivity
有关这方面的更多信息,请转到my this

发布您的布局和manifest.xml文件您是否为不同的屏幕大小使用多个布局文件?AndroidManifest没有发布-两秒钟!不,我不知道,我不确定谷歌地图会有多大问题?也导入com.Google.android.gms.Maps.SupportMapFragment;在您的活动中,您可以解释此返回为空的原因吗?它为空,因为如果手机尚未获得位置修复,则可能没有最后已知的位置。经过几个小时和几个错误,您是正确的!非常感谢。需要导入support-v4 Library这有所帮助,但现在我遇到了更多错误:还需要将google_play_services.lib导入到项目中,还需要将INTERNET权限添加到manifest.xml文件中。我已经完成了您所说的所有操作,但仍然会遇到错误:
 map=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();