获取NullException [重复](Getting a NullException [duplicate])

系统教程 行业动态 更新时间:2024-06-14 16:57:40
获取NullException [重复](Getting a NullException [duplicate])

可能重复: 我的android程序中的NULL异常

有一个例外:

03-16 17:47:57.058: ERROR/AndroidRuntime(609): FATAL EXCEPTION: main 03-16 17:47:57.058: ERROR/AndroidRuntime(609): java.lang.RuntimeException: Unable to destroy activity {com.TravelPharmacy/com.TravelPharmacy.CountryView}: java.lang.NullPointerException 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2636) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2654) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at android.app.ActivityThread.access$2100(ActivityThread.java:117) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at android.os.Handler.dispatchMessage(Handler.java:99) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at android.os.Looper.loop(Looper.java:123) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at android.app.ActivityThread.main(ActivityThread.java:3647) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at java.lang.reflect.Method.invokeNative(Native Method) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at java.lang.reflect.Method.invoke(Method.java:507) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at dalvik.system.NativeStart.main(Native Method) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): Caused by: java.lang.NullPointerException 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at com.TravelPharmacy.CountryView.onDestroy(CountryView.java:117) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2623) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): ... 11 more

这是onCreate of First活动,当你按下按钮(fromButton)时,它将调用另一个活动并期望返回该国家的名称

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.home); // final int recquestCode = 0; final Button btnCountryfrom = (Button) findViewById(R.id.fromButton); btnCountryfrom.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent pingIntent = new Intent(getApplicationContext(),CountryView.class); pingIntent.putExtra("btnText", " "); pingIntent.setClass(TravelPharmacy.this, CountryView.class); startActivityForResult(pingIntent, RECEIVE_MESSAGE); Log.i("tag for easy filter in logcat 6","I am Working!!!"); ButtonFromPressed=true; } }); }

获取在其他活动中选择的国家/地区的名称时覆盖onActivityResult并在按钮中设置国家/地区的名称

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Button btnCountryfrom = (Button) findViewById(R.id.fromButton); CharSequence seq = data.getCharSequenceExtra("response"); btnCountryfrom.setText(seq); ButtonFromPressed=false; } }

我的第二个活动的onCreate

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.country); mListUsers = getCountry(); lvUsers = (ListView) findViewById(R.id.countrylistView); lvUsers.setAdapter(new ListAdapter(this, R.id.countrylistView, mListUsers)); lvUsers.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view,int position, long id) { Intent pongIntent = new Intent(); Log.i("tag for easy filter in logcat 1", "information you provide"); Log.i("tag for easy filter in logcat 2",mListUsers.get(position).pays); pongIntent.putExtra("response",mListUsers.get(position).pays); setResult(Activity.RESULT_OK,pongIntent); finish(); Log.i("tag for easy filter in logcat 3",mListUsers.get(position).pays); } }); }

我的第二个Activity的onDestroy()。

@Override public void onDestroy() { // adapter.imageLoader.stopThread(); //mListUsers=null; lv.setAdapter(null); super.onDestroy(); } <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/widget142" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" > <RelativeLayout android:id="@+id/widget143" android:layout_height="33px" android:background="@color/blue" android:layout_width="fill_parent"> <ImageButton android:id="@+id/homeImageButton" android:layout_width="47px" android:layout_height="34px" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" > </ImageButton> <TextView android:id="@+id/countryTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Country" android:textSize="20sp" android:textStyle="bold" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" > </TextView> </RelativeLayout> <ListView android:id="@+id/countrylistView" android:layout_width="match_parent" android:layout_height="250px"> </ListView> </LinearLayout>

我会感激帮助!!!

谢谢!

Possible Duplicate: NULL Exception in my android program

Have an Exception:

03-16 17:47:57.058: ERROR/AndroidRuntime(609): FATAL EXCEPTION: main 03-16 17:47:57.058: ERROR/AndroidRuntime(609): java.lang.RuntimeException: Unable to destroy activity {com.TravelPharmacy/com.TravelPharmacy.CountryView}: java.lang.NullPointerException 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2636) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2654) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at android.app.ActivityThread.access$2100(ActivityThread.java:117) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at android.os.Handler.dispatchMessage(Handler.java:99) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at android.os.Looper.loop(Looper.java:123) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at android.app.ActivityThread.main(ActivityThread.java:3647) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at java.lang.reflect.Method.invokeNative(Native Method) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at java.lang.reflect.Method.invoke(Method.java:507) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at dalvik.system.NativeStart.main(Native Method) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): Caused by: java.lang.NullPointerException 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at com.TravelPharmacy.CountryView.onDestroy(CountryView.java:117) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2623) 03-16 17:47:57.058: ERROR/AndroidRuntime(609): ... 11 more

That is the onCreate of First activity, when you press on the button (fromButton) that will call the other activity and expect to get back the name of the country

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.home); // final int recquestCode = 0; final Button btnCountryfrom = (Button) findViewById(R.id.fromButton); btnCountryfrom.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent pingIntent = new Intent(getApplicationContext(),CountryView.class); pingIntent.putExtra("btnText", " "); pingIntent.setClass(TravelPharmacy.this, CountryView.class); startActivityForResult(pingIntent, RECEIVE_MESSAGE); Log.i("tag for easy filter in logcat 6","I am Working!!!"); ButtonFromPressed=true; } }); }

when getting back the name of the country that be choose in the other activity Override onActivityResult and set the name of country in the button

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Button btnCountryfrom = (Button) findViewById(R.id.fromButton); CharSequence seq = data.getCharSequenceExtra("response"); btnCountryfrom.setText(seq); ButtonFromPressed=false; } }

the onCreate of my second Activity

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.country); mListUsers = getCountry(); lvUsers = (ListView) findViewById(R.id.countrylistView); lvUsers.setAdapter(new ListAdapter(this, R.id.countrylistView, mListUsers)); lvUsers.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view,int position, long id) { Intent pongIntent = new Intent(); Log.i("tag for easy filter in logcat 1", "information you provide"); Log.i("tag for easy filter in logcat 2",mListUsers.get(position).pays); pongIntent.putExtra("response",mListUsers.get(position).pays); setResult(Activity.RESULT_OK,pongIntent); finish(); Log.i("tag for easy filter in logcat 3",mListUsers.get(position).pays); } }); }

The onDestroy() for my second Activity.

@Override public void onDestroy() { // adapter.imageLoader.stopThread(); //mListUsers=null; lv.setAdapter(null); super.onDestroy(); } <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/widget142" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" > <RelativeLayout android:id="@+id/widget143" android:layout_height="33px" android:background="@color/blue" android:layout_width="fill_parent"> <ImageButton android:id="@+id/homeImageButton" android:layout_width="47px" android:layout_height="34px" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" > </ImageButton> <TextView android:id="@+id/countryTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Country" android:textSize="20sp" android:textStyle="bold" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" > </TextView> </RelativeLayout> <ListView android:id="@+id/countrylistView" android:layout_width="match_parent" android:layout_height="250px"> </ListView> </LinearLayout>

i will appreciate help!!!

thanks!

最满意答案

检查R.layout.country上是否存在ListAdapter,看起来您遇到了问题:

findViewById(R.id.countrylistView);

因为您的listView尚未实例化。 你能发布国家布局吗?

Check if the ListAdapter exist on R.layout.country where it looks like you are getting the problem:

findViewById(R.id.countrylistView);

Because your listView have not been instatiated. Can you post the country layout

更多推荐

本文发布于:2023-04-13 12:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/b7238f90f106b70d4348d5241687e62d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NullException   duplicate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!