设置ListView适配器时的Android NullPointerException

时间:2018-06-14 19:37:09

标签: android nullpointerexception imageview listview-adapter

我遇到一个令人沮丧的问题,我在将自定义listview适配器设置为listview时遇到空指针异常。我无法弄清楚什么是null。我在应用程序的另一部分做了类似的事情,并没有任何问题。我不确定这个问题是否与这次使用图像有关?

以下是我的代码:

我正在获取数据并启动listview的片段类,(我已经成功测试了我的数据)

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;


public class FinalUnitStatusFragment extends Fragment {

Context finalUnitStatusContext;
Activity finalUnitStatusActivity;

ProgressDialog progressDialog;

//FinalUnitStatusListViewAdapter finalUnitStatusLVAdapter;

String ReadOnly;
String LexaUser;
String Password;
String SearchValue;

String finalResultFinalUS;
String HttpURLFinalUS = "https://[my/path/to/page]/getFinalUnitStatus.php";
HashMap<String, String> hashMapFinalUS = new HashMap<>();
HttpParse httpParseFinalUS = new HttpParse();

String[] Uid;
String[] Ustatus;
String[] Uqastandard;
String[] Ufinaloutcome;
String[] Ugencause;
String[] Uspeccause;
String[] Usurface;
String[] Uorigin;
String[] Ulexauser;
String[] Ulexausercomment;
String[] Urejphoto1String;
Bitmap[] Urejphoto1;
String[] Ulexausertimestamp;
String[] Uqauser;
String[] Uqacomment;
String[] Urejphoto2String;
Bitmap[] Urejphoto2;
String[] Uqausertimestamp;

ListView finalUSListView;

public FinalUnitStatusFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_siblings, container, false);

    finalUSListView = (ListView) view.findViewById(R.id.FinalUnitStatusList);

    finalUnitStatusContext = getContext();

    finalUnitStatusActivity = getActivity();

    if (getArguments() != null) {
        SearchValue = getArguments().getString("SearchValue");
        LexaUser = getArguments().getString("LexaUser");
        getFinalUnitStatus(SearchValue, LexaUser);
    } else {
        Toast.makeText(finalUnitStatusContext, "Arguments Null", Toast.LENGTH_LONG).show();
    }

    return view;
}

public void getFinalUnitStatus(String searchInput, String lexaUser) {

    class FinalUnitStatusClass extends AsyncTask<String,Void,String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            progressDialog = ProgressDialog.show(finalUnitStatusContext, "Loading Data", null, true, true);
        }

        @Override
        protected void onPostExecute(String httpResponseMsg) {

            super.onPostExecute(httpResponseMsg);

            if (httpResponseMsg != null) {

                try {

                    JSONArray json = new JSONArray(httpResponseMsg);

                    Uid = new String[json.length()];
                    Ustatus = new String[json.length()];
                    Uqastandard = new String[json.length()];
                    Ufinaloutcome = new String[json.length()];
                    Ugencause = new String[json.length()];
                    Uspeccause = new String[json.length()];
                    Usurface = new String[json.length()];
                    Uorigin = new String[json.length()];
                    Ulexauser = new String[json.length()];
                    Ulexausercomment = new String[json.length()];
                    Urejphoto1String = new String[json.length()];
                    Urejphoto1 = new Bitmap[json.length()];
                    Ulexausertimestamp = new String[json.length()];
                    Uqauser = new String[json.length()];
                    Uqacomment = new String[json.length()];
                    Urejphoto2String = new String[json.length()];
                    Urejphoto2 = new Bitmap[json.length()];
                    Uqausertimestamp = new String[json.length()];

                    for (int i = 0; i < json.length(); i++) {

                        JSONObject object = json.getJSONObject(i);

                        Uid[i] = object.getString("statusID");
                        Ustatus[i] = object.getString("unitStatus");
                        Uqastandard[i] = object.getString("qaStandard");
                        Ufinaloutcome[i] = object.getString("finalOutcome");
                        Ugencause[i] = object.getString("genCause");
                        Uspeccause[i] = object.getString("specCause");
                        Usurface[i] = object.getString("surface");
                        Uorigin[i] = object.getString("origin");
                        Ulexauser[i] = object.getString("lexaUser");
                        Ulexausercomment[i] = object.getString("initComment");
                        Urejphoto1String[i] = object.getString("rejImage1");
                        Ulexausertimestamp[i] = object.getString("addDate");
                        Uqauser[i] = object.getString("qaUser");
                        Uqacomment[i] = object.getString("qaComment");
                        Urejphoto2String[i] = object.getString("rejImage2");
                        Uqausertimestamp[i] = object.getString("qaAddDate");

                        if (Urejphoto1String.equals("Could not find image.")) {
                            String placeholder = "";
                        } else {
                            byte[] decodedString = Base64.decode(Urejphoto1String[i], Base64.DEFAULT);
                            Urejphoto1[i] = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                        }
                        if (Urejphoto2String.equals("Could not find image.")) {
                            String placeholder = "";
                        } else {
                            byte[] decodedString2 = Base64.decode(Urejphoto2String[i], Base64.DEFAULT);
                            Urejphoto2[i] = BitmapFactory.decodeByteArray(decodedString2, 0, decodedString2.length);
                        }
                    }

                    FinalUnitStatusListViewAdapter finalUSLVAdapter = new FinalUnitStatusListViewAdapter(finalUnitStatusActivity, Uid, Ustatus, Uqastandard, Ufinaloutcome, Ugencause, Uspeccause, Usurface, Uorigin, Ulexauser, Ulexausercomment, Urejphoto1, Ulexausertimestamp, Uqauser, Uqacomment, Urejphoto2, Uqausertimestamp);
                    if (finalUSLVAdapter != null) {
                        finalUSListView.setAdapter(finalUSLVAdapter);
                    } else {
                        Toast.makeText(finalUnitStatusContext, "Error: ListView Adapter is null", Toast.LENGTH_LONG).show();
                    }

                } catch (JSONException e) {
                    Log.e("JSONException", "Error: " + e.toString());
                    Toast.makeText(finalUnitStatusContext, "Error: " + e.toString(), Toast.LENGTH_LONG).show();
                } // catch (JSONException e)

                progressDialog.dismiss();

            } else {
                progressDialog.dismiss();
                Toast.makeText(finalUnitStatusContext, "HttpResponseMsg is null.", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        protected String doInBackground(String... params) {

            hashMapFinalUS.put("searchinput", params[0]);
            hashMapFinalUS.put("lexauser", params[1]);

            finalResultFinalUS = httpParseFinalUS.postRequest(hashMapFinalUS, HttpURLFinalUS);

            return finalResultFinalUS;
        }
    }
    FinalUnitStatusClass finalUnitStatusClass = new FinalUnitStatusClass();
    finalUnitStatusClass.execute(searchInput, lexaUser);
}

}

我的自定义适配器:

package com.ltiws.www.lexaapp;

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FinalUnitStatusListViewAdapter extends BaseAdapter {

    Activity context;
    String Uid[];
    String Ustatus[];
    String Uqastandard[];
    String Uresolution[];
    String Ugencause[];
    String UspecCause[];
    String Usurface[];
    String Uorigin[];
    String Ulexauser[];
    String Uinitcomment[];
    Bitmap Urejphoto1[];
    String Ulexausertimestamp[];
    String Uqauser[];
    String Uqacomment[];
    Bitmap Urejphoto2[];
    String Uqausertimestamp[];

 public FinalUnitStatusListViewAdapter(Activity context, String[] Uid, String[] Ustatus, String[] Uqastandard, String[] Uresolution, String[] Ugencause, String[] UspecCause, String[] Usurface, String[] Uorigin, String[] Ulexauser, String[] Uinitcomment, Bitmap[] Urejphoto1, String[] Ulexausertimestamp, String[] Uqauser, String[] Uqacomment, Bitmap[] Urejphoto2, String[] Uqausertimestamp) {
    super();
    this.context = context;
    this.Uid = Uid;
    this.Ustatus = Ustatus;
    this.Uqastandard = Uqastandard;
    this.Uresolution = Uresolution;
    this.Ugencause = Ugencause;
    this.UspecCause = UspecCause;
    this.Usurface = Usurface;
    this.Uorigin = Uorigin;
    this.Ulexauser = Ulexauser;
    this.Uinitcomment = Uinitcomment;
    this.Urejphoto1 = Urejphoto1;
    this.Ulexausertimestamp = Ulexausertimestamp;
    this.Uqauser = Uqauser;
    this.Uqacomment = Uqacomment;
    this.Urejphoto2 = Urejphoto2;
    this.Uqausertimestamp = Uqausertimestamp;
    }

public int getCount() {
    // TODO Auto-generated method stub
    return Uid.length;
    }

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

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

private class ViewHolder {
  TextView txtViewUid;
  TextView txtViewStatus;
  TextView txtViewQAStandard;
  TextView txtViewResolution;
  TextView txtViewGenCause;
  TextView txtViewSpecCause;
  TextView txtViewSurface;
  TextView txtViewOrigin;
  TextView txtViewLexaUser;
  TextView txtViewInitComment;
  ImageView imageViewRejPhoto1;
  TextView txtViewLexaUserTimestamp;
  TextView txtViewQAUser;
  TextView txtViewQAComment;
  ImageView imageViewRejPhoto2;
  TextView txtViewQAUserTimestamp;

}

public View getView(int position, View convertView2, ViewGroup parent)
{
    // TODO Auto-generated method stub
    FinalUnitStatusListViewAdapter.ViewHolder holder2;
    LayoutInflater inflater =  context.getLayoutInflater();

    if (convertView2 == null)
    {
        convertView2 = inflater.inflate(R.layout.finalunitstatus_list, null);
        holder2 = new FinalUnitStatusListViewAdapter.ViewHolder();
        holder2.txtViewUid = (TextView) convertView2.findViewById(R.id.FinalStatusID);
        holder2.txtViewStatus = (TextView) convertView2.findViewById(R.id.FinalStatus);
        holder2.txtViewQAStandard = (TextView) convertView2.findViewById(R.id.FinalQAStandard);
        holder2.txtViewResolution = (TextView) convertView2.findViewById(R.id.FinalResolution);
        holder2.txtViewGenCause = (TextView) convertView2.findViewById(R.id.FinalGenCause);
        holder2.txtViewSpecCause = (TextView) convertView2.findViewById(R.id.FinalSpecCause);
        holder2.txtViewSurface = (TextView) convertView2.findViewById(R.id.FinalSurface);
        holder2.txtViewOrigin = (TextView) convertView2.findViewById(R.id.FinalOrigin);
        holder2.txtViewLexaUser = (TextView) convertView2.findViewById(R.id.FinalLexaUser);
        holder2.txtViewInitComment = (TextView) convertView2.findViewById(R.id.FinalLexaUserComment);
        holder2.imageViewRejPhoto1 = (ImageView) convertView2.findViewById(R.id.FinalLexaUserPhoto);
        holder2.txtViewLexaUserTimestamp = (TextView) convertView2.findViewById(R.id.FinalLexaUserTimestamp);
        holder2.txtViewQAUser = (TextView) convertView2.findViewById(R.id.FinalQAUser);
        holder2.txtViewQAComment = (TextView) convertView2.findViewById(R.id.FinalQAUserComment);
        holder2.imageViewRejPhoto2 = (ImageView) convertView2.findViewById(R.id.FinalQAUserPhoto);
        holder2.txtViewQAUserTimestamp = (TextView) convertView2.findViewById(R.id.FinalQAUserTimestamp);
        convertView2.setTag(holder2);
    }
    else
    {
        holder2 = (FinalUnitStatusListViewAdapter.ViewHolder) convertView2.getTag();
    }

    holder2.txtViewUid.setText(Uid[position]);
    holder2.txtViewStatus.setText(Ustatus[position]);
    holder2.txtViewQAStandard.setText(Uqastandard[position]);
    holder2.txtViewResolution.setText(Uresolution[position]);
    holder2.txtViewGenCause.setText(Ugencause[position]);
    holder2.txtViewSpecCause.setText(UspecCause[position]);
    holder2.txtViewSurface.setText(Usurface[position]);
    holder2.txtViewOrigin.setText(Uorigin[position]);
    holder2.txtViewLexaUser.setText(Ulexauser[position]);
    holder2.txtViewInitComment.setText(Uinitcomment[position]);
    holder2.imageViewRejPhoto1.setImageBitmap(Urejphoto1[position]);
    holder2.txtViewLexaUserTimestamp.setText(Ulexausertimestamp[position]);
    holder2.txtViewQAUser.setText(Uqauser[position]);
    holder2.txtViewQAComment.setText(Uqacomment[position]);
    holder2.imageViewRejPhoto2.setImageBitmap(Urejphoto2[position]);
    holder2.txtViewQAUserTimestamp.setText(Uqausertimestamp[position]);

    return convertView2;
  }
}

我的堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: [package], PID: 25971
              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
                  at com.ltiws.www.lexaapp.FinalUnitStatusFragment$1FinalUnitStatusClass.onPostExecute(FinalUnitStatusFragment.java:168)
                  at com.ltiws.www.lexaapp.FinalUnitStatusFragment$1FinalUnitStatusClass.onPostExecute(FinalUnitStatusFragment.java:92)
                  at android.os.AsyncTask.finish(AsyncTask.java:660)
                  at android.os.AsyncTask.-wrap1(AsyncTask.java)
                  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:159)
                  at android.app.ActivityThread.main(ActivityThread.java:6097)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

我的代码第168行导致中断:

finalUSListView.setAdapter(finalUSLVAdapter);

我似乎无法弄清楚我的适配器中的null是什么......

感谢所有帮助!谢谢!

2 个答案:

答案 0 :(得分:3)

问题似乎是你的listview finalUSListView而不是适配器。 您确定使用正确的ID来夸大此视图吗?你能检查一下它是否被正确实例化了吗?

答案 1 :(得分:1)

Vinicius Sant'Ana是对的,你的listView为null,因为你使用大写的id绑定id

(ListView) view.findViewById(R.id.FinalUnitStatusList);

将xml中的引用和代码重命名为final_unit_status_list而不是

如果您想了解更多信息,请选中此项:

Why is it not possible to use uppercase in naming resources in android?

下次好的做法是在调试模式下启动应用程序并在错误行上添加断点,您将很快看到列表视图为空