我正在努力让我的应用程序启动并运行,但我面临的问题所以我试图保存屏幕状态,当Android方向更改但应用程序始终保持崩溃时,我收到以上错误否则该应用程序工作正常。我认为最后2个方法(onSaveInstanceState和onRestoreInstanceState)导致了这个问题。
我感谢任何帮助。
03-30 15:14:40.199: E/AndroidRuntime(18902): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
03-30 15:14:40.199: E/AndroidRuntime(18902): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3924)
03-30 15:14:40.199: E/AndroidRuntime(18902): at android.app.ActivityThread.access$1000(ActivityThread.java:161)
03-30 15:14:40.199: E/AndroidRuntime(18902): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
03-30 15:14:40.199: E/AndroidRuntime(18902): at android.os.Handler.dispatchMessage(Handler.java:102)
03-30 15:14:40.199: E/AndroidRuntime(18902): at android.os.Looper.loop(Looper.java:157)
03-30 15:14:40.199: E/AndroidRuntime(18902): at android.app.ActivityThread.main(ActivityThread.java:5356)
03-30 15:14:40.199: E/AndroidRuntime(18902): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 15:14:40.199: E/AndroidRuntime(18902): at java.lang.reflect.Method.invoke(Method.java:515)
03-30 15:14:40.199: E/AndroidRuntime(18902): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
03-30 15:14:40.199: E/AndroidRuntime(18902): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
03-30 15:14:40.199: E/AndroidRuntime(18902): at dalvik.system.NativeStart.main(Native Method)
03-30 15:14:40.199: E/AndroidRuntime(18902): Caused by: java.lang.NullPointerException
03-30 15:14:40.199: E/AndroidRuntime(18902): at com.planetplace.GPSAPlanetActivity.onRestoreInstanceState(GPSAPlanetActivity.java:212)
03-30 15:14:40.199: E/AndroidRuntime(18902): at android.app.Activity.performRestoreInstanceState(Activity.java:949)
03-30 15:14:40.199: E/AndroidRuntime(18902): at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1156)
03-30 15:14:40.199: E/AndroidRuntime(18902): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283)
03-30 15:14:40.199: E/AndroidRuntime(18902): ... 12 more
GPSAPlanetActivity:
package com.planetplace;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.plant.abc.Plant;
public class GPSAPlanetActivity extends Activity implements LocationListener {
private static final String PLANT2 = "PLANT";
private static final String LONGITUDE2 = "Longitude";
private static final String LATITUDE2 = "Latitude";
EditText description;
TextView txtSelectedPlant;
public final static int CAREA_RESULT = 5;
public Bitmap plantImage;
private ImageView imgPlant;
private LocationManager LocationManager;
private Plant plant;
private double latitude;
private double longitude;
private TextView lblLatitudeValue;
private TextView lblLongitudevalue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps_plants);
description = (EditText) findViewById(R.id.etDescription);
txtSelectedPlant = (TextView) findViewById(R.id.tvSelectedPlant);
// get a references to the image view that will display a plant photot.
imgPlant = (ImageView) findViewById(R.id.imgPlant);
// get the LocationManager as a system service. Save it into a field
LocationManager = (android.location.LocationManager) getSystemService(LOCATION_SERVICE);
lblLatitudeValue = (TextView) findViewById(R.id.tvLatt);
lblLongitudevalue = (TextView) findViewById(R.id.tvLong);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void searchClicked(View v) {
Intent searchIntent = new Intent(this, AdvancedSearchActivity.class);
startActivityForResult(searchIntent,
AdvancedSearchActivity.PLANT_RESULTS);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == AdvancedSearchActivity.PLANT_RESULTS) {
// change the label of the text view to be the plant that was
// passed
// in
// store the plant as an attribute
Plant plant = (Plant) data
.getSerializableExtra(PlantResultActivity.PLANT_RESULT);
// set this plant in the TextView on the UI
txtSelectedPlant.setText(plant.toString());
} else if (requestCode == CAREA_RESULT) {
// we are here because we received result from the camera
plantImage = (Bitmap) data.getExtras().get("data");
imgPlant.setImageBitmap(plantImage);
}
}
}
public void onTakePhotoClicked(View v) {
// use an implicit intent to invoke the camera
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// start this intent, and antipcipate a result.
startActivityForResult(cameraIntent, CAREA_RESULT);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
requestLocation();
}
private void requestLocation() {
// TODO Auto-generated method stub
if (LocationManager != null) {
// the variable locationManger has an object assigned to it if we
// have gotten inside this
// if test. we want to do a null check like this first, because we
// want to avoid a NullPointerException.
// request location update
LocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 60000, 0, this);
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
// this method will be invoked when the GPS service informs us that us that
// our Location has changed.
// Anything that we want to do that should be updated when the GPS position
// of our phone as moved
// we must do in this method
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
latitude = location.getLatitude();
longitude = location.getLongitude();
updateUIForLocation();
}
private void updateUIForLocation() {
// update our user interface.
lblLatitudeValue.setText("" + latitude);
lblLongitudevalue.setText("" + longitude);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
//This method is called right before we change orientation, so that we can preserve values when the screen is redrawn
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putDouble(LATITUDE2, latitude);
outState.putDouble(LONGITUDE2, longitude);
outState.putSerializable(PLANT2, plant);
}
/**
* Dispaly values in the UI that were saved right before orientation changed.
*/
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
//repopulate values from the bundle we created in onSaveInstanceState method.
latitude = savedInstanceState.getDouble(LATITUDE2);
longitude = savedInstanceState.getDouble(LONGITUDE2);
updateUIForLocation();
plant = (Plant) savedInstanceState.getSerializable(PLANT2);
txtSelectedPlant.setText(plant.toString());
}
}
植物类:
package com.plant.abc;
import java.io.Serializable;
public class Plant implements Serializable {
private String genus;
private String species;
private String cultivar;
private String common;
private int id;
private int guid;
public String getGenus() {
return genus;
}
public void setGenus(String genus) {
this.genus = genus;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
public String getCultivar() {
return cultivar;
}
public void setCultivar(String cultivar) {
this.cultivar = cultivar;
}
public String getCommon() {
return common;
}
public void setCommon(String common) {
this.common = common;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getGuid() {
return guid;
}
public void setGuid(int guid) {
this.guid = guid;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return genus + " " + species + " " + common;
}
}
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="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.plantplaces.GPSAPlanetActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="GPS A Planet" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="22dp"
android:text="Description of the Plant" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/bntSearchPlant"
android:layout_below="@+id/bntSearchPlant"
android:layout_marginTop="52dp"
android:text="Lattitude" />
<TextView
android:id="@+id/tvLatt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView3"
android:layout_toRightOf="@+id/etDescription"
android:text="TextView" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:text="Longitude" />
<TextView
android:id="@+id/tvLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignLeft="@+id/tvLatt"
android:text="TextView" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView4"
android:text="Selected Plant" />
<TextView
android:id="@+id/tvSelectedPlant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvLong"
android:layout_below="@+id/tvLong"
android:text="TextView" />
<Button
android:id="@+id/btnCaptureImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView5"
android:layout_alignRight="@+id/bntSearchPlant"
android:layout_below="@+id/textView5"
android:layout_marginTop="14dp"
android:onClick="onTakePhotoClicked"
android:text="Take Photo" />
<EditText
android:id="@+id/etDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="15dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/bntSearchPlant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView2"
android:layout_below="@+id/etDescription"
android:onClick="searchClicked"
android:text="select a Planet" />
<ImageView
android:id="@+id/imgPlant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/btnCaptureImage"
android:src="@drawable/abc_ab_share_pack_holo_dark" />
</RelativeLayout>
答案 0 :(得分:0)
您的问题是plant
在调用onRestoreInstanceState()
时为空,因为plant
方法中已初始化onActivityResult()
(而不是onCreate()
方法)。更改代码以检查工厂是否为null,然后再尝试检索它并设置EditText
的文本:
plant = (Plant) savedInstanceState.getSerializable(PLANT2);
if(plant != null) // plant may have been null when it was saved in onSaveInstanceState
txtSelectedPlant.setText(plant.toString());