致命异常:主要在BinderData.getView(BinderData.java:93)

时间:2013-12-24 21:45:19

标签: java android nullpointerexception fatal-error forceclose

我收到错误FATAL EXCEPTION: main at BinderData.getView(BinderData.java:93)

我的来源第93行是:Drawable image = vi.getContext().getResources().getDrawable(imageResource);

我试图在它上方的线上设置一个断点并跳过它,但是当我这样做时,我得到了:

Source not found. Edit Source Lookup Path.


12-24 16:40:20.266: E/AndroidRuntime(8649): FATAL EXCEPTION: main
12-24 16:40:20.266: E/AndroidRuntime(8649): android.content.res.Resources$NotFoundException: Resource ID #0x0
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.content.res.Resources.getValue(Resources.java:1033)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.content.res.Resources.getDrawable(Resources.java:672)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at com.idg.voiscphone.BinderData.getView(BinderData.java:93)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.widget.AbsListView.obtainView(AbsListView.java:2207)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.widget.ListView.onMeasure(ListView.java:1162)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.View.measure(View.java:15604)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.View.measure(View.java:15604)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.View.measure(View.java:15604)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.View.measure(View.java:15604)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2191)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.View.measure(View.java:15604)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4879)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.Choreographer.doCallbacks(Choreographer.java:579)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.Choreographer.doFrame(Choreographer.java:548)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.os.Handler.handleCallback(Handler.java:725)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.os.Looper.loop(Looper.java:153)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at android.app.ActivityThread.main(ActivityThread.java:5297)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at java.lang.reflect.Method.invokeNative(Native Method)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at java.lang.reflect.Method.invoke(Method.java:511)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-24 16:40:20.266: E/AndroidRuntime(8649):     at dalvik.system.NativeStart.main(Native Method)

JAVA:

public class BinderData extends BaseAdapter {

    // XML node keys
    static final String KEY_TAG = "weatherdata"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_CITY = "city";
    static final String KEY_TEMP_C = "tempc";
    static final String KEY_TEMP_F = "tempf";
    static final String KEY_CONDN = "condition";
    static final String KEY_SPEED = "windspeed";
    static final String KEY_ICON = "icon";

    LayoutInflater inflater;
    ImageView thumb_image;
    List<HashMap<String,String>> weatherDataCollection;
    ViewHolder holder;
    public BinderData() {
        // TODO Auto-generated constructor stub
    }

    public BinderData(Activity act, List<HashMap<String,String>> map) {

        this.weatherDataCollection = map;

        inflater = (LayoutInflater) act
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    public int getCount() {
        // TODO Auto-generated method stub
//      return idlist.size();
        return weatherDataCollection.size();
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

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

        View vi=convertView;
        if(convertView==null){

          vi = inflater.inflate(R.layout.list_row, null);
          holder = new ViewHolder();

          holder.tvCity = (TextView)vi.findViewById(R.id.tvCity); // city name
          holder.tvWeather = (TextView)vi.findViewById(R.id.tvCondition); // city weather overview
          holder.tvTemperature =  (TextView)vi.findViewById(R.id.tvTemp); // city temperature
          holder.tvWeatherImage =(ImageView)vi.findViewById(R.id.list_image); // thumb image

          vi.setTag(holder);
        }
        else{

            holder = (ViewHolder)vi.getTag();
        }

          // Setting all values in listview

          holder.tvCity.setText(weatherDataCollection.get(position).get(KEY_CITY));
          holder.tvWeather.setText(weatherDataCollection.get(position).get(KEY_CONDN));
          holder.tvTemperature.setText(weatherDataCollection.get(position).get(KEY_TEMP_C));

          //Setting an image
          String uri = "drawable/"+ weatherDataCollection.get(position).get(KEY_ICON);
          int imageResource = vi.getContext().getApplicationContext().getResources().getIdentifier(uri, null, vi.getContext().getApplicationContext().getPackageName());
          Drawable image = vi.getContext().getResources().getDrawable(imageResource);
          holder.tvWeatherImage.setImageDrawable(image);

          return vi;
    }

    /*
     * 
     * */
    static class ViewHolder{

        TextView tvCity;
        TextView tvTemperature;
        TextView tvWeather;
        ImageView tvWeatherImage;
    }

}

1 个答案:

答案 0 :(得分:0)

Resources $ NotFoundException - 当找不到请求的资源时,资源API抛出此异常。

问题在这里

int imageResource = vi.getContext().getApplicationContext().getResources().getIdentifier(uri, null, vi.getContext().getApplicationContext().getPackageName());