2014-11-02 60 views
1
@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; 
     } 
    } 

嗨 我得到空指針異常。我知道這會來,當我試圖訪問空值的東西。但我不知道如何刪除。我會解釋我的問題。我有一個我需要證明這是從service.Actully即將來臨的ListView數據我需要一個條件如果(!ConstantVariable.isfilterOccured)列表視圖{ intially isfilterOccured是假的。它進入這個狀態裏面如何在android中刪除null pointerException?

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

但當我打電話給另一個webservice if(!ConstantVariable.isfil terOccured){ Informed isfilterOccured is true爲什麼它沒有進入內部條件,因爲我得到null。

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爲空。我覺得,因爲它的ID未使用XML

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

platFormId在此filterdashboard.xml

定義的那樣

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()); 
    } 
工作爲null
+1

你可以添加引發NPE的文字嗎?從你的問題中不清楚哪個對象是空的。 – juh 2014-11-02 00:48:11

+0

請發佈您的logcat輸出。 – 2014-11-02 00:55:01

+0

好的我會告訴你 – user944513 2014-11-02 01:12:00

回答

0

我建議你爲包含當前佈局的列表項目創建一個佈局R.layout.row_listitemR.layout.filterdashboard(所以現在你會膨脹R.layout.merged_layouts),以及基於

ConstantVariable.isfilterOccured 

你可以改變相應的容器佈局的知名度並保持視圖的可重用性(甚至可以添加ViewHolder模式來提高性能)。

希望它有幫助。

+0

請檢查更新..我能夠做到這一點..它的工作..這是一個最好的方式來那樣做 – user944513 2014-11-02 02:44:18

+0

更好的方法是我的建議。您可以重新使用視圖,甚至添加ViewHolder模式以提高性能。將兩個佈局添加到單個xml文件,然後根據變量切換可見性。 – 2014-11-02 02:57:45

+0

你可以請舉例其實我是新的android – user944513 2014-11-02 03:01:53