2012-06-28 61 views
0

我在Android連接的Google應用程序引擎項目上部署自定義數組適配器時遇到了關鍵問題。即使在我將Android應用程序作爲Android應用程序進行調試時輸入代碼時沒有錯誤,但模擬器會導致錯誤並停止應用程序。我的自定義適配器代碼如下,根據調試器,在第46行之後有NullPointerException。這是什麼以及如何解決它?在Android連接的Google App Engine項目上創建自定義ArrayAdapter

package com.cheapchase; 

import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class MyListAdapter extends ArrayAdapter<ListItem>{ 

    Context context; 
    int layoutResourceId; 
    ListItem data[] = null; 

    public MyListAdapter(Context context, int layoutResourceId, ListItem[] data){ 

     super(context,layoutResourceId, data); 
     this.layoutResourceId = layoutResourceId; 
     this.context = context; 
     this.data = data; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent){ 

     View row = convertView; 
     ListHolder holder = null; 

     if(row == null){ 

      LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
      row = inflater.inflate(layoutResourceId, parent, false); 

      holder = new ListHolder(); 
      holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon); 
      holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle); 
     } 
     else 
     { 
      holder = (ListHolder)row.getTag(); 
     } 

     ListItem listrow = data[position]; 
     holder.txtTitle.setText(listrow.title); 
     holder.imgIcon.setImageResource(listrow.icon); 

     return row; 
    } 

    static class ListHolder 
    { 
     ImageView imgIcon; 
     TextView txtTitle; 
    } 

} 

這是活動調用適配器:

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.ListView; 

public class StoresList extends Activity{ 

    /** 
    * The current context. 
    */ 

    private ListView listView1; 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.store_list); 
    } 

    public void onResume(){ 

     super.onResume(); 

     ListItem storelist[] = new ListItem[]{ 
       new ListItem(R.drawable.androidmsg, "test1"), 
       new ListItem(R.drawable.androidmsg, "test2") 
     }; 


     MyListAdapter adapter = new MyListAdapter(this, R.layout.listview_item_row, storelist); 

     listView1 = (ListView)findViewById(R.id.listView1); 

     /*View header = (View)getLayoutInflater().inflate(R.layout.listview_item_header, null); 
     listView1.addHeaderView(header);*/ 

     listView1.setAdapter(adapter); 
    } 



// **** end of file 
} 

這是錯誤日誌...

06-28 15:16:44.687: W/dalvikvm(1584): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
06-28 15:16:44.706: E/AndroidRuntime(1584): FATAL EXCEPTION: main 
06-28 15:16:44.706: E/AndroidRuntime(1584): java.lang.RuntimeException: Unable to resume activity {com.cheapchase/com.cheapchase.StoresList}: android.app.SuperNotCalledException: Activity {com.cheapchase/com.cheapchase.StoresList} did not call through to super.onResume() 
06-28 15:16:44.706: E/AndroidRuntime(1584):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120) 
06-28 15:16:44.706: E/AndroidRuntime(1584):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135) 
06-28 15:16:44.706: E/AndroidRuntime(1584):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668) 
06-28 15:16:44.706: E/AndroidRuntime(1584):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
06-28 15:16:44.706: E/AndroidRuntime(1584):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
06-28 15:16:44.706: E/AndroidRuntime(1584):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-28 15:16:44.706: E/AndroidRuntime(1584):  at android.os.Looper.loop(Looper.java:130) 
06-28 15:16:44.706: E/AndroidRuntime(1584):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
06-28 15:16:44.706: E/AndroidRuntime(1584):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-28 15:16:44.706: E/AndroidRuntime(1584):  at java.lang.reflect.Method.invoke(Method.java:507) 
06-28 15:16:44.706: E/AndroidRuntime(1584):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
06-28 15:16:44.706: E/AndroidRuntime(1584):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
06-28 15:16:44.706: E/AndroidRuntime(1584):  at dalvik.system.NativeStart.main(Native Method) 
06-28 15:16:44.706: E/AndroidRuntime(1584): Caused by: android.app.SuperNotCalledException: Activity {com.cheapchase/com.cheapchase.StoresList} did not call through to super.onResume() 
06-28 15:16:44.706: E/AndroidRuntime(1584):  at android.app.Activity.performResume(Activity.java:3834) 
06-28 15:16:44.706: E/AndroidRuntime(1584):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110) 
06-28 15:16:44.706: E/AndroidRuntime(1584):  ... 12 more 
06-28 15:17:19.967: W/ActivityThread(1617): Application com.cheapchase is waiting for the debugger on port 8100... 

謝謝您的幫助。

+0

第46行指向兩個代碼集都沒有代碼。請向我們提供您的logcat –

回答

0

您從未設置標籤值,因此當您執行holder = (ListHolder)row.getTag();時,它將返回null

 holder = new ListHolder(); 
     holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon); 
     holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle); 
     row.setTag(holder); //<--- missing this 

編輯:除了現在你的日誌抱怨SuperNotCalled。