0

我在應用程序中出現錯誤,當我到達列表視圖的末尾時崩潰。不知道是什麼導致了這個問題,以及如何解決這個問題。我使用遊標來填充我的列表視圖。這是我的代碼,我用來填充從列表視圖:當列表視圖到達列表末尾時出現錯誤/崩潰

How to assing an Image to Imageview based on a query results?

錯誤:

11-15 12:45:22.446: E/AndroidRuntime(276): FATAL EXCEPTION: main 
11-15 12:45:22.446: E/AndroidRuntime(276): java.lang.NullPointerException 
11-15 12:45:22.446: E/AndroidRuntime(276): at android.content.res.AssetManager.getResourceIdentifier(Native Method) 
11-15 12:45:22.446: E/AndroidRuntime(276): at android.content.res.Resources.getIdentifier(Resources.java:1422) 
11-15 12:45:22.446: E/AndroidRuntime(276): at com.example.yao.zCustomUsersAdapter.getView(zCustomUsersAdapter.java:62) 
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.AbsListView.obtainView(AbsListView.java:1294) 
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.ListView.makeAndAddView(ListView.java:1727) 
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.ListView.fillDown(ListView.java:652) 
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.ListView.fillGap(ListView.java:623) 
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2944) 
11-15 12:45:22.446: E/AndroidRuntime(276): at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:2485) 
11-15 12:45:22.446: E/AndroidRuntime(276): at android.os.Handler.handleCallback(Handler.java:587) 
11-15 12:45:22.446: E/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:92) 
11-15 12:45:22.446: E/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123) 
11-15 12:45:22.446: E/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627) 
11-15 12:45:22.446: E/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method) 
11-15 12:45:22.446: E/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521) 
11-15 12:45:22.446: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
11-15 12:45:22.446: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
11-15 12:45:22.446: E/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method) 
+1

zCustomUsersAdapter.java的第62行是什麼?你在那裏得到一個NPE。 – TronicZomB

+0

那麼,zCustomeUserAdapter是我創建自定義列表視圖的類(其中包含一個imageview和2個textview)此類將從我的數據庫中將值分配給imageview和textview。 – user3128770

回答

1

把空指針檢查將String.valueOf方法

,如: -

package com.example.yao; 

    import java.util.ArrayList; 
    import java.util.List; 

    import android.content.Context; 
    import android.graphics.drawable.Drawable; 
    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 zCustomUsersAdapter extends ArrayAdapter<YAOYVD> { 

     public zCustomUsersAdapter(Context context, List<YAOYVD> users) { 
      super(context, 0, users); 

     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      // Get the data item for this position 
     // User user = getItem(position); 
      YAOYVD user = getItem(position); 
      // Check if an existing view is being reused, otherwise inflate the view 
      if (convertView == null) { 
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.zitem_user, parent, false); 
      } 
      // Lookup view for data population 
      TextView tvName = (TextView) convertView.findViewById(R.id.tvName); 
      TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown); 

      ImageView tvImage = (ImageView) convertView.findViewById(R.id.ivUserIcon); 
      // Populate the data into the template view using the data object 
      tvName.setText(user.getID_YAO()!=null ? String.valueOf(user.getID_YAO()): null); 
       //.name); 
      tvHome.setText(user.getID_YAO() != null ? String.valueOf(user.getID_YAO()):   null); 
      //.hometown); 

      if(user.getSPIC_YAO() != null{ 
       tvImage.setBackgroundResource(user.getSPIC_YAO()); 
      } 

     return convertView; 
    } 
} 

問題可能是您的字符串值變爲空或後臺資源ID是錯誤的。 如果您對數值進行適當的檢查會更好。

+0

感謝這有助於。我注意到,如果我拿出Imageview的Listview作品找到。然後,我再次添加Imageview,看看該圖像是否存在,它沒有。有一些存在但其他人不存在。我添加了這個語法:Context ctx = getContext(); int resId = ctx.getResources()。getIdentifier(user.getSPIC_YAO(),「drawable」,ctx.getPackageName()); (resId!= 0){ tvImage.setBackgroundResource(resId); } else {tvImage.setImageResource(R.drawable.a4); //如果resId爲0,則替換圖像}但是,它會一直崩潰。然後,我添加了「if(file.exist)」,但它仍然崩潰。 – user3128770

+1

我想補充說,它是從數據庫傳遞給imageview的image(spic)值有一些空值。我回到數據庫並添加了一些空的值。現在正在工作,它不會崩潰。謝謝。 – user3128770