这是xml文件。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:text="@string/edit_city"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/cityName"
android:layout_marginTop="20dp"
android:text="@string/country_name" />
<EditText
android:id="@+id/countryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/cityName"
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/countryName"
android:layout_alignLeft="@+id/textView1"
android:text="@string/city_name" />
<EditText
android:id="@+id/cityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:inputType="text" />
<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:layout_marginTop="20dp"
android:text="@string/population" />
<EditText
android:id="@+id/population"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView4"
android:layout_alignLeft="@+id/countryName"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:layout_marginTop="20dp"
android:text="@string/traditions" />
<EditText
android:id="@+id/traditions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView5"
android:layout_alignLeft="@+id/population"
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView5"
android:layout_below="@+id/textView5"
android:layout_marginTop="21dp"
android:text="@string/impressions" />
<EditText
android:id="@+id/impressions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView6"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView6"
android:layout_below="@+id/textView6"
android:layout_marginTop="21dp"
android:text="@string/url_image" />
<EditText
android:id="@+id/urlImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView7"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="text" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="38dp"
android:layout_toRightOf="@+id/textView3"
android:background="#444444"
android:textColor="#FFFFFF"
android:onClick="editCity"
android:text="@string/edit_button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_marginLeft="56dp"
android:layout_toRightOf="@+id/button1"
android:background="#444444"
android:onClick="displayCity"
android:text="@string/view_button"
android:textColor="#FFFFFF"
android:textSize="20sp" />
</RelativeLayout>
这是 logcat 内容。
09-10 16:55:15.975: D/AndroidRuntime(297): Shutting down VM
09-10 16:55:15.975: W/dalvikvm(297): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-10 16:55:15.995: E/AndroidRuntime(297): FATAL EXCEPTION: main
09-10 16:55:15.995: E/AndroidRuntime(297): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.citiestoremember/com.example.citiestoremember.EditCity}: java.lang.NullPointerException
09-10 16:55:15.995: E/AndroidRuntime(297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-10 16:55:15.995: E/AndroidRuntime(297): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-10 16:55:15.995: E/AndroidRuntime(297): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-10 16:55:15.995: E/AndroidRuntime(297): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-10 16:55:15.995: E/AndroidRuntime(297): at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 16:55:15.995: E/AndroidRuntime(297): at android.os.Looper.loop(Looper.java:123)
09-10 16:55:15.995: E/AndroidRuntime(297): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-10 16:55:15.995: E/AndroidRuntime(297): at java.lang.reflect.Method.invokeNative(Native Method)
09-10 16:55:15.995: E/AndroidRuntime(297): at java.lang.reflect.Method.invoke(Method.java:521)
09-10 16:55:15.995: E/AndroidRuntime(297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-10 16:55:15.995: E/AndroidRuntime(297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-10 16:55:15.995: E/AndroidRuntime(297): at dalvik.system.NativeStart.main(Native Method)
09-10 16:55:15.995: E/AndroidRuntime(297): Caused by: java.lang.NullPointerException
09-10 16:55:15.995: E/AndroidRuntime(297): at com.example.citiestoremember.EditCity.onCreate(EditCity.java:44)
09-10 16:55:15.995: E/AndroidRuntime(297): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-10 16:55:15.995: E/AndroidRuntime(297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-10 16:55:15.995: E/AndroidRuntime(297): ... 11 more
你好!当我运行我的程序时,它一直运行良好,直到某个特定时刻。我在我的 logcat 中收到此错误。
发生错误的 EditCity.java 文件如下。谁能帮我 ?谢谢!
package com.example.citiestoremember;
import java.util.HashMap;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class EditCity extends Activity{
EditText cityName;
EditText countryName;
EditText populationNumber;
EditText traditions;
EditText impressions;
EditText urlImage;
DBTools dbTools = new DBTools(this);
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_city);
cityName = (EditText) findViewById(R.id.cityName);
countryName = (EditText) findViewById(R.id.countryName);
populationNumber = (EditText) findViewById(R.id.populationNumber);
traditions = (EditText) findViewById(R.id.traditions);
impressions = (EditText) findViewById(R.id.impressions);
urlImage = (EditText) findViewById(R.id.urlImage);
Intent theIntent = getIntent();
String cityId = theIntent.getStringExtra("cityId");
HashMap<String, String> cityList = dbTools.getCityInfo(cityId);
if(cityList.size() != 0){
cityName.setText(cityList.get("cityName"));
countryName.setText(cityList.get("countryName"));
populationNumber.setText(cityList.get("populationNumber"));
traditions.setText(cityList.get("traditions"));
impressions.setText(cityList.get("impressions"));
urlImage.setText(cityList.get("urlImage"));
}
}
public void editCity(View view){
HashMap<String, String> queryValuesMap = new HashMap<String, String>();
cityName = (EditText) findViewById(R.id.cityName);
countryName = (EditText) findViewById(R.id.countryName);
populationNumber = (EditText) findViewById(R.id.populationNumber);
traditions = (EditText) findViewById(R.id.traditions);
impressions = (EditText) findViewById(R.id.impressions);
urlImage = (EditText) findViewById(R.id.urlImage);
Intent theIntent = getIntent();
String cityId = theIntent.getStringExtra("cityId");
queryValuesMap.put("cityId", cityId);
queryValuesMap.put("cityName", cityName.getText().toString());
queryValuesMap.put("countryName", countryName.getText().toString());
queryValuesMap.put("populationNumber", populationNumber.getText().toString());
queryValuesMap.put("traditions", traditions.getText().toString());
queryValuesMap.put("impressions", impressions.getText().toString());
queryValuesMap.put("urlImage", urlImage.getText().toString());
dbTools.updateCity(queryValuesMap);
this.callMainActivity(view);
}
public void displayCity(View view){
Intent theIntent = new Intent(getApplicationContext(), DisplayImage.class);
String urlImage = theIntent.getStringExtra("urlImage");
theIntent.putExtra("urlImage", urlImage);
startActivity(theIntent);
}
public void callMainActivity(View view){
Intent objIntent = new Intent(getApplication(), MainActivity.class);
startActivity(objIntent);
}
}