我是一个新手没有足够的信息如何解决这个问题。这里的任何专业知识都会有所帮助。
当我运行应用程序时,nullpointexception。没有在他们的观点中显示任何内容
这就是我的PHP脚本:
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["image_name"] = $row["image_name"];
$product["image_type"] = $row["image_type"];
// push single product into final response array
array_push($response["products"], $product);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
我对异步任务的主要活动:
public class SimpleGridPatternViewActivity extends BaseActivity {
String url = "http://192.168.0.23/giveaways/tickets.php";
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_grid_pattern_view);
final GridPatternView<BaseAdapter> gridPatternView = (GridPatternView<BaseAdapter>) findViewById(R.id.parchment_view);
final List<GridPatternItemDefinition> gridPatternItemDefinitions = new ArrayList<GridPatternItemDefinition>();
FetchDataTask task = new FetchDataTask();
gridPatternItemDefinitions
.add(new GridPatternItemDefinition(0, 0, 6, 4));
gridPatternView
.addGridPatternGroupDefinition(gridPatternItemDefinitions);
gridPatternItemDefinitions.clear();
gridPatternItemDefinitions
.add(new GridPatternItemDefinition(0, 0, 2, 2));
gridPatternItemDefinitions
.add(new GridPatternItemDefinition(2, 0, 2, 2));
gridPatternItemDefinitions
.add(new GridPatternItemDefinition(4, 0, 2, 3));
gridPatternItemDefinitions
.add(new GridPatternItemDefinition(0, 2, 4, 4));
gridPatternItemDefinitions
.add(new GridPatternItemDefinition(4, 3, 2, 3));
gridPatternView
.addGridPatternGroupDefinition(gridPatternItemDefinitions);
gridPatternItemDefinitions.clear();
gridPatternItemDefinitions
.add(new GridPatternItemDefinition(0, 0, 2, 3));
gridPatternItemDefinitions
.add(new GridPatternItemDefinition(2, 0, 4, 4));
gridPatternItemDefinitions
.add(new GridPatternItemDefinition(0, 3, 2, 3));
gridPatternItemDefinitions
.add(new GridPatternItemDefinition(2, 4, 2, 2));
gridPatternItemDefinitions
.add(new GridPatternItemDefinition(4, 4, 2, 2));
gridPatternView
.addGridPatternGroupDefinition(gridPatternItemDefinitions);
task.execute(url);
gridPatternView.setAdapter(getProductsAdapter());
}
@Override
public int getLayoutResourceId() {
return R.layout.list_item_gridpatternview_picture;
}
class FetchDataTask extends AsyncTask<String, Void, String> {
private String msg;
@Override
protected String doInBackground(String... params) {
if (params == null)
return null;
// get url from params
String url = params[0];
try {
// create http connection
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
// connect
HttpResponse response = client.execute(httpget);
// get response
HttpEntity entity = response.getEntity();
if (entity == null) {
msg = "No response from server";
return null;
}
// get response content and convert it to json string
InputStream is = entity.getContent();
return streamToString(is);
} catch (IOException e) {
msg = "No Network Connection";
}
return null;
}
@Override
protected void onPostExecute(String arg0) {
try {
// convert json string to json array
JSONArray aJson = new JSONArray(products);
// create apps list
List<Model> mModels = new ArrayList<Model>();
for (int i = 0; i < aJson.length(); i++) {
JSONObject json = aJson.getJSONObject(i);
Model mdl = new Model();
mdl.setImage(json.getString("image_name"));
mdl.setType(json.getString("image_type"));
// add the app to apps list
mModels.add(mdl);
}
// notify the activity that fetch data has been complete
} catch (JSONException e) {
msg = "Invalid response";
return;
}
}
/**
* This function will convert response stream into json string
*
* @param is
* respons string
* @return json string
* @throws IOException
*/
public String streamToString(final InputStream is) throws IOException {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
throw e;
} finally {
try {
is.close();
} catch (IOException e) {
throw e;
}
}
return sb.toString();
}
}
}
我的适配器:
public class ProductsAdapter extends BaseAdapter {
private final int mLayoutResourceId;
public ProductsAdapter(final int layoutResourceId) {
super();
mLayoutResourceId = layoutResourceId;
}
private List<Model> mModels = new ArrayList<Model>();
@Override
public int getCount() {
return mModels.size();
}
@Override
public Object getItem(final int position) {
return mModels.get(position);
}
@Override
public long getItemId(final int position) {
return position;
}
@Override
public boolean hasStableIds() {
return true;
}
private View getView(final Context context, final View convertView,
final ViewGroup viewGroup) {
if (convertView == null) {
final LayoutInflater layoutInflater = LayoutInflater.from(context);
final View view = layoutInflater.inflate(mLayoutResourceId,
viewGroup, false);
return view;
}
return convertView;
}
@Override
public View getView(final int position, final View convertView,
final ViewGroup parent) {
final Context context = parent.getContext();
final View view = getView(context, convertView, parent);
Model model = (Model) getItem(position);
final ImageView imageView = (ImageView) view
.findViewById(R.id.list_item_picture_image_view);
imageView.setImageBitmap(null);
Picasso.with(context).load(model.getmUrl()).into(imageView);
final TextView textView = (TextView) view
.findViewById(R.id.list_item_picture_text_view);
textView.setText(model.getImage());
return view;
}
}