2016-09-24 32 views
-1

每個人在Android中使用Intent時都會出現錯誤。我從那裏我打電話叫BackgroundWorker的另一個類這樣登錄的一些功能我要到用戶頁面後MainActivity如果是sucesss.enter代碼在這裏我在這裏附上我的代碼請幫助Android中的意圖錯誤

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

您是否在logcat中遇到錯誤?您是否爲清單添加了'用戶'聲明'? '用戶'是'活動'嗎?我可以問一百萬個問題。我們不讀頭腦。請告訴我們你期望發生的事情,實際發生的事情以及你在logcat中看到的錯誤或其他消息。 –

回答

0
Intent i =Intent(context,User.class); 

這應該修復錯誤,應該使用當前上下文調用意圖。

0
Intent i =Intent(context,User.class); 
context.startActivity(i); 

這將作爲BackgroundWorker.this是一個非活動的參考。

0

意圖構造函數需要一個上下文作爲第一個參數,並且你給它AsyncTask。你是不是構建Intent類的對象不當,換句話說,你已經忘記了寫new keyword.This是語法來創建對象: -

<Class> <objectName> = new <Class/Subclass>(constructor_params_separated_by_commas); 

Intent i =Intent(BackgroundWorker.this,User.class);//Problem 

改變它

Intent i =new Intent(context,User.class);//Problem solved 
context.startActivity(i); 
+0

您好harish,感謝您的幫助,但現在的問題是,當我運行該應用程序,它說「不幸的是,該應用程序已停止」 –

+0

檢查Android監視器選項卡,並給我從日誌的例外信息,然後我可以幫助你 –

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"); 
0

正如在上面的答案中提到的,第一個問題是不恰當的上下文,第二個問題是在執行後臺任務執行時,您在後臺worker類中傳遞了錯誤的參數。您的AsyncTask擴展:

AsyncTask<String, Void, String> 

表明你必須通過字符串在doInBackground方法,而在上面的代碼中你試圖讓字符串數組雖然你已經從你的主要活動即

protected String doInBackground(String[] params) 
傳遞的字符串參數

應該是

protected String doInBackground(String... params) 

希望能解決這個問題。