如何在android中删除nullpointerException?

时间:2014-11-02 00:34:30

标签: android

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
         if(!ConstantVariable.isfilterOccured){
             View v = convertView;
            if (v == null) {
                LayoutInflater mInflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = mInflater.inflate(R.layout.row_listitem, null);
            }

            final TextView platFormName = (TextView) v
                    .findViewById(R.id.item_platform);

            final TextView schDepart = (TextView) v
                    .findViewById(R.id.item_schDepart);
            final TextView expDepart = (TextView) v
                    .findViewById(R.id.item_expectdepart);
            final TextView arrival = (TextView) v
                    .findViewById(R.id.item_arrival);
            final TextView exparrival = (TextView) v
                    .findViewById(R.id.item_expertarrival);
            final TextView stationName = (TextView) v
                    .findViewById(R.id.item_stationName);
            final ImageView alertStatusImage = (ImageView) v
                    .findViewById(R.id.alertStatus);

            final String platformValue = deparaturedaseboarddto.get(position).getPlatformNo();
            final String schDepartValue= deparaturedaseboarddto.get(position).getSchDepart();
            final String schExpectValue= deparaturedaseboarddto.get(position).getExpDepart();
            final String arrivalValue= deparaturedaseboarddto.get(position).getDestSchArrival();
            final String exparrivalValue= deparaturedaseboarddto.get(position).getDestSchArrival();
           String stationNameValue= deparaturedaseboarddto.get(position).getDestinationStation().getStationName();
           if(deparaturedaseboarddto.get(position).getAlertsId()!=0){
               alertStatusImage.setVisibility(View.VISIBLE);
           }else {
               alertStatusImage.setVisibility(View.INVISIBLE);  
           }


            platFormName.setText(platformValue);

            schDepart.setText(schDepartValue);
            expDepart.setText(schExpectValue);
            arrival.setText(arrivalValue);
            exparrival.setText(exparrivalValue);
            stationName.setText(stationNameValue);
            if (position%2==0) {
                Log.d("--------", "old");
                v.setBackgroundColor(Color.parseColor("#FFFFFF"));
            } else {
                Log.d("--------", "even");

                v.setBackgroundColor(Color.parseColor("#f3f3f3"));
            }
            return v;
         }else {
             View vv = convertView;

                if (vv == null) {
                    LayoutInflater mInflater = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    vv = mInflater.inflate(R.layout.filterdashboard, null);
                }

                final TextView platFormName = (TextView) vv
                        .findViewById(R.id.platFormId);

                final TextView destinationName = (TextView) vv
                        .findViewById(R.id.destinationStationName);
                final TextView filterStationName = (TextView) vv
                        .findViewById(R.id.filterStationNameId);
                final TextView toSchArrival = (TextView) vv
                        .findViewById(R.id.toschArrivalID);
                final TextView toExpArrival = (TextView) vv
                        .findViewById(R.id.toExpArrivalID);
                final TextView destinationSchArrival = (TextView) vv
                        .findViewById(R.id.destSchArrivalID);
                final TextView destinationExpArrival = (TextView) vv
                        .findViewById(R.id.destExpArrivalID);

                final TextView schDepart = (TextView) vv
                        .findViewById(R.id.schDepartID);
                final TextView expDepart = (TextView) vv
                        .findViewById(R.id.expDepartID);

                final ImageView alertStatusImage = (ImageView) vv
                        .findViewById(R.id.alertStatus);

                final String platformValue = deparaturedaseboarddto.get(position).getPlatformNo();
                final String toSchArrivalValue= deparaturedaseboarddto.get(position).getToSchArrival();
                final String toExpArrivalValue= deparaturedaseboarddto.get(position).getToExpArrival();
                final String destSchArrivalValue= deparaturedaseboarddto.get(position).getDestSchArrival();
                final String destExpArrivalValue= deparaturedaseboarddto.get(position).getDestExpArrival();


                final String schDepartValue= deparaturedaseboarddto.get(position).getSchDepart();
                final String expDepartValue= deparaturedaseboarddto.get(position).getExpDepart();
               String destinationStationNamValue= deparaturedaseboarddto.get(position).getDestinationStation().getStationName();
               String filterStationNameValue= deparaturedaseboarddto.get(position).getFilteredStation().getStationName();

               if(deparaturedaseboarddto.get(position).getAlertsId()!=0){
                   alertStatusImage.setVisibility(View.VISIBLE);
               }else {
                   alertStatusImage.setVisibility(View.INVISIBLE);  
               }


                platFormName.setText(platformValue);

                destinationName.setText(destinationStationNamValue);
                filterStationName.setText(filterStationNameValue);
                toSchArrival.setText(toSchArrivalValue);
                toExpArrival.setText(toExpArrivalValue);
                destinationSchArrival.setText(destSchArrivalValue);
                destinationExpArrival.setText(destExpArrivalValue);
                schDepart.setText(schDepartValue);
                expDepart.setText(expDepartValue);



                if (position%2==0) {
                    Log.d("--------", "old");
                    vv.setBackgroundColor(Color.parseColor("#FFFFFF"));
                } else {
                    Log.d("--------", "even");

                    vv.setBackgroundColor(Color.parseColor("#f3f3f3"));
                }
                return vv;
         }
    }

您好 我得到空指针异常。我知道当我尝试从null值访问某些东西时会出现这种情况。但我不知道如何删除它。我会解释我的问题。我有一个列表视图我需要在listview上显示来自service.inctully的一个条件 if(!ConstantVariable.isfilterOccured){ intially isfilterOccured 是假的。这是在这个条件下进行的

if (v == null) {
                    LayoutInflater mInflater = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = mInflater.inflate(R.layout.row_listitem, null);
                }

但是,当我打电话给另一个网络服务 if(!ConstantVariable.isfilterOccured){ intially isfilterOccured true 为什么它不会进入条件这就是我得到空的原因。

View vv = convertView;

                    if (vv == null) {
                        LayoutInflater mInflater = (LayoutInflater) context
                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        vv = mInflater.inflate(R.layout.filterdashboard, null);
                    }

然后找不到这个项目

  final TextView platFormName = (TextView) vv
                            .findViewById(R.id.platFormId);

请参阅更新:

11-02 06:42:30.634: E/AndroidRuntime(743): FATAL EXCEPTION: main
11-02 06:42:30.634: E/AndroidRuntime(743): java.lang.NullPointerException
11-02 06:42:30.634: E/AndroidRuntime(743):  at com.firstgroup.components.DepartureListAdapter.getView(DepartureListAdapter.java:156)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.widget.AbsListView.obtainView(AbsListView.java:2012)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.widget.ListView.onMeasure(ListView.java:1155)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.view.View.measure(View.java:12603)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:579)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.view.View.measure(View.java:12603)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.view.View.measure(View.java:12603)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.view.View.measure(View.java:12603)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
11-02 06:42:30.634: E/AndroidRuntime(743):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2072)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.view.View.measure(View.java:12603)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1044)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.os.Looper.loop(Looper.java:137)
11-02 06:42:30.634: E/AndroidRuntime(743):  at android.app.ActivityThread.main(ActivityThread.java:4340)
11-02 06:42:30.634: E/AndroidRuntime(743):  at java.lang.reflect.Method.invokeNative(Native Method)
11-02 06:42:30.634: E/AndroidRuntime(743):  at java.lang.reflect.Method.invoke(Method.java:511)
11-02 06:42:30.634: E/AndroidRuntime(743):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-02 06:42:30.634: E/AndroidRuntime(743):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-02 06:42:30.634: E/AndroidRuntime(743):  at dalvik.system.NativeStart.main(Native Method)

我在这一行得到例外

platFormName.setText(platformValue);

因为 platFormName 为空。我认为它是null,因为它不使用此xml

LayoutInflater mInflater = (LayoutInflater) context
                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        vv = mInflater.inflate(R.layout.filterdashboard, null);

此filterdashboard.xml中定义的platFormId

像那样工作

public class DepartureListAdapter extends BaseAdapter{
    ArrayList<Deparaturedaseboarddto> deparaturedaseboarddto;
    private Context context;
    public DepartureListAdapter( Context context, ArrayList<Deparaturedaseboarddto> deparaturedaseboarddto){
        this.deparaturedaseboarddto=deparaturedaseboarddto;
        this.context = context;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
         if(!ConstantVariable.isfilterOccured){
             View v = convertView;
            if (v == null) {
                LayoutInflater mInflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = mInflater.inflate(R.layout.row_listitem, null);
            }

            final TextView platFormName = (TextView) v
                    .findViewById(R.id.item_platform);

            final TextView schDepart = (TextView) v
                    .findViewById(R.id.item_schDepart);
            final TextView expDepart = (TextView) v
                    .findViewById(R.id.item_expectdepart);
            final TextView arrival = (TextView) v
                    .findViewById(R.id.item_arrival);
            final TextView exparrival = (TextView) v
                    .findViewById(R.id.item_expertarrival);
            final TextView stationName = (TextView) v
                    .findViewById(R.id.item_stationName);
            final ImageView alertStatusImage = (ImageView) v
                    .findViewById(R.id.alertStatus);

            final String platformValue = deparaturedaseboarddto.get(position).getPlatformNo();
            final String schDepartValue= deparaturedaseboarddto.get(position).getSchDepart();
            final String schExpectValue= deparaturedaseboarddto.get(position).getExpDepart();
            final String arrivalValue= deparaturedaseboarddto.get(position).getDestSchArrival();
            final String exparrivalValue= deparaturedaseboarddto.get(position).getDestSchArrival();
           String stationNameValue= deparaturedaseboarddto.get(position).getDestinationStation().getStationName();
           if(deparaturedaseboarddto.get(position).getAlertsId()!=0){
               alertStatusImage.setVisibility(View.VISIBLE);
           }else {
               alertStatusImage.setVisibility(View.INVISIBLE);  
           }


            platFormName.setText(platformValue);

            schDepart.setText(schDepartValue);
            expDepart.setText(schExpectValue);
            arrival.setText(arrivalValue);
            exparrival.setText(exparrivalValue);
            stationName.setText(stationNameValue);
            if (position%2==0) {
                Log.d("--------", "old");
                v.setBackgroundColor(Color.parseColor("#FFFFFF"));
            } else {
                Log.d("--------", "even");

                v.setBackgroundColor(Color.parseColor("#f3f3f3"));
            }
            return v;
         }else {
             View vv = convertView;

//              if (vv == null) {
//                  LayoutInflater mInflater = (LayoutInflater) context
//                          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//                  vv = mInflater.inflate(R.layout.filterdashboard, null);
//              }
             LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              vv = mInflater.inflate(R.layout.filterdashboard, null);

                final TextView platFormName = (TextView) vv
                        .findViewById(R.id.platFormId);

                final TextView destinationName = (TextView) vv
                        .findViewById(R.id.destinationStationName);
                final TextView filterStationName = (TextView) vv
                        .findViewById(R.id.filterStationNameId);
                final TextView toSchArrival = (TextView) vv
                        .findViewById(R.id.toschArrivalID);
                final TextView toExpArrival = (TextView) vv
                        .findViewById(R.id.toExpArrivalID);
                final TextView destinationSchArrival = (TextView) vv
                        .findViewById(R.id.destSchArrivalID);
                final TextView destinationExpArrival = (TextView) vv
                        .findViewById(R.id.destExpArrivalID);

                final TextView schDepart = (TextView) vv
                        .findViewById(R.id.schDepartID);
                final TextView expDepart = (TextView) vv
                        .findViewById(R.id.expDepartID);

                final ImageView alertStatusImage = (ImageView) vv
                        .findViewById(R.id.alertStatusImage);

                final String platformValue = deparaturedaseboarddto.get(position).getPlatformNo();
                final String toSchArrivalValue= deparaturedaseboarddto.get(position).getToSchArrival();
                final String toExpArrivalValue= deparaturedaseboarddto.get(position).getToExpArrival();
                final String destSchArrivalValue= deparaturedaseboarddto.get(position).getDestSchArrival();
                final String destExpArrivalValue= deparaturedaseboarddto.get(position).getDestExpArrival();


                final String schDepartValue= deparaturedaseboarddto.get(position).getSchDepart();
                final String expDepartValue= deparaturedaseboarddto.get(position).getExpDepart();
               String destinationStationNamValue= deparaturedaseboarddto.get(position).getDestinationStation().getStationName();
               String filterStationNameValue= deparaturedaseboarddto.get(position).getFilteredStation().getStationName();

               if(deparaturedaseboarddto.get(position).getAlertsId()!=0){
                   alertStatusImage.setVisibility(View.VISIBLE);
               }else {
                   alertStatusImage.setVisibility(View.INVISIBLE);  
               }


                platFormName.setText(platformValue);

                destinationName.setText(destinationStationNamValue);
                filterStationName.setText(filterStationNameValue);
                toSchArrival.setText(toSchArrivalValue);
                toExpArrival.setText(toExpArrivalValue);
                destinationSchArrival.setText(destSchArrivalValue);
                destinationExpArrival.setText(destExpArrivalValue);
                schDepart.setText(schDepartValue);
                expDepart.setText(expDepartValue);



                if (position%2==0) {
                    Log.d("--------", "old");
                    vv.setBackgroundColor(Color.parseColor("#FFFFFF"));
                } else {
                    Log.d("--------", "even");

                    vv.setBackgroundColor(Color.parseColor("#f3f3f3"));
                }
                return vv;
         }
    }

    public void referhList( ArrayList<Deparaturedaseboarddto> deparaturedaseboarddto){
        this.deparaturedaseboarddto=deparaturedaseboarddto;

        notifyDataSetChanged();
    }

}


private void showFilterWebDashBoard(String response) {
        Log.d("--",response);
        ConstantVariable.isfilterOccured=true;
        DepartureHolder localdata = new Gson().fromJson(response, DepartureHolder.class);
        data=localdata;

        adapter.referhList(data.getData());
    }

1 个答案:

答案 0 :(得分:0)

我建议您为包含当前布局 R.layout.row_listitem R.layout.filterdashboard 的列表项创建布局(所以现在你'll R.layout.merged_layouts ),并基于

ConstantVariable.isfilterOccured

您可以更改相应容器布局的可见性,并保持视图的可重用性(您甚至可以添加ViewHolder模式以提高性能)。

希望它有所帮助。