Thursday, February 13, 2014

Andriod Runtime error solved!


07-08 10:30:19.604: E / AndroidRuntime (567): FATAL EXCEPTION: main
07-08 10:30:19.604: E / AndroidRuntime (567): java.lang.ArrayIndexOutOfBoundsException: length = 1; index = 1
07-08 10:30:19.604: E / AndroidRuntime (567): at com.amaker.wlo.LoginActivity.saveUserMsg (LoginActivity.java: 85)
07-08 10:30:19.604: E / AndroidRuntime (567): at com.amaker.wlo.LoginActivity.login (LoginActivity.java: 70)
07-08 10:30:19.604: E / AndroidRuntime (567): at com.amaker.wlo.LoginActivity.access $ 1 (LoginActivity.java: 60)
07-08 10:30:19.604: E / AndroidRuntime (567): at com.amaker.wlo.LoginActivity $ 2.onClick (LoginActivity.java: 49)
07-08 10:30:19.604: E / AndroidRuntime (567): at android.view.View.performClick (View.java: 3480)
07-08 10:30:19.604: E / AndroidRuntime (567): at android.view.View $ PerformClick.run (View.java: 13983)
07-08 10:30:19.604: E / AndroidRuntime (567): at android.os.Handler.handleCallback (Handler.java: 605)
07-08 10:30:19.604: E / AndroidRuntime (567): at android.os.Handler.dispatchMessage (Handler.java: 92)
07-08 10:30:19.604: E / AndroidRuntime (567): at android.os.Looper.loop (Looper.java: 137)
07-08 10:30:19.604: E / AndroidRuntime (567): at android.app.ActivityThread.main (ActivityThread.java: 4340)
07-08 10:30:19.604: E / AndroidRuntime (567): at java.lang.reflect.Method.invokeNative (Native Method)
07-08 10:30:19.604: E / AndroidRuntime (567): at java.lang.reflect.Method.invoke (Method.java: 511)
07-08 10:30:19.604: E / AndroidRuntime (567): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java: 784)
07-08 10:30:19.604: E / AndroidRuntime (567): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 551)
07-08 10:30:19.604: E / AndroidRuntime (567): at dalvik.system.NativeStart.main (Native Method)
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
LoginActivity 85 line array bounds, suggesting very clear: the total length of just one, but taken to index1

07-08 10:30:19.604: E / AndroidRuntime (567): java.lang.ArrayIndexOutOfBoundsException: length = 1; index = 1
07-08 10:30:19.604: E / AndroidRuntime (567): at com.amaker.wlo.LoginActivity.saveUserMsg (LoginActivity.java: 85)
Reply:
Well this is obviously crossed the line
Reply:
package com.amaker.wlo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.amaker.util.HttpUtil;

public class LoginActivity extends Activity {
/ / Declare login, cancel button
private Button cancelBtn, loginBtn;
/ / Declare a user name, password input box
private EditText userEditText, pwdEditText;
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
/ / Set the title
setTitle ("palm-sized wireless ordering system - user login");
/ / Set the current Activity interface layout
setContentView (R.layout.login_system);
/ / Instantiate components
by findViewById methodcancelBtn = (Button) findViewById (R.id.cancelButton);
/ / Instantiate components
by findViewById methodloginBtn = (Button) findViewById (R.id.loginButton);
/ / Instantiate components
by findViewById methoduserEditText = (EditText) findViewById (R.id.userEditText);
/ / Instantiate components
by findViewById methodpwdEditText = (EditText) findViewById (R.id.pwdEditText);

cancelBtn.setOnClickListener (new OnClickListener () {

public void onClick (View v) {
finish ();
}
});

loginBtn.setOnClickListener (new OnClickListener () {

public void onClick (View v) {
if (validate ()) {
if (login ()) {
Intent intent = new Intent (LoginActivity.this, MainMenuActivity.class);
startActivity (intent);
} Else {
showDialog ("User name or password is incorrect, please re-enter!");
}
}
}
});
}
/ / Login method
private boolean login () {
/ / Get the user name
String username = userEditText.getText () toString ();.
/ / Get the password
String pwd = pwdEditText.getText () toString ();.
/ / Get login results
String result = query (username, pwd);
if (result! = null && result.equals ("0")) {
return false;
} Else {
saveUserMsg (result);
return true;
}
}

/ / Save the user information to the configuration file
private void saveUserMsg (String msg) {
/ / User ID
String id = "";
/ / User name
String name = "";
/ / Get information array
String [] msgs = msg.split (";");
. int idx = msgs [0] indexOf ("=");
id = msgs [0] substring (idx +1);.
. idx = msgs [1] indexOf ("=");
. name = msgs [1] substring (idx +1);
/ / Share information
SharedPreferences pre = getSharedPreferences ("user_msg", MODE_WORLD_WRITEABLE);
SharedPreferences.Editor editor = pre.edit ();
editor.putString ("id", id);
editor.putString ("name", name);
editor.commit ();
}

/ / Authentication method
private boolean validate () {
String username = userEditText.getText () toString ();.
if (username.equals ("")) {
showDialog ("User name is required!");
return false;
}
String pwd = pwdEditText.getText () toString ();.
if (pwd.equals ("")) {
showDialog ("User password is required!");
return false;
}
return true;
}
private void showDialog (String msg) {
AlertDialog.Builder builder = new AlertDialog.Builder (this);
builder.setMessage (msg)
. SetCancelable (false)
. SetPositiveButton ("OK", new DialogInterface.OnClickListener () {
public void onClick (DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create ();
alert.show ();
}
/ / Query the user name password
private String query (String account, String password) {
/ / Query parameters
String queryString = "account =" + account + "& password =" + password;
/ / Url
String url = HttpUtil.BASE_URL + "? Servlet / LoginServlet" + queryString;
/ / Query results
return HttpUtil.queryStringForPost (url);
}
}
Program source code, and the Great God help copy of the book, but there is always running above error
Reply:
 
/ / Query the user name password
private String query (String account, String password) {
/ / Query parameters
String queryString = "account =" + account + "& password =" + password;
/ / Url
String url = HttpUtil.BASE_URL + "? Servlet / LoginServlet" + queryString;
/ / Query results
return HttpUtil.queryStringForPost (url);
}

This period, your program has LoginServlet this Servlet it? If not, certainly not, ah, if anything, to see the return of String have this semicolon; Because there when he split with a semicolon
 
/ / Get information array
String [] msgs = msg.split (";");

Reply:
Array bounds, it should be a problem with your return data, parse it, just parse out the part of the latter can log debug look at the data returned
Reply:
When the array index is out of range, the interception that you may not have the string delimiters, first, you can split the time to test, second, can give you an array of default values, so, at least not an error, then debug see, in the end is not enough data format which place. android technology group: 237 305 376, a new group of many people, and everyone is welcome to join ~
Reply:
Significant cross-border, debug tracing down
Reply:
Seemingly not a cross-border ah, changed the array is not enough, Debug result

this LoginActivity (id = 830022674824)
id "network anomalies!" (id = 830026090968)
idx -1
msgs String [1] (id = 830026091240)
name "" (id = 830012502784)
count 0
hashCode 0
offset 0
value (id = 830012502816)
msg "network anomalies!" (id = 830026090968)
count 5
hashCode 1027227840
offset 0
value (id = 830026091000)
The following is my mySql database link is not an erroneous it, the machine is running Andrews Links mySql database format did not address what, thank you friends
public static final String BASE_URL = "http://192.168.1.104:8080/mysql/";
Reply:
referenced 8th Floor reply:
seemingly not a cross-border ah, changed the array is not enough, Debug result

this LoginActivity (id = 830022674824)
id "network anomalies!" (id = 830026090968)
idx -1
msgs String [1] (id = 830026091240)
name "" (id = 830012502784)
count 0
hashCode 0
offset 0
......


private void saveUserMsg (String msg) {msg put under this print, do not understand yet
Reply:
Bounds.
Reply:
07-11 13:01:25.616: E / AndroidRuntime (581): FATAL EXCEPTION: main
07-11 13:01:25.616: E / AndroidRuntime (581): java.lang.NullPointerException
07-11 13:01:25.616: E / AndroidRuntime (581): at com.amaker.wlo.LoginActivity.login (LoginActivity.java: 67)
07-11 13:01:25.616: E / AndroidRuntime (581): at com.amaker.wlo.LoginActivity.access $ 1 (LoginActivity.java: 60)
07-11 13:01:25.616: E / AndroidRuntime (581): at com.amaker.wlo.LoginActivity $ 2.onClick (LoginActivity.java: 49)
07-11 13:01:25.616: E / AndroidRuntime (581): at android.view.View.performClick (View.java: 3480)
07-11 13:01:25.616: E / AndroidRuntime (581): at android.view.View $ PerformClick.run (View.java: 13983)
07-11 13:01:25.616: E / AndroidRuntime (581): at android.os.Handler.handleCallback (Handler.java: 605)
07-11 13:01:25.616: E / AndroidRuntime (581): at android.os.Handler.dispatchMessage (Handler.java: 92)
07-11 13:01:25.616: E / AndroidRuntime (581): at android.os.Looper.loop (Looper.java: 137)
07-11 13:01:25.616: E / AndroidRuntime (581): at android.app.ActivityThread.main (ActivityThread.java: 4340)
07-11 13:01:25.616: E / AndroidRuntime (581): at java.lang.reflect.Method.invokeNative (Native Method)
07-11 13:01:25.616: E / AndroidRuntime (581): at java.lang.reflect.Method.invoke (Method.java: 511)
07-11 13:01:25.616: E / AndroidRuntime (581): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java: 784)
07-11 13:01:25.616: E / AndroidRuntime (581): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 551)
07-11 13:01:25.616: E / AndroidRuntime (581): at dalvik.system.NativeStart.main (Native Method)
Still have problems ah
Reply:
LoginActivity.java line 67 error, a null pointer, to send up to look at the source code LoginActivity.java
Reply:
Error should be userEditText.getText () or pwdEditText.getText () is null,

The following two lines of code can change it

String username = userEditText.getText () toString ();.
String pwd = pwdEditText.getText () toString ();.

Changed:
String username = null;
String pwd = null;
if (userEditText! = null && userEditText.getText ()! = null)
{
username = userEditText.getText () toString ();.
}
if (pwdEditText! = null && pwdEditText.getText ()! = null)
{
pwd = pwdEditText.getText () toString ();.
}

Reply:
reference to the 11th floor xydf_1992 reply:
07-11 13:01:25.616: E / AndroidRuntime (581): FATAL EXCEPTION: main
07-11 13:01:25.616: E / AndroidRuntime (581): java.lang.NullPointerException
07-11 13:01:25.616: E / AndroidRuntime (581): at com.amaker.wlo.LoginActivity.login (LoginActivity.java: 67)
07-11 13:01:25.616: E / AndroidRuntime (581): at com.amaker.wlo.LoginActivity.access $ 1 (LoginActivity.java: 60)
07-11 13:01:25.616: E / AndroidRuntime (581): at com.amaker.wlo.LoginActivity $ 2.onClick (LoginActivity.java: 49)
07-11 13:01:25.616: E / AndroidRuntime (581): at android.view.View.performClick (View.java: 3480)
07-11 13:01:25.616: E / AndroidRuntime (581): at android.view.View $ PerformClick.run (View.java: 13983)
07-11 13:01:25.616: E / AndroidRuntime (581): at android.os.Handler.handleCallback (Handler.java: 605)
07-11 13:01:25.616: E / AndroidRuntime (581): at android.os.Handler.dispatchMessage (Handler.java: 92)
07-11 13:01:25.616: E / AndroidRuntime (581): at android.os.Looper.loop (Looper.java: 137)
07-11 13:01:25.616: E / AndroidRuntime (581): at android.app.ActivityThread.main (ActivityThread.java: 4340)
07-11 13:01:25.616: E / AndroidRuntime (581): at java.lang.reflect.Method.invokeNative (Native Method)
07-11 13:01:25.616: E / AndroidRuntime (581): at java.lang.reflect.Method.invoke (Method.java: 511)
07-11 13:01:25.616: E / AndroidRuntime (581): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java: 784)
07-11 13:01:25.616: E / AndroidRuntime (581): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 551)
07-11 13:01:25.616: E / AndroidRuntime (581): at dalvik.system.NativeStart.main (Native Method)
Still have problems ah


Finally, how to modify to get the ah?
Reply:
reference to the 13th floor AMinfo reply:
error should be userEditText.getText () or pwdEditText.getText () is null,

The following two lines of code can change it

String username = userEditText.getText () toString ();.
String pwd = pwdEditText.getText () toString ();.

Changed:
String username = null;
String pwd = null;
if (userEditText! = null && userEditText.getText ()! = null)
{
username = userEditText.getText () toString ();.
}
if (pwdEditText! = null && pwdEditText.getText ()! = null)
{
pwd = pwdEditText.getText () toString ();.
}
as you modify or not ah. . .

No comments:

Post a Comment