我的应用从数据库接收数据以进行授权和注册。当startActivity(new Intent(this, Main.class));
时
line被评论出应用程序只是最小化,当取消注释它崩溃。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.littledev.test_v2">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main">
</activity>
</application>
</manifest>
package com.example.littledev.test_v2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class Login extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void onLoginSuccess() {
startActivity(new Intent(this, Main.class));
}
public void onLogin(View view){
EditText eLogin = (EditText) findViewById(R.id.username);
EditText ePass = (EditText) findViewById(R.id.password);
String login = eLogin.getText().toString();
String pass = ePass.getText().toString();
Sql_bridge bridge = new Sql_bridge(this);
bridge.execute("login",login,pass);
finish();
}
public void onRegister(View view) {
//startActivity(new Intent(this, Register.class));
}
}
package com.example.littledev.test_v2;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
/**
* Created by LittleDev on 3/30/2017.
*/
public class Sql_bridge extends AsyncTask<String,Void,String> {
private Context ctx;
Sql_bridge (Context ctx)
{
this.ctx =ctx;
}
@Override
protected void onPreExecute() {
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Main Information....");
}
@Override
protected String doInBackground(String... params) {
String reg_url = "http://e-shops.hol.es/app_register";
String log_url = "http://e-shops.hol.es/app_login";
String method = params[0];
if (method.equals("register")) {
String name = params[1];
String surname = params[2];
String email = params[3];
String password = params[4];
try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
URLEncoder.encode("surname", "UTF-8") + "=" + URLEncoder.encode(surname, "UTF-8") + "&" +
URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8") + "&" +
URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response = "";
String line;
while ((line = bufferedReader.readLine())!=null)
{
response+= line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;
} catch (IOException e) {
e.printStackTrace();
}
}else if (method.equals("login")){
String login_name = params[1];
String login_pass = params[2];
try {
URL url = new URL(log_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data = URLEncoder.encode("login","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+
URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response = "";
String line;
while ((line = bufferedReader.readLine())!=null)
{
response+= line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Login login = new Login();
switch (result) {
case "Registered":
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
break;
case "Login":
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
login.onLoginSuccess();
break;
default:
Toast.makeText(ctx, "Failed", Toast.LENGTH_LONG).show();
break;
}
}
}
03-30 15:11:48.323 9997-9997/com.example.littledev.test_v2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.littledev.test_v2, PID: 9997
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
at android.app.Activity.startActivityForResult(Activity.java:4298)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:4245)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4582)
at android.app.Activity.startActivity(Activity.java:4550)
at com.example.littledev.test_v2.Login.onLoginSuccess(Login.java:19)
at com.example.littledev.test_v2.Sql_bridge.onPostExecute(Sql_bridge.java:131)
at com.example.littledev.test_v2.Sql_bridge.onPostExecute(Sql_bridge.java:23)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.access$500(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7329)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
我真的不知道为什么会崩溃。
答案 0 :(得分:1)
public void onLoginSuccess() {
startActivity(new Intent(Login.this, Main.class));
}
从onLogin方法中删除finish()
因为AsyncTask工作背景你调用finish方法来完成当前活动。
public void onLogin(View view){
EditText eLogin = (EditText) findViewById(R.id.username);
EditText ePass = (EditText) findViewById(R.id.password);
String login = eLogin.getText().toString();
String pass = ePass.getText().toString();
Sql_bridge bridge = new Sql_bridge(this);
bridge.execute("login",login,pass);
// finish(); remove it
}
答案 1 :(得分:0)
您是否尝试从finish()
删除onLogin()
并仅在收到登录结果时调用它?
public void onLoginSuccess() {
finish();
startActivity(new Intent(this, Main.class));
}
答案 2 :(得分:0)
在第一个参数中使用当前活动上下文
startActivity(new Intent(Login.this, Main.class));
答案 3 :(得分:0)
你不能新建一个活动。
package com.example.littledev.test_v2;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class Sql_bridge extends AsyncTask<String,Void,String> {
private Activity ctx;
Sql_bridge (Activity ctx)
{
this.ctx =ctx;
}
@Override
protected void onPostExecute(String result) {
switch (result) {
case "Registered":
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
break;
case "Login":
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
if(ctx instanceof Login){
((Login)ctx).onLoginSuccess();
}
break;
default:
Toast.makeText(ctx, "Failed", Toast.LENGTH_LONG).show();
break;
}
}
}
并删除finish():
public void onLogin(View view){
EditText eLogin = (EditText) findViewById(R.id.username);
EditText ePass = (EditText) findViewById(R.id.password);
String login = eLogin.getText().toString();
String pass = ePass.getText().toString();
Sql_bridge bridge = new Sql_bridge(this);
bridge.execute("login",login,pass);
}
并在登录成功时调用完成:
public void onLoginSuccess() {
startActivity(new Intent(this, Main.class));
finish();
}
答案 4 :(得分:0)
@sasilKumar和@Jaydeep Patel说:
在onLogin()
中的Login.java
方法中,您在启动Sql_bridge
异步任务实例之后立即调用了finish(),并且在result
中onPostExecute(String result)
之后调用onLoginSuccess()之前就已经过了MainThread
1 {}在<%@ Page Language="C#" Inherits="sbmanager.mgrGridHome" MasterPageFile="~/masterpages/global.master" %>
上,那是什么引发了异常。