【发布时间】:2017-06-09 10:45:42
【问题描述】:
这是我点击切换租用时的视图,然后异步任务将运行,我得到结果,回收视图未更新,它显示图像按钮和回收视图空指针异常这是我的代码 Image here
public void rent_buy_selection(String type,String propertyID,String placeId,String buildingName,String bedRooms)
{
Property_listing1 listing1 = new Property_listing1(type,propertyID,placeId,buildingName,bedRooms);
listing1.execute();
}
public class Property_listing1 extends AsyncTask<Void, Void, Boolean> {
String PropertyID;
String placeId;
String property_type;
String BuildingName;
String BedRooms;
String image1,SenderId;
JSONArray detail;
JSONParser ob = new JSONParser();
JSONArray jArray;
Property_listing1(String type,String propertyID,String PlaceId,String buildingName,String bedRooms) {
Log.e("listFragment",":"+type);
PropertyID = propertyID;
placeId = PlaceId;
BuildingName = buildingName;
BedRooms = bedRooms;
property_type= type;
/* if(getActivity().getIntent().hasExtra("PropertyID")) {
PropertyID = getActivity().getIntent().getExtras().getString("PropertyID");
}
if(getActivity().getIntent().hasExtra("PropertyID")) {
placeId = getActivity().getIntent().getExtras().getString("placeId");
}
property_type = type;//getActivity().getIntent().getExtras().getString("property_type");
if(getActivity().getIntent().hasExtra("PropertyID")) {
BuildingName = getActivity().getIntent().getExtras().getString("BuildingName");
}
if(getActivity().getIntent().hasExtra("PropertyID")) {
BedRooms = getActivity().getIntent().getExtras().getString("BedRooms");
}
if(getActivity().getIntent().hasExtra("PropertyID")) {
SenderId = getActivity().getIntent().getExtras().getString("SenderId");
}*/
}
List<DataProperty> data=new ArrayList<>();
@Override
protected Boolean doInBackground(Void... params) {
try {
// Building Parameters
List<NameValuePair> paramss = new ArrayList<NameValuePair>();
paramss.add(new BasicNameValuePair("more_list", "1"));
paramss.add(new BasicNameValuePair("PropertyID", PropertyID));
paramss.add(new BasicNameValuePair("property_type", property_type));
paramss.add(new BasicNameValuePair("BuildingName", BuildingName));
paramss.add(new BasicNameValuePair("BedRooms", BedRooms));
paramss.add(new BasicNameValuePair("placeId", placeId));
try {
/* if (getActivity().getIntent().hasExtra("shared")) {
paramss.add(new BasicNameValuePair("shared", "1"));
paramss.add(new BasicNameValuePair("SenderId", SenderId));
}*/
}
catch (Exception e)
{
e.printStackTrace();
}
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = ob.makeHttpRequest("http://208.77.22.13/proprek_android/property_listing.php", "POST", paramss);
Log.d("Parameters",":"+paramss);
// check logcat for response
Log.d("Create Response", json.toString());
// check for success tag
try {
String success = json.getString("status");
Log.d("test123",":"+success);
if (success.equals("true")) {
jArray = json.getJSONArray("result");
// Extract data from json and store into ArrayList as class objects
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
DataProperty fishData = new DataProperty();
fishData.Title = json_data.getString("title");
fishData.Id = json_data.getString("id");
fishData.agentId = json_data.getString("agent_id");
fishData.prprtyId = json_data.getString("property_id");
fishData.Price = json_data.getString("price");
fishData.Area = json_data.getString("area");
fishData.agent_name = json_data.getString("first_name");
fishData.agent_pic = json_data.getString("fb_img");
fishData.Bedrooms = json_data.getString("bedrooms");
fishData.Bathrooms = json_data.getString("bathrooms");
fishData.Image = json_data.getString("agent_logo");
fishData.Phone = json_data.getString("phone");
fishData.property_Img = json_data.getString("image1");
fishData.agent_logo = json_data.getString("agent_logo");
fishData.company_name = json_data.getString("company_name");
fishData.shared = "0";
fishData.description = "0";
// data.add(fishData);
}
catch (Exception e)
{
e.printStackTrace();
}
Log.e("agent_name",":"+fishData.agent_name);
data.add(fishData);
}
} else {
// failed to create product
// flag = true;
throw new InterruptedException("test");
}
// Simulate network access.
//Thread.sleep(2000);
} catch (InterruptedException e) {
return false;
}
} catch (JSONException e) {
e.printStackTrace();
}
return true;
}
@Override
protected void onPostExecute(final Boolean success) {
String Title="";
try {
mAdapter = new PropertyAdapter(getActivity(), data);
mRVFishPrice.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
mRVFishPrice.setLayoutManager(new LinearLayoutManager(getActivity()));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
点击租借时
public PropertyAdapter(Context context, List<DataProperty> data){
Log.e("shared",":test");
SharedPreferences sharedPreferences = context.getSharedPreferences("reg_id",Context.MODE_PRIVATE);
user_id = sharedPreferences.getString("id","");
this.context=context;
// inflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.data=data;
PropertyID = ((Activity) context).getIntent().getExtras().getString("PropertyID");
placeId = ((Activity) context).getIntent().getExtras().getString("placeId");
property_type = ((Activity) context).getIntent().getExtras().getString("property_type");
BuildingName = ((Activity) context).getIntent().getExtras().getString("BuildingName");
BedRooms = ((Activity) context).getIntent().getExtras().getString("BedRooms");
SenderId = ((Activity) context).getIntent().getExtras().getString("SenderId");
}
如何解决这个问题?
【问题讨论】:
-
问题在这里
ImageButton img = (ImageButton)getActivity().findViewById(R.id.imageButton);。而不是使用getActivity(),您应该在onCreateView()方法中初始化ImageButton。 -
从
onPostExecute中删除此 ImageButton img = (ImageButton)getActivity().findViewById(R.id.imageButton); 并检查图像是否为空。这是如果(!img.equals("null")) {没有必要 -
ImageButton img = (ImageButton)getActivity().findViewById(R.id.imageButton);你不应该从你的 Fragment 访问 Activity 的视图。 getActivity 也可能返回 null。 -
你能解决这个现在共享偏好设置为空的问题吗@piyush@Vlad Matvienko
标签: android android-fragments android-asynctask sharedpreferences