这是我的Java代码。我的问题是当我向OnPostExecute
添加一些代码以打开另一个活动时。我是Android Studio的新手,所以我第一次遇到这个问题。
package com.example.gonalo.meu;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
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.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import static android.support.v4.app.ActivityCompat.startActivity;
import static android.support.v4.content.ContextCompat.startActivities;
/**
* Created by Gonçalo on 23/03/2016.
*/
public class BackGroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
BackGroundWorker (Context ctx) {
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://192.168.1.79/login.php";
String register_url = "http://192.168.1.79/register.php";
//String verifyuserpass_url = "http://192.168.0.102/verifyuserpass.php";
//String verifypass_url = "http://192.168.0.102/verifyuserpass.php";
if(type.equals("login")) {
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_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 post_data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
String user = URLEncoder.encode(user_name, "UTF-8");//guarda o nome de utilizador introduzido
String pass = URLEncoder.encode(password, "UTF-8");//guarda a pass introduzido
System.err.println("------------------------------------------");
/*/ if(user.equals("Nome de Utilizador")){
if(pass.equals("Password")) {
System.err.println("Entrou no if");
startActivity(new Intent(this, Pagina1.class));}
/*/
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result +=line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else if(type.equals("register")) {
try {
String name = params[1];
String surname = params[2];
String age = params[3];
String username = params[4];
String password = params[5];
URL url = new URL(register_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 post_data = URLEncoder.encode("name", "UTF-8")+"="+URLEncoder.encode(name, "UTF-8")+"&"
+ URLEncoder.encode("surname", "UTF-8")+"="+URLEncoder.encode(surname, "UTF-8")+"&"
+ URLEncoder.encode("age", "UTF-8")+"="+URLEncoder.encode(age, "UTF-8")+"&"
+ URLEncoder.encode("username", "UTF-8")+"="+URLEncoder.encode(username, "UTF-8")+"&"
+URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(password, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
if (result.equals("Login Success"));
Intent myIntent = new Intent(this, Pagina1.class); // Im facing problem here
startActivity(myIntent); // Im facing problem here
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
我正在尝试添加一些简单的代码,我的Android无法识别它。
答案 0 :(得分:0)
尝试重新启动你的android studio.And还要检查你是否已在清单中添加。
答案 1 :(得分:0)
兄弟,你的问题是你在意图中传递this
,而this
目前是AsyncTask
来自任何有Context
(活动有上下文)。只需用以下内容替换该行:
startActivity(new Intent(context, Pagina1.class));}
//context being the field you initialize in the constructor
这会起作用。 我知道你可能已经发现这是开始这项活动的正确方法:
startActivity(new Intent(this, YourClass.class));
它来自哪个,来自另一个Activity
,因为Intent()
构造函数的第一个参数是Context
,哪些活动确实有。 Fragments
,AsyncTasks
等不是来自Context
,因此当您尝试this
时,您无法致电startActivity
。
答案 2 :(得分:0)
试试这个
Intent myIntent = new Intent(this,Pagina1.class);
在上面的语句中这个指向当前的类对象,不是应用程序的活动或上下文。您必须提供应用程序的上下文。因为当前的类不是活动。上面的陈述应该像这样
试试这句话
Intent myIntent = new Intent(getApplicationContext(), Pagina1.class);
在您的onPostExecute()
方法中替换上述语句。
答案 3 :(得分:0)
在onPostExecute方法中执行以下操作:-
Intent intent = new Intent(context,SecondActivity.class);
context.startActivity(intent);
肯定会起作用!