android.widget.ListView.java中的空指针异常

时间:2014-09-29 12:41:37

标签: java android eclipse android-layout listview

我的活动包含swipe view,有四个标签。每个选项卡都通过自定义布局实现List View(通过扩展基本适配器)。

当我突然得到一个空指针异常时,一切正常。应用程序仍然在4.4.3上运行时没有任何错误,而当我在4.4.2上运行它时强制关闭。

Log Cat

09-28 17:12:56.786: W/View(12924): requestLayout() improperly called by android.widget.ProgressBar{42feb9a8 V.ED.... ......ID 312,471-408,567 #7f090020 app:id/progress_bar} during layout: running second layout pass
09-28 17:12:56.806: D/Admin check(12924): yes
09-28 17:12:56.811: D/Admin check(12924): yes
09-28 17:12:56.816: D/AndroidRuntime(12924): Shutting down VM
09-28 17:12:56.816: W/dalvikvm(12924): threadid=1: thread exiting with uncaught exception (group=0x41d73c08)
09-28 17:12:56.831: E/AndroidRuntime(12924): FATAL EXCEPTION: main
09-28 17:12:56.831: E/AndroidRuntime(12924): Process: com.example.shoutoutloud, PID: 12924
09-28 17:12:56.831: E/AndroidRuntime(12924): java.lang.NullPointerException
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.widget.ListView.measureScrapChild(ListView.java:1194)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1276)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.widget.ListView.onMeasure(ListView.java:1186)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.View.measure(View.java:17495)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.View.measure(View.java:17495)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.View.measure(View.java:17495)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1451)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.View.measure(View.java:17495)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.View.measure(View.java:17495)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.View.measure(View.java:17495)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.View.measure(View.java:17495)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:412)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.View.measure(View.java:17495)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2548)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.View.measure(View.java:17495)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2285)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1396)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2381)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6637)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:814)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.Choreographer.doCallbacks(Choreographer.java:614)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.Choreographer.doFrame(Choreographer.java:584)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:800)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.os.Handler.handleCallback(Handler.java:733)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.os.Handler.dispatchMessage(Handler.java:95)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.os.Looper.loop(Looper.java:146)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at android.app.ActivityThread.main(ActivityThread.java:5602)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at java.lang.reflect.Method.invokeNative(Native Method)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at java.lang.reflect.Method.invoke(Method.java:515)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
09-28 17:12:56.831: E/AndroidRuntime(12924):    at dalvik.system.NativeStart.main(Native Method)

其中一个自定义适配器的源代码 `@SuppressLint({" NewApi"," UseSparseArrays"," DefaultLocale"}) 公共类AlertsAdapter扩展了BaseAdapter {

String client_emailId;

JSONParser jsonParser = new JSONParser();

public int lastPosition = -1;

public LayoutInflater inflater=null;
public Context context;
public Activity activity;
ArrayList<HashMap<String, String>>data;
public ImageLoader imageLoader; 

public static HashMap<String, String> news_alerts;

public AlertsAdapter(Context a, Activity act, ArrayList<HashMap<String, String>> d) {
    //super();
    this.context = a;
    this.data = d;
    this.activity = act;
    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.imageLoader=new ImageLoader(context);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return data.get(position);
}

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

static class ViewHolder{
    TextView name, action, grit, location, time, distance;
    LinearLayout v;
}

int clientId;
public HashMap<Integer, Integer> postIdHash = new HashMap<Integer, Integer>();
public HashMap<Integer, String> readHash = new HashMap<Integer, String>();

@SuppressLint("DefaultLocale")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View vi = convertView;

    final ViewHolder holder;

    if(vi == null){
       vi = inflater.inflate(R.layout.alerts_landing_list, parent, false);
       holder = new ViewHolder();
       holder.name = (TextView) vi.findViewById(R.id.profileName);
       holder.action = (TextView) vi.findViewById(R.id.action);
       holder.grit = (TextView) vi.findViewById(R.id.grit);
       holder.location = (TextView) vi.findViewById(R.id.geoCoding);
       holder.time = (TextView) vi.findViewById(R.id.timePassed);
       holder.distance = (TextView) vi.findViewById(R.id.distance);
       holder.v = (LinearLayout) vi.findViewById(R.id.container);

       vi.setTag(holder);
    }
    else{
        holder = (ViewHolder) vi.getTag();
    }

       news_alerts = new HashMap<String, String>();
       news_alerts = data.get(position);

       postIdHash.put(position, Integer.parseInt(news_alerts.get(Alerts.POSTID)));

       holder.name.setText(news_alerts.get(Alerts.NAME)+"  ");
       holder.action.setText(news_alerts.get(Alerts.ACTION)+"  ");
       holder.grit.setText(news_alerts.get(Alerts.MESSAGE)+"  ");
       holder.location.setText(" "+news_alerts.get(Alerts.LOCATION)+"  ");
       holder.time.setText(" "+news_alerts.get(Alerts.TIME)+"  ");
       holder.distance.setText(" "+news_alerts.get(Alerts.DISTANCE)+" kms");

       readHash.put(position, news_alerts.get(Alerts.READ).toString());

       if(readHash.get(position).equals("0")){
           holder.v.setBackgroundColor(Color.parseColor("#878787"));
           holder.name.setTextColor(Color.parseColor("#f8f8f8"));
           holder.action.setTextColor(Color.parseColor("#f8f8f8"));
           holder.grit.setTextColor(Color.parseColor("#f8f8f8"));
           holder.location.setTextColor(Color.parseColor("#f8f8f8"));
           holder.time.setTextColor(Color.parseColor("#f8f8f8"));
           holder.distance.setTextColor(Color.parseColor("#f8f8f8"));
       }

       Log.d("post_id", postIdHash.get(position).toString());
       // Open ShowAlert.java activity on clicking the notification
       holder.v.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(readHash.get(position).equals("0")){
                   holder.v.setBackgroundColor(Color.parseColor("#ffffff"));
                   holder.name.setTextColor(Color.parseColor("#606060"));
                   holder.action.setTextColor(Color.parseColor("#919191"));
                   holder.grit.setTextColor(Color.parseColor("#606060"));
                   holder.location.setTextColor(Color.parseColor("#919191"));
                   holder.time.setTextColor(Color.parseColor("#919191"));
                   holder.distance.setTextColor(Color.parseColor("#919191"));
            }
            // TODO Auto-generated method stub
            client_emailId = LandingPage.email_id;
            Intent intent = new Intent(activity, ShowAlert.class);
            intent.putExtra("post_id", postIdHash.get(position).toString());
            activity.startActivity(intent);
            Log.d("Post ID from alert tab on clicking:", postIdHash.get(position).toString());
        }
    });

    return vi;
}

}`

调试时也会出现类似android manifest包的错误。

这种奇怪行为的可能原因是什么?

0 个答案:

没有答案