這是我的logcat嘗試通過JSON做登錄,但有運行時錯誤
12-08 13:37:39.849: W/dalvikvm(5757): threadid=1: thread exiting with uncaught exception (group=0x40018578)
12-08 13:37:39.859: E/AndroidRuntime(5757): FATAL EXCEPTION: main
12-08 13:37:39.859: E/AndroidRuntime(5757): java.lang.NullPointerException
12-08 13:37:39.859: E/AndroidRuntime(5757): at com.pmss.Login$1.onClick(Login.java:55)
12-08 13:37:39.859: E/AndroidRuntime(5757): at android.view.View.performClick(View.java:2485)
12-08 13:37:39.859: E/AndroidRuntime(5757): at android.view.View$PerformClick.run(View.java:9080)
12-08 13:37:39.859: E/AndroidRuntime(5757): at android.os.Handler.handleCallback(Handler.java:587)
12-08 13:37:39.859: E/AndroidRuntime(5757): at android.os.Handler.dispatchMessage(Handler.java:92)
12-08 13:37:39.859: E/AndroidRuntime(5757): at android.os.Looper.loop(Looper.java:130)
12-08 13:37:39.859: E/AndroidRuntime(5757): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-08 13:37:39.859: E/AndroidRuntime(5757): at java.lang.reflect.Method.invokeNative(Native Method)
12-08 13:37:39.859: E/AndroidRuntime(5757): at java.lang.reflect.Method.invoke(Method.java:507)
12-08 13:37:39.859: E/AndroidRuntime(5757): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-08 13:37:39.859: E/AndroidRuntime(5757): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-08 13:37:39.859: E/AndroidRuntime(5757): at dalvik.system.NativeStart.main(Native Method)
這是我Login.java
public class Login extends ActionBarActivity implements OnClickListener {
private Button login,register;
private EditText email,password;
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://192.168.1.14:1234/PMSS/login.php";
//JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login = (Button) findViewById(R.id.login);
register = (Button) findViewById(R.id.register);
email = (EditText) findViewById(R.id.emailtext);
password = (EditText) findViewById(R.id.passwordtext);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AttemptLogin().execute();
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Login.this, Register.class);
startActivity(intent);
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// For the main activity, make sure the app icon in the action bar
// does not behave as a button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
/*case R.id.login:
new AttemptLogin().execute();
break;
case R.id.register:
Intent i = new Intent(Login.this, Register.class);
startActivity(i);
break;
*/
default:
break;
}
}
//AsyncTask is a seperate thread than the thread that runs the GUI
//Any type of networking should be done with asynctask.
class AttemptLogin extends AsyncTask<String, String, String> {
//three methods get called, first preExecture, then do in background, and once do
//in back ground is completed, the onPost execture method will be called.
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
int success;
String Username = email.getText().toString();
String Password = password.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", Username));
params.add(new BasicNameValuePair("password", Password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
System.out.print("Here");
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(Login.this, MainMenu.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
正如我應該提到,當我點擊登錄按鈕似乎Attemping登錄...那麼幾秒鐘更成爲強制關閉。我確定的logcat的錯誤,但不知道該怎麼糾正呢..
順便說一句,我的Android應用是連接到Wi-Fi這我的筆記本電腦還共同nnect到同一Wi-Fi和我能夠從我的手機訪問的login.php與URL這是http://192.168.XX.XX:1234/PMSS/login.php
這是我activity.login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:useDefaultMargins="true"
tools:ignore="ExtraText" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/userid"
android:layout_centerHorizontal="true"
android:text="@string/userid"
android:textSize="20sp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/password"
android:layout_centerHorizontal="true"
android:text="@string/password"
android:textSize="20sp" />
<EditText
android:id="@+id/userid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView3"
android:layout_alignRight="@+id/password"
android:ems="10"
android:inputType="textPersonName" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:src="@drawable/pos_malaysia"
android:contentDescription="@string/app_main"/>
<Button
android:id="@+id/login"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_above="@+id/newuser"
android:layout_alignRight="@+id/textView3"
android:layout_marginBottom="30dp"
android:gravity="center"
android:text="@string/button_login" />
<TextView
android:id="@+id/newuser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/register"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp"
android:text="@string/newuser" />
<Button
android:id="@+id/register"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignLeft="@+id/login"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text="@string/button_register" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/login"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="textPassword" >
</EditText>
</RelativeLayout>
什麼是行號** ** 122在'Login.java' – Hariharan
'字符串username = email.getText()的ToString();'這是行122 – Nick