您好我有2个活动,一个叫登录,另一个叫select_company 登录工作正常但我似乎无法让它改变活动 这是使用以下代码调用的类:
Login myActivity = new Login();
Login.Check_Login asyncTask = myActivity.new Check_Login();
asyncTask.execute();
我的课程:
public class Check_Login extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", Username));
params.add(new BasicNameValuePair("password", Password));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest("http://mobirep.co.za/server_scripts/check_login/check_login.php", "GET", params);
// Check your log cat for JSON reponse
Log.d("Status: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt("success");
TAG_SUCCESS = json.getString("success");
if (success == 1) {
JSONArray login_details_onj = json.getJSONArray("login");
JSONObject login_details = login_details_onj.getJSONObject(0);
// Get The Data
Name = login_details.getString(TAG_NAME);
ID = login_details.getString(TAG_ID);
Company_ID = login_details.getString(TAG_COMPANY);
}
else
{
TAG_NAME = json.getString("success");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
//pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
pDialog.dismiss();
Intent Select_company = new Intent(Login.this, Select_Company.class);
startActivity(Select_company);
}
});
}
}
doinbackground完成后就会给我错误。 当成功= 1时,线程必须停止,发送数据并加载select_company活动 这是我的LogCat:
05-14 13:39:51.863: E/Trace(4070): error opening trace file: No such file or directory (2)
05-14 13:39:53.273: D/dalvikvm(4070): GC_FOR_ALLOC freed 70K, 7% free 2598K/2792K, paused 154ms, total 161ms
05-14 13:39:53.422: I/dalvikvm-heap(4070): Grow heap (frag case) to 7.192MB for 4760016-byte allocation
05-14 13:39:53.752: D/dalvikvm(4070): GC_FOR_ALLOC freed 3K, 3% free 7243K/7444K, paused 228ms, total 228ms
05-14 13:39:54.082: D/dalvikvm(4070): GC_CONCURRENT freed <1K, 3% free 7251K/7444K, paused 10ms+53ms, total 291ms
05-14 13:39:55.083: I/Choreographer(4070): Skipped 42 frames! The application may be doing too much work on its main thread.
05-14 13:39:55.452: D/gralloc_goldfish(4070): Emulator without GPU emulation detected.
05-14 13:39:56.082: I/Choreographer(4070): Skipped 33 frames! The application may be doing too much work on its main thread.
05-14 13:40:04.203: I/Choreographer(4070): Skipped 65 frames! The application may be doing too much work on its main thread.
05-14 13:40:04.443: I/Choreographer(4070): Skipped 32 frames! The application may be doing too much work on its main thread.
05-14 13:40:04.982: I/Choreographer(4070): Skipped 47 frames! The application may be doing too much work on its main thread.
05-14 13:40:05.673: I/Choreographer(4070): Skipped 144 frames! The application may be doing too much work on its main thread.
05-14 13:40:05.823: I/Choreographer(4070): Skipped 36 frames! The application may be doing too much work on its main thread.
05-14 13:40:05.962: I/Choreographer(4070): Skipped 32 frames! The application may be doing too much work on its main thread.
05-14 13:40:06.162: I/Choreographer(4070): Skipped 45 frames! The application may be doing too much work on its main thread.
05-14 13:40:06.334: I/Choreographer(4070): Skipped 40 frames! The application may be doing too much work on its main thread.
05-14 13:40:07.313: I/Choreographer(4070): Skipped 77 frames! The application may be doing too much work on its main thread.
05-14 13:40:07.453: I/Choreographer(4070): Skipped 32 frames! The application may be doing too much work on its main thread.
05-14 13:40:07.672: I/Choreographer(4070): Skipped 52 frames! The application may be doing too much work on its main thread.
05-14 13:40:08.244: I/Choreographer(4070): Skipped 35 frames! The application may be doing too much work on its main thread.
05-14 13:40:08.382: I/Choreographer(4070): Skipped 34 frames! The application may be doing too much work on its main thread.
05-14 13:40:08.853: I/Choreographer(4070): Skipped 98 frames! The application may be doing too much work on its main thread.
05-14 13:40:09.052: I/Choreographer(4070): Skipped 33 frames! The application may be doing too much work on its main thread.
05-14 13:40:09.644: I/Choreographer(4070): Skipped 35 frames! The application may be doing too much work on its main thread.
05-14 13:40:10.083: I/Choreographer(4070): Skipped 39 frames! The application may be doing too much work on its main thread.
05-14 13:40:10.413: I/Choreographer(4070): Skipped 48 frames! The application may be doing too much work on its main thread.
05-14 13:40:10.582: I/Choreographer(4070): Skipped 38 frames! The application may be doing too much work on its main thread.
05-14 13:40:10.922: I/Choreographer(4070): Skipped 42 frames! The application may be doing too much work on its main thread.
05-14 13:40:11.062: I/Choreographer(4070): Skipped 34 frames! The application may be doing too much work on its main thread.
05-14 13:40:11.312: I/Choreographer(4070): Skipped 58 frames! The application may be doing too much work on its main thread.
05-14 13:40:11.452: I/Choreographer(4070): Skipped 32 frames! The application may be doing too much work on its main thread.
05-14 13:40:11.634: I/Choreographer(4070): Skipped 44 frames! The application may be doing too much work on its main thread.
05-14 13:40:12.072: I/Choreographer(4070): Skipped 36 frames! The application may be doing too much work on its main thread.
05-14 13:40:12.222: I/Choreographer(4070): Skipped 35 frames! The application may be doing too much work on its main thread.
05-14 13:40:12.422: I/Choreographer(4070): Skipped 48 frames! The application may be doing too much work on its main thread.
05-14 13:40:12.622: I/Choreographer(4070): Skipped 47 frames! The application may be doing too much work on its main thread.
05-14 13:40:12.763: I/Choreographer(4070): Skipped 32 frames! The application may be doing too much work on its main thread.
05-14 13:40:13.033: I/Choreographer(4070): Skipped 42 frames! The application may be doing too much work on its main thread.
05-14 13:40:13.193: I/Choreographer(4070): Skipped 37 frames! The application may be doing too much work on its main thread.
05-14 13:40:13.532: I/Choreographer(4070): Skipped 54 frames! The application may be doing too much work on its main thread.
05-14 13:40:13.682: I/Choreographer(4070): Skipped 36 frames! The application may be doing too much work on its main thread.
05-14 13:40:13.882: I/Choreographer(4070): Skipped 47 frames! The application may be doing too much work on its main thread.
05-14 13:40:14.092: I/Choreographer(4070): Skipped 45 frames! The application may be doing too much work on its main thread.
05-14 13:40:14.573: I/Choreographer(4070): Skipped 59 frames! The application may be doing too much work on its main thread.
05-14 13:40:14.922: I/Choreographer(4070): Skipped 47 frames! The application may be doing too much work on its main thread.
05-14 13:40:15.292: I/Choreographer(4070): Skipped 32 frames! The application may be doing too much work on its main thread.
05-14 13:40:16.284: I/Choreographer(4070): Skipped 44 frames! The application may be doing too much work on its main thread.
05-14 13:40:16.442: I/Choreographer(4070): Skipped 33 frames! The application may be doing too much work on its main thread.
05-14 13:40:16.632: D/Status:(4070): {"success":1,"login":[{"id":"1","company_id":"1","name":"Andre"}]}
05-14 13:40:16.852: I/Choreographer(4070): Skipped 33 frames! The application may be doing too much work on its main thread.
05-14 13:40:17.103: D/AndroidRuntime(4070): Shutting down VM
05-14 13:40:17.133: W/dalvikvm(4070): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-14 13:40:17.222: E/AndroidRuntime(4070): FATAL EXCEPTION: main
05-14 13:40:17.222: E/AndroidRuntime(4070): java.lang.NullPointerException
05-14 13:40:17.222: E/AndroidRuntime(4070): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
05-14 13:40:17.222: E/AndroidRuntime(4070): at android.content.ComponentName.<init>(ComponentName.java:75)
05-14 13:40:17.222: E/AndroidRuntime(4070): at android.content.Intent.<init>(Intent.java:3491)
05-14 13:40:17.222: E/AndroidRuntime(4070): at com.mobirep.app.Login$Check_Login$1.run(Login.java:170)
05-14 13:40:17.222: E/AndroidRuntime(4070): at android.os.Handler.handleCallback(Handler.java:725)
05-14 13:40:17.222: E/AndroidRuntime(4070): at android.os.Handler.dispatchMessage(Handler.java:92)
05-14 13:40:17.222: E/AndroidRuntime(4070): at android.os.Looper.loop(Looper.java:137)
05-14 13:40:17.222: E/AndroidRuntime(4070): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-14 13:40:17.222: E/AndroidRuntime(4070): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 13:40:17.222: E/AndroidRuntime(4070): at java.lang.reflect.Method.invoke(Method.java:511)
05-14 13:40:17.222: E/AndroidRuntime(4070): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-14 13:40:17.222: E/AndroidRuntime(4070): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-14 13:40:17.222: E/AndroidRuntime(4070): at dalvik.system.NativeStart.main(Native Method)
这是我的login.java:
package com.mobirep.app;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends ActionBarActivity {
//
private static ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
static String TAG_SUCCESS = "success";
String TAG_ID = "id";
String TAG_NAME = "name";
String TAG_COMPANY = "company_id";
//buttons
public static String Username;
public static String Password;
static String Name, Company_ID, ID;
//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_login,
container, false);
Button btnLogin = (Button) rootView.findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText edtUsername = (EditText)getActivity().findViewById(R.id.edtUsername);
Username = edtUsername.getText().toString();
EditText edtPassword = (EditText)getActivity().findViewById(R.id.edtPassword);
Password = edtPassword.getText().toString();
// Toast for debugging purposes
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Checking Details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
Login myActivity = new Login();
Login.Check_Login asyncTask = myActivity.new Check_Login();
asyncTask.execute();
}
});
return rootView;
}
}
public class Check_Login extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", Username));
params.add(new BasicNameValuePair("password", Password));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest("http://mobirep.co.za/server_scripts/check_login/check_login.php", "GET", params);
// Check your log cat for JSON reponse
Log.d("Status: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt("success");
TAG_SUCCESS = json.getString("success");
if (success == 1) {
JSONArray login_details_onj = json.getJSONArray("login");
JSONObject login_details = login_details_onj.getJSONObject(0);
// Get The Data
Name = login_details.getString(TAG_NAME);
ID = login_details.getString(TAG_ID);
Company_ID = login_details.getString(TAG_COMPANY);
}
else
{
TAG_NAME = json.getString("success");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
runOnUiThread(new Runnable() {
public void run() {
pDialog.dismiss();
Intent Select_company = new Intent(Login.this, Select_Company.class);
startActivity(Select_company);
}
});
}
}
}
答案 0 :(得分:1)
public class Login extends ActionBarActivity {
Login
是一个Activity类。您正在实例化一个错误的活动类Login myActivity = new Login();
你有
Intent Select_company = new Intent(Login.this, Select_Company.class);
startActivity(Select_company);
您需要一个有效的Context作为Intent构造函数的第一个参数,startActivity也是一个Activity的方法。
进一步阅读
Why do you have to pass this Context as a parameter in Android development?
您需要做的是将有效的上下文从Activity传递给类的构造函数,并使用上下文作为intent的第一个参数并使用context.startActivtiy
编辑:
在Fragment中有这个。
new Check_Login(getActivity()).execute(Username,Password);
然后将AsyncTask
课程从Login
课程移到单独的.java
文件
public class Check_Login extends AsyncTask<String, String, String> {
Context context;
ProgressDialog pDialog;
static String TAG_SUCCESS = "success";
String TAG_ID = "id";
String TAG_NAME = "name";
String TAG_COMPANY = "company_id";
static String Name, Company_ID, ID;
public Check_Login(Context context) {
// TODO Auto-generated constructor stub
this.context =context;
pDialog = new ProgressDialog(context);
pDialog.setMessage("Checking Details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", args[0]));
params.add(new BasicNameValuePair("password", args[1]));
// getting JSON string from URL
HttpClient client = new DefaultHttpClient();
HttpPost httpGet = new HttpPost("http://mobirep.co.za/server_scripts/check_login/check_login.php");
httpGet.setEntity(new UrlEncodedFormEntity(params));
HttpResponse res = client.execute(httpGet);
String jsonres = EntityUtils.toString(res.getEntity());
JSONObject json = new JSONObject(jsonres);
// Check your log cat for JSON reponse
Log.d("Status: ", json.toString());
// Checking for SUCCESS TAG
int success = json.getInt("success");
TAG_SUCCESS = json.getString("success");
if (success == 1) {
JSONArray login_details_onj = json.getJSONArray("login");
JSONObject login_details = login_details_onj.getJSONObject(0);
// Get The Data
Name = login_details.getString(TAG_NAME);
ID = login_details.getString(TAG_ID);
Company_ID = login_details.getString(TAG_COMPANY);
}
else
{
TAG_NAME = json.getString("success");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
{
pDialog.dismiss();
Intent Select_company = new Intent(context, Select_Company.class);
context.startActivity(Select_company);
}
}
}