无法从Android中的onPostExecute方法启动新活动(Can't able to start new activity from onPostExecute method in

编程入门 行业动态 更新时间:2024-10-27 18:30:37
无法从Android中的onPostExecute方法启动新活动(Can't able to start new activity from onPostExecute method in Android)

这是我的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无法识别它。

Here is my Java code. My problem is when I add some code to OnPostExecute to open another activity. I'm new to Android Studio so I'm facing this problem for first time.

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); } }

Start Activity error

Cannot resolve constructor

I'm trying to add some simple code and my Android does not recognize it.

最满意答案

兄弟,你的问题是你在Intent中传递了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派生的,因此当您尝试从它们启动startActivity时,您不能只调用它。

Bro, your problem is that you're passing this in the Intent, and this is currently the AsyncTask which is NOT derived from anything that has Context (Activities have Context). Just replace that line with this:

startActivity(new Intent(context, Pagina1.class));} //context being the field you initialize in the constructor

And this'll work. I know you have probably found somewhere that this is the correct way to start the activity:

startActivity(new Intent(this, YourClass.class));

Which it is, when it is from another Activity, because the first parameter of the Intent() constructor is Context, which activities do have. Fragments, AsyncTasks, etc. are not derived from Context and therefore you cannot just call this when you're trying to startActivity from them.

更多推荐

本文发布于:2023-07-21 09:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1208960.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   onPostExecute   Android   method   activity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!