1

это xml-файл.java.lang.RuntimeException: Невозможно запустить Activity ComponentInfo {... EditCity}: java.lang.NullPointerException

<?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> 

это содержание логарифма.

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 

Hello! Когда я запускаю свою программу, она идет хорошо до определенного момента. Я получаю эту ошибку в своем логарифме.

Файл 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); 

} 

} 
+0

показать все данные о logcat !! – duggu

+0

'DBTools dbTools = new DBTools (this)' переместите это внутри 'onCreate' и опубликуйте stacktrace, и нет необходимости инициализировать ваши просмотры дважды – Raghunandan

+0

опубликуйте строку, которая вызывает NPE. 'EditCity.java: 44'? – Raghunandan

ответ

0

попробовать это: -

DBTools dbTools ; 

public void onCreate(Bundle savedInstanceState){ 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.edit_city); 
    dbTools = new DBTools(this); 
    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")); 

    } 
} 

вы не инициируют this значение перед вашим началом деятельности или бега. Первое действие началось, тогда вам нужно получить контекст активности или приложение (this, getBasecontext() и т. Д.).

В приведенном выше примере вы начинаете это перед началом вашей деятельности, поэтому logcat указывает исключение исключающего указателя.

+0

Я пробовал это, но ничего не изменилось. – zhukae

+0

@ user2759836 Что такое строка 44 'EditCity.java' – Raghunandan

+0

@Golu, как вы знаете, что это линия, которая вызывает NPE, это строка 44' EditCity.java' – Raghunandan

Смежные вопросы