我在Android中使用Intent时遇到错误。我有一个MainActivity,我在那里调用另一个名为BackgroundWorker的类,所以在做了一些登录功能之后我想去用户页面,如果它是sucesss.enter代码,我在这里附上我的代码请帮助
package com.example.user.mybookapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText UsernameEt, PasswordEt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UsernameEt = (EditText) findViewById(R.id.etusername);
PasswordEt = (EditText) findViewById(R.id.etpassword);
}
public void OnLogin(View view) {
String username = UsernameEt.getText().toString();
String password = PasswordEt.getText().toString();
String type = "login";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type,username,password);
}
}
// BackgoundWorker类
package com.example.user.mybookapp;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
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.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
/**
* Created by user on 15-09-2016.
*/
public class BackgroundWorker extends AsyncTask<String,Void,String> {
public 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.4.2/login.php";
if(type.equals("login"))
{
try {
//Context context = getApplicationContext();
//Toast t= Toast.makeText(ctx,"click",Toast.LENGTH_SHORT).show();
//alertDialog = new AlertDialog.Builder(context).create();
//alertDialog.setTitle("login status");
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");
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();
String s=result.trim();
if (s.equalsIgnoreCase("success")){
Intent i =Intent(BackgroundWorker.this,User.class);//Problem
}
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
答案 0 :(得分:0)
Intent i =Intent(context,User.class);
这应该修复错误,应该使用当前上下文调用intent。
答案 1 :(得分:0)
Intent i =Intent(context,User.class);
context.startActivity(i);
这将作为BackgroundWorker使用。这是一个非活动参考。
答案 2 :(得分:0)
Intent构造函数需要一个上下文作为第一个参数,并且您将它赋予AsyncTask。您没有正确构建Intent类的对象,换句话说,您忘记编写\n
关键字。这是创建对象的语法: -
new
将其更改为
<Class> <objectName> = new <Class/Subclass>(constructor_params_separated_by_commas);
Intent i =Intent(BackgroundWorker.this,User.class);//Problem
答案 3 :(得分:0)
Intent intent = new Intent(mContext,SecondClass.class);
intent.putExtra("KEY","Value");
startActivity(intent);
//here KEY = the identifier of specific value
//on other side of SecondClass Activity. catch this data using
String res = getIntent().getStringExtra("KEY");
答案 4 :(得分:0)
如上面的答案中所提到的,第一个问题是不恰当的上下文,第二个问题是你在执行后台任务执行时在后台工作者类中传递了错误的参数。你的AsyncTask扩展:
session_start();
if (isset ( $_GET ['waypoint'] )) {
$waypoint = json_decode($_GET ['waypoint'],true);
print_r($waypoint['waypoint']); <<<========output 1
// as whatever happens we are just adding a waypoint
// to an array of waypoints in the SESSION variable
// this one line will create the $_SESSION['waypoints'][]
// or add a new [] to it.
$_SESSION['waypoints'][] = $waypoint;
}
// Just as belt and brace check,
// assuming someone could try to pass this code some rubbish
// like xxx.php&hacker=yes we ought to check before attempting to
// use the $_SESSION['waypoints'] that one exists
if ( isset($_SESSION['waypoints'] ) {
foreach( $_SESSION['waypoints'] as $var) {
// $var is now a waypoint array so it is not $var[0]['lat']
echo sprintf( 'Latitude = %s - Longitude = %s',
$var['lat'],
$var['long']
);
}
}
?>
表明你必须在doInBackground方法中传递String,而在上面的代码中,你试图获取字符串数组,尽管你已经从主要活动中传递了字符串参数,即
AsyncTask<String, Void, String>
应该是
protected String doInBackground(String[] params)
希望能解决问题。