我正在尝试使用JSON构建一个具有基本登录注销功能的应用程序,但是每当我登录它时崩溃。根据logcat,错误来自我的登录类,但我无法确定确切的原因。什么似乎是问题?
Logcat报告
09-20 15:15:33.476: E/AndroidRuntime(2099): FATAL EXCEPTION: main
09-20 15:15:33.476: E/AndroidRuntime(2099): Process: com.learn2crack.tab, PID: 2099
09-20 15:15:33.476: E/AndroidRuntime(2099): java.lang.NullPointerException
09-20 15:15:33.476: E/AndroidRuntime(2099): at com.learn2crack.tab.Login$ProcessLogin.onPostExecute(Login.java:201)
09-20 15:15:33.476: E/AndroidRuntime(2099): at com.learn2crack.tab.Login$ProcessLogin.onPostExecute(Login.java:1)
09-20 15:15:33.476: E/AndroidRuntime(2099): at android.os.AsyncTask.finish(AsyncTask.java:632)
09-20 15:15:33.476: E/AndroidRuntime(2099): at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-20 15:15:33.476: E/AndroidRuntime(2099): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
09-20 15:15:33.476: E/AndroidRuntime(2099): at android.os.Handler.dispatchMessage(Handler.java:102)
09-20 15:15:33.476: E/AndroidRuntime(2099): at android.os.Looper.loop(Looper.java:136)
09-20 15:15:33.476: E/AndroidRuntime(2099): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-20 15:15:33.476: E/AndroidRuntime(2099): at java.lang.reflect.Method.invokeNative(Native Method)
09-20 15:15:33.476: E/AndroidRuntime(2099): at java.lang.reflect.Method.invoke(Method.java:515)
09-20 15:15:33.476: E/AndroidRuntime(2099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-20 15:15:33.476: E/AndroidRuntime(2099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-20 15:15:33.476: E/AndroidRuntime(2099): at dalvik.system.NativeStart.main(Native Method)
我的login.java代码
package com.learn2crack.tab;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
//import com.learn2crack.library.DatabaseHandler;
import com.learn2crack.library.UserFunctions;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Login extends Activity {
Button btnLogin;
Button Btnregister;
Button passreset;
EditText inputusername;
EditText inputPassword;
private TextView loginErrorMsg;
/**
* Called when the activity is first created.
*/
private static String KEY_SUCCESS = "success";
private static String KEY_UID = "uid";
private static String KEY_USERNAME = "uname";
private static String KEY_FIRSTNAME = "fname";
private static String KEY_LASTNAME = "lname";
private static String KEY_username = "username";
private static String KEY_CREATED_AT = "created_at";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
inputusername = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.pword);
Btnregister = (Button) findViewById(R.id.registerbtn);
btnLogin = (Button) findViewById(R.id.login);
loginErrorMsg = (TextView) findViewById(R.id.loginErrorMsg);
Btnregister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Register.class);
startActivityForResult(myIntent, 0);
finish();
}});
/**
* Login button click event
* A Toast is set to alert when the username and Password field is empty
**/
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if ( ( !inputusername.getText().toString().equals("")) && ( !inputPassword.getText().toString().equals("")) )
{
NetAsync(view);
}
else if ( ( !inputusername.getText().toString().equals("")) )
{
Toast.makeText(getApplicationContext(),
"Password field empty", Toast.LENGTH_SHORT).show();
}
else if ( ( !inputPassword.getText().toString().equals("")) )
{
Toast.makeText(getApplicationContext(),
"username field empty", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),
"username and Password field are empty", Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* Async Task to check whether internet connection is working.
**/
private class NetCheck extends AsyncTask<String,String,Boolean>
{
private ProgressDialog nDialog;
@Override
protected void onPreExecute(){
super.onPreExecute();
nDialog = new ProgressDialog(Login.this);
nDialog.setTitle("Checking Network");
nDialog.setMessage("Loading..");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
/**
* Gets current device state and checks for working internet connection by trying Google.
**/
@Override
protected Boolean doInBackground(String... args){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(3000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
return true;
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
@Override
protected void onPostExecute(Boolean th){
if(th == true){
nDialog.dismiss();
new ProcessLogin().execute();
}
else{
nDialog.dismiss();
loginErrorMsg.setText("Error in Network Connection");
}
}
}
/**
* Async Task to get and send data to My Sql database through JSON respone.
**/
private class ProcessLogin extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
String username,password;
@Override
protected void onPreExecute() {
super.onPreExecute();
inputusername = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.pword);
username = inputusername.getText().toString();
password = inputPassword.getText().toString();
pDialog = new ProgressDialog(Login.this);
pDialog.setTitle("Contacting Servers");
pDialog.setMessage("Logging in ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(username, password);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
try {
if (json.getString(KEY_SUCCESS) != null) {
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
pDialog.setMessage("Loading User Space");
pDialog.setTitle("Getting Data");
//DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
/**
* Clear all previous data in SQlite database.
**/
UserFunctions logout = new UserFunctions();
logout.logoutUser(getApplicationContext());
//db.addUser(json_user.getString(KEY_FIRSTNAME),json_user.getString(KEY_LASTNAME),json_user.getString(KEY_username),json_user.getString(KEY_USERNAME),json_user.getString(KEY_UID),json_user.getString(KEY_CREATED_AT));
/**
*If JSON array details are stored in SQlite it launches the User Panel.
**/
Intent upanel = new Intent(getApplicationContext(), MainActivity.class);
upanel.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pDialog.dismiss();
startActivity(upanel);
/**
* Close Login Screen
**/
finish();
}else{
pDialog.dismiss();
loginErrorMsg.setText("Incorrect username/password");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public void NetAsync(View view){
new NetCheck().execute();
}
}
UserFunctions.java代码段
public JSONObject loginUser(String username, String password){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
return json;
答案 0 :(得分:0)
第194行的userFunction.loginUser(username, password);
似乎正在返回null
。
检查loginUser
中的UserFunctions
方法。