【问题标题】:How to make in alertDialog by clicking OK then, go to the next page?如何通过单击确定在alertDialog中进行然后转到下一页?
【发布时间】:2019-09-21 05:56:18
【问题描述】:

我的问题:

1.我有错误。

2。为什么我使用 startActivity 时无法进入下一页?

3。如何解决?

4.我已经通过 Intent 方法使用 startActivity

以下是证明1:

下面是证明2:

下面是证明3:

下面是代码 sn-p :

 public void OnLog(View view)
 {
    String Username = username.getText().toString();
    String Password = password.getText().toString();
    String type = "login";

    if(Username.equals("") || Password.equals(""))
    {
        Toast.makeText(getApplicationContext(), "Please fill the Username and Password!", Toast.LENGTH_LONG).show();
    }
    else {

        Background bg = new Background(Context, act);
        bg.execute(type, Username, Password);
    }
}

下面是 Background.java 的编码:

public class Background extends AsyncTask<String,Void,String> {

Context context;
AlertDialog alertDialog;

Background(Context ctx) {
    context = ctx;
}

@Override
protected String doInBackground(String... params)
{
    String type = params[0];
    String login_url = "http://172.20.10.4/LoginLab3.php";
    String reg_url = "http://172.20.10.4/RegisterLab3.php";
    if (type.equals("login")) {
        try {
            String username = 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("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();
        }
    }
    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(reg_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.Builder dialog = new AlertDialog.Builder(context);
    dialog.setTitle("Login Status");
    dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            startActivity(new Intent(Login.this, Welcome.class));
        }
    });
    dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

        }
    });
    dialog.create().show();
}

@Override
protected void onProgressUpdate(Void... values)
{
    super.onProgressUpdate(values);
}
}

帮帮我!我有一些问题。我已经使用了几乎所有方法,但无法转到下一页。为什么?

【问题讨论】:

  • 发生这种情况是因为您在 AsyncTask 中使用它。

标签: java android android-studio connection


【解决方案1】:

Activity OnCreate 代码

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Pass activity to Async Task
    new Background(this).execute();
 }  

异步任务

public class Background extends AsyncTask<Void,Void,Void> {
    private Context context;

    public Background(Context context){
        this.context=context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        Intent intent = new Intent(context, TargetActivity.class);
        context.startActivity(intent);
        ((Activity)context).finish();
    }
}

【讨论】:

    【解决方案2】:

    尝试context.startActivity(),因为AsyncTask 类没有继承startActivity() 方法。

    另外,在构造函数中传递活动并将其分配给一个变量,并在创建意图时使用该变量

    Context context;
    Activity activity;
    AlertDialog alertDialog;
    
    Background(Context ctx, Activity act) {
        context = ctx;
        activity = act;
    }
    

    onClick()

    context.startActivity(new Intent(activity, Welcome.class));
    

    【讨论】:

    • 当我进行更正时。在 Login.java 在“(this)”处有错误。背景 bg = 新背景(此); bg.execute(类型、用户名、密码);
    • 在上面我已经为 Login.java 添加了 logcat 和代码 sn-p。你可以查看
    • @wanyarash 将`Background bg = new Background(Context, act);`替换为Background bg = new Background(getContext(), getActivity());
    • 已经替换了'Background bg = new Background(getContext(), getActivity());'并且在'(getContext(), getActivity()'上有错误。构建输出写入'错误: 找不到符号方法 getContext( )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多