当我按下登录按钮时发生java.lang.NullPointerException,它假设从数据库中检索数据并将其放入我在代码中定义的数组列表中。任何人都知道如何解决这个问题?提前谢谢:D
package info.androidhive.materialdesign.activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
import info.androidhive.materialdesign.R;
enter code here
public class loginFragment extends Fragment {
private TextView textViewForgotPass, textView2;
private Button btnLogin;
private EditText editTextEmail, editTextPassword;
List<UserAccount> uaList;
private ProgressDialog pDialog;
private Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_login, container, false);
textViewForgotPass= (TextView)rootView.findViewById(R.id.textViewForgotPass);
textViewForgotPass.setTextColor(0xAA81DEFD);
editTextEmail = (EditText)rootView.findViewById(R.id.editTextEmail);
editTextPassword = (EditText)rootView.findViewById(R.id.editTextPassword);
btnLogin=(Button)rootView.findViewById(R.id.buttonLogin);
btnLogin.setOnClickListener(loginListener);
textViewForgotPass.setOnClickListener(forgotPassListener);
pDialog = new ProgressDialog(loginFragment.this.getContext());
textView2=(TextView)rootView.findViewById(R.id.textView2);
return rootView;
}
View.OnClickListener loginListener = new View.OnClickListener() {
@Override
public void onClick(final View v) {
readUser();
}
};
View.OnClickListener forgotPassListener = new View.OnClickListener() {
@Override
public void onClick(final View v) {
Toast.makeText(loginFragment.this.getContext(), "Forgot Password?", Toast.LENGTH_SHORT).show();
}
};
private void readUser() {
try {
// Check availability of network connection.
ConnectivityManager connMgr = (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
Boolean isConnected = networkInfo != null && networkInfo.isConnectedOrConnecting();
if (isConnected) {
//downloadUser().execute(getResources().getString(R.string.get_user_url));
downloadUser(this.getActivity(), getResources().getString(R.string.get_user_url));
} else {
Toast.makeText(loginFragment.this.getContext(), "Network is NOT available",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(loginFragment.this.getContext(),
"Error reading record:" + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
private void downloadUser(Context context, String url) {
//mPostCommentResponse.requestStarted();
RequestQueue queue = Volley.newRequestQueue(context);
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
if (!pDialog.isShowing())
pDialog.show();
try{
//Clear list
uaList.clear();
for(int i=0; i < response.length();i++){
JSONObject userAccResponse = (JSONObject) response.get(i);
String email = userAccResponse.getString("email");
String password = userAccResponse.getString("password");
String name = userAccResponse.getString("name");
String address = userAccResponse.getString("address");
String phonenum = userAccResponse.getString("phonenum");
UserAccount user = new UserAccount();
user.setEmail(email);
user.setPassword(password);
user.setName(name);
user.setAddress(address);
user.setPhoneNum(phonenum);
uaList.add(user);
textView2.setText(uaList.get(0).getEmail());
}
if (pDialog.isShowing())
pDialog.dismiss();
}catch (JSONException e){
Toast.makeText(loginFragment.this.getContext(), "Erroraaaa:" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(loginFragment.this.getContext(), "Errorssss:" + volleyError.getMessage(), Toast.LENGTH_SHORT).show();
if (pDialog.isShowing())
pDialog.dismiss();
}
});
queue.add(jsonObjectRequest);
}
}
这是错误日志
12-15 18:38:01.618 12060-12060/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at info.androidhive.materialdesign.activity.loginFragment$3.onResponse(loginFragment.java:107)
at info.androidhive.materialdesign.activity.loginFragment$3.onResponse(loginFragment.java:99)
at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4624)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您的uaList
为空,而您正试图清除它。
首先使用uaList = new Arraylist<>();