gpt4 book ai didi

java - 运行时异常无法启动 MainActivity

转载 作者:太空狗 更新时间:2023-10-29 16:18:06 26 4
gpt4 key购买 nike

我正在尝试在刚购买的搭载 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 的行。

请查看this回答。可能会有帮助

关于java - 运行时异常无法启动 MainActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21968077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com