我有一个主要的活动,它将执行asynctask,然后使用interface将数据发送回活动,看起来像下面的代码
主要活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_scroll);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
String type = "getProfile";
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
SharedPreferences.Editor editor = pref.edit();
String username = pref.getString("username", null);
RetrieveData retrieveData = new RetrieveData(this);
retrieveData.execute(type,username);
}
public void sendData(String response) {
// TODO Auto-generated method stub
String name="";
String age="";
String gender = "";
String id = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray("result");
JSONObject collegeData = result.getJSONObject(0);
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
name = collegeData.getString("name");
age = collegeData.getString("age");
gender = collegeData.getString("gender");
id = collegeData.getString("id");
EditText MyName = (EditText) findViewById(R.id.editText14);
MyName.setText(name);
EditText MyAge = (EditText) findViewById(R.id.editText21);
MyAge.setText(age);
EditText MyGender = (EditText) findViewById(R.id.editText22);
MyGender.setText(gender);
Picasso.with(this).load("http://192.168.12.252/"+id+"/profile.png").memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(imageView);
} catch (JSONException e) {
e.printStackTrace();
}
}
的AsyncTask
public class RetrieveData extends AsyncTask<String,Void,String>{
Context context;
RetrieveData(Context ctx) {
context = ctx;
}
private OnDataSendToActivity dataSendToActivity;
public RetrieveData(View.OnClickListener activity){
dataSendToActivity = (OnDataSendToActivity)activity;
}
@Override
protected String doInBackground(String... params) {
String profile_url = "http://192.168.12.252/getprofile.php";
String image_url = "http://192.168.12.252/imagelist.php";
String type = params[0];
if(type.equals("getProfile")){
try {
String user_name = params[1];
URL url = new URL(profile_url);
HttpURLConnection httpURLConnection = null;
httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
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");
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 = null;
result = bufferedReader.readLine();
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (IOException e) {
e.printStackTrace();
}
}else if(type.equals("getProfileImages")){
try {
String user_name = params[1];
URL url = new URL(image_url);
HttpURLConnection httpURLConnection = null;
httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("Image", "UTF-8")+"="+URLEncoder.encode("Image", "UTF-8")+"&"+URLEncoder.encode("user_name", "UTF-8")+"="+URLEncoder.encode(user_name, "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 = null;
result = bufferedReader.readLine();
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
dataSendToActivity.sendData(result);
}
接口
public interface OnDataSendToActivity {
void sendData(String str);
}
当我执行代码时,它会在此行崩溃
dataSendToActivity.sendData(result);
并且android监视器中的错误就像下面我不明白什么是错的
--------- beginning of crash
03-24 04:12:42.518 20964-20964/com.example.lenovo.maidfriends E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lenovo.maidfriends, PID: 20964
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.lenovo.maidfriends.OnDataSendToActivity.sendData(java.lang.String)' on a null object reference
at com.example.lenovo.maidfriends.RetrieveData.onPostExecute(RetrieveData.java:119)
at com.example.lenovo.maidfriends.RetrieveData.onPostExecute(RetrieveData.java:33)
at android.os.AsyncTask.finish(AsyncTask.java:667)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
我认为它说,它在结果中没有任何价值,但不是,结果中有一些东西。
有谁能让我知道什么是错的?
答案 0 :(得分:1)
您没有覆盖主要活动中的RetrieveData功能。您只是创建一个具有相同名称的新功能。
而不是将Asynctask称为:
RetrieveData retrieveData = new RetrieveData(this);
使用以下代码:
RetrieveData retrieveData = new RetrieveData(new RetrieveData(){
public void sendData(String response) {
// TODO Auto-generated method stub
String name="";
String age="";
String gender = "";
String id = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray("result");
JSONObject collegeData = result.getJSONObject(0);
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
name = collegeData.getString("name");
age = collegeData.getString("age");
gender = collegeData.getString("gender");
id = collegeData.getString("id");
EditText MyName = (EditText) findViewById(R.id.editText14);
MyName.setText(name);`enter code here`
EditText MyAge = (EditText) findViewById(R.id.editText21);
MyAge.setText(age);
EditText MyGender = (EditText) findViewById(R.id.editText22);
MyGender.setText(gender);
Picasso.with(this).load("http://192.168.12.252/"+id+"/profile.png").memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(imageView);
} catch (JSONException e) {
e.printStackTrace();
}
});
删除主要活动的sendData()
同样在RetrieveData类中,替换以下代码:
public RetrieveData(View.OnClickListener activity){
dataSendToActivity = (OnDataSendToActivity)activity;
}
使用:
public RetrieveData(OnDataSendToActivity activity){
dataSendToActivity = activity;
}
如果您遇到任何问题,请告诉我。
答案 1 :(得分:-1)
从sendData()
致电onPostExecute
简单的事情:
MainActivity ma=new MainActivity();
ma.sendData(result);