2011-03-16 108 views
1

可能重复:
NULL Exception in my android program获得一个NullException

有一个例外:

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,当你按下按钮( 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); 



     } 
    }); 

} 

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> 

我会感谢帮助!

谢谢!

+1

哪一行引发抛出NullPointer异常? – pankajagarwal 2011-03-16 13:12:58

+0

我怎么知道(对不起,我是新的) – dirko 2011-03-16 14:53:20

+0

你已经添加了堆栈跟踪的一部分。在那里你可以找到你活动崩溃的哪一行 – Tima 2011-03-16 15:30:47

回答

2

检查是否存在ListAdapter上R.layout.country它看起来像你所得到的问题:

findViewById(R.id.countrylistView);

因为你的listView没有被instatiated。您可以发布全国布局

+0

非常感谢你!!!!!惊人!!!! – dirko 2011-03-16 21:19:54

1

ListAdapter是一个接口,所以你不能新建一个ListAdapter,除非你在某处创建了一个名为ListAdapter的类,这可能是一个坏主意,你应该使用一个不同的名字。

在创建适配器时,您传入列表项的布局,而不是ListView的ID。

+0

我已经做了一个类ListAdapter – dirko 2011-03-16 14:57:23

0

莫非,该

mListUser为空或 mListUsers.get(位置)为空?

+0

如果mListUser为空,问题是什么? – dirko 2011-03-16 14:52:43

+0

问题是,你得到空指针异常。所以检查它 – Tima 2011-03-16 15:03:42

1

既然你已经张贴你的问题在another线程,很显然,这个问题是lv永远不会在你的activity初始化调用setAdapteronDestroy(),造成NullPointerException,因为它是null,因为它显示在这个片段你的代码:

@Override 
public void onDestroy() 
{ 

    //  adapter.imageLoader.stopThread(); 
    //mListUsers=null; 


    lv.setAdapter(null); 
    super.onDestroy(); 
} 

此外,当您想发布更多的数据关于您的问题,只需编辑你的问题,而不是创建一个新的:) 我希望这有助于。