进一步冒险进入Android Studio!
我确实有这个工作。我有一个登录活动,然后传递到另一个。现在非常简单。完美地在模拟器上工作。尝试连接我的手机并且它抛出了一个NetworkOnMainException ...所以,研究了这个并将互联网登录检查到一个单独的异步线程中。
当我现在执行我的应用程序时,当它启动新活动时,它会崩溃。奇怪的是,如果我调试它并逐步执行代码,它就可以工作,并显示第二个活动。如果我让应用程序按照自己的速度运行,它就会崩溃。
我的登录按钮方法是:
public void signInButtonClick(View v) throws InterruptedException {
// Button button=(Button) v;
String loginSql;
Globals.userName = loginID.getText().toString();
Globals.passWord = loginPW.getText().toString();
//www.myarraydata.com/app/androidlogin.php?username=X&password=Y
loginSql = "http://www.mywebsite.com/app/loginscript.php?username="+Globals.userName+"&password="+Globals.passWord ;
((Button) v).setText("Signing in...");
System.out.println(loginSql);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
try {
url = new URL(loginSql);
new loginTask().execute();
System.out.println(readStream);
if(readStream.equals("FAIL"))
{
loggedIn = false;
}
else
{
loggedIn = true;
//Store the list of arrays for this customer in the arrayList global variable
Globals.arrayList = Arrays.asList(readStream.split("\\s*#\\s*"));
}
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
if (loggedIn==true)
{
startActivity(new Intent(MainActivity.this,LiveStatus.class));
Thread.sleep(1000);
finish();
}
else
{
new AlertDialog.Builder(this)
.setTitle("Oops...")
.setMessage("Sign in failed. Please try again.")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
Logcat显示它是由signInButtonClick中的InvocationTargetExcecption和NullPointerException引起的。
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: FATAL EXCEPTION: main
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method of the activity
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.view.View$1.onClick(View.java:2072)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.view.View.performClick(View.java:2408)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:8816)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:587)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.os.Looper.loop(Looper.java:123)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4627)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:521)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at app.ictis.com.myarraydaya.MainActivity.signInButtonClick(MainActivity.java:133)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:521)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.view.View$1.onClick(View.java:2067)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.view.View.performClick(View.java:2408)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:8816)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:587)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.os.Looper.loop(Looper.java:123)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4627)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:521)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: Caused by: java.lang.NullPointerException
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at app.ictis.com.myarraydaya.MainActivity.signInButtonClick(MainActivity.java:133)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:521)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.view.View$1.onClick(View.java:2067)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.view.View.performClick(View.java:2408)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:8816)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:587)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.os.Looper.loop(Looper.java:123)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4627)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:521)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
有什么想法吗?
答案 0 :(得分:1)
发现当我声明时,loggedIn变量需要分配。那是空指针。我
new loginTask().execute();
在正常模式下,似乎没有正确执行,只有在设置时才会执行......这是奇怪的。
指向弗兰克指针(没有双关语!)