我创建了php + mysql数据库服务器,创建了一个包含四个字段(id,name,info,img)的数据库,这四个字段是文本类型我把这个数据转换为json url:http://giclub.esy.es/show.php现在我想要将此json url加载到我的列表视图中,其中包含三个文本视图和图像视图 这是我的代码,但是当我运行时,我在logcat中收到了带有此消息的白页
public class ByCategoryMenu extends Activity {
RequestQueue requestQueue;
String url = "http://giclub.esy.es/show.php";
TextView textView;
ListView listView;
ArrayList<listitme> listitmes = new ArrayList<listitme>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bycategorydetails);
textView = (TextView) findViewById(R.id.textView);
listView = (ListView) findViewById(R.id.listView);
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("allstudents");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject respons = jsonArray.getJSONObject(i);
String id = respons.getString("id");
String name = respons.getString("name");
String info = respons.getString("info");
String img = respons.getString("img");
listitmes.add(new listitme(id, name, info, img));
listAllItme();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", "ERROR");
}
}
);
requestQueue.add(jsonObjectRequest);
}
public void listAllItme() {
listAdpter lA = new listAdpter(listitmes);
listView.setAdapter(lA);
}
class listAdpter extends BaseAdapter {
ArrayList<listitme> listA = new ArrayList<listitme>();
public listAdpter(ArrayList<listitme> listA) {
this.listA = listA;
}
@Override
public int getCount() {
return listA.size();
}
@Override
public Object getItem(int position) {
return listA.get(position).id;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.row_itm, null);
TextView id = (TextView) view.findViewById(R.id.textView_id);
TextView name = (TextView) view.findViewById(R.id.textView_name);
TextView info = (TextView) view.findViewById(R.id.textView_info);
ImageView img = (ImageView) view.findViewById(R.id.imageView);
id.setText(listA.get(position).id);
name.setText(listA.get(position).name);
info.setText(listA.get(position).info); Picasso.with(ByCategoryMenu.this).load("http://giclub.esy.es/image/" + listA.get(position).img).into(img);
return view;
}
}
}
我创建了listitem类
public class listitme {
public String id;
public String name;
public String info;
public String img;
public listitme( String id, String name, String info, String img) {
this.id = id;
this.name = name;
this.info = info;
this.img = img;
}
}
row_itm.xml
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/textView_id"
android:textColor="#fa0000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView_name"
android:textColor="@color/accent_material_light" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView_info" />
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
/>
bycategorydetalis.xml
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"/>
show.php
<?php
include("config.php");
header('Content-Type: application/json; charset=utf-8');
$result = mysqli_query($db, 'SELECT * FROM student');
$stu = array();
while ($row = mysqli_fetch_array($result)) {
array_push($stu,$row);
}
echo "{allstudents : ".json_encode($stu);
?>
logcat是
D/dalvikvm: create interp thread : stack size=128KB
D/dalvikvm: create new thread
D/dalvikvm: new thread created
D/dalvikvm: update thread list
D/dalvikvm: threadid=20: interp stack at 0x626bf000
D/dalvikvm: threadid=20: created from interp
D/dalvikvm: start new thread
D/dalvikvm: threadid=20: notify debugger
D/dalvikvm: threadid=20 (Picasso-refQueue): calling run()
D/AndroidRuntime: Shutting down VM
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41d2dcf8)
W/dalvikvm: threadid=1: uncaught exception occurred
W/System.err: java.lang.IllegalArgumentException: Target must not be null.
W/System.err: at com.squareup.picasso.RequestCreator.into(RequestCreator.java:618)
W/System.err: at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
W/System.err: at com.example.amr.fujeraapp.ByCategoryMenu$listAdpter.getView(ByCategoryMenu.java:136)
W/System.err: at android.widget.AbsListView.obtainView(AbsListView.java:2338)
W/System.err: at android.widget.ListView.makeAndAddView(ListView.java:1812)
W/System.err: at android.widget.ListView.fillDown(ListView.java:698)
W/System.err: at android.widget.ListView.fillFromTop(ListView.java:759)
W/System.err: at android.widget.ListView.layoutChildren(ListView.java:1645)
W/System.err: at android.widget.AbsListView.onLayout(AbsListView.java:2149)
W/System.err: at android.view.View.layout(View.java:15125)
W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:4862)
W/System.err: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
W/System.err: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
W/System.err: at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
W/System.err: at android.view.View.layout(View.java:15125)
W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:4862)
W/System.err: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
W/System.err: at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
W/System.err: at android.view.View.layout(View.java:15125)
W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:4862)
W/System.err: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
W/System.err: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
W/System.err: at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
W/System.err: at android.view.View.layout(View.java:15125)
W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:4862)
W/System.err: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
W/System.err: at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
W/System.err: at android.view.View.layout(View.java:15125)
W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:4862)
W/System.err: at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2317)
W/System.err: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2023)
W/System.err: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189)
W/System.err: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6223)
W/System.err: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
W/System.err: at android.view.Choreographer.doCallbacks(Choreographer.java:591)
W/System.err: at android.view.Choreographer.doFrame(Choreographer.java:560)
W/System.err: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
W/System.err: at android.os.Handler.handleCallback(Handler.java:808)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:103)
W/System.err: at android.os.Looper.loop(Looper.java:193)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5333)
W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
W/System.err: at dalvik.system.NativeStart.main(Native Method)
W/dalvikvm: threadid=1: calling UncaughtExceptionHandler
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.amr.fujeraapp, PID: 13372
java.lang.IllegalArgumentException: Target must not be null.
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:618)
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
at com.example.amr.fujeraapp.ByCategoryMenu$listAdpter.getView(ByCategoryMenu.java:136)
at android.widget.AbsListView.obtainView(AbsListView.java:2338)
at android.widget.ListView.makeAndAddView(ListView.java:1812)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1645)
at android.widget.AbsListView.onLayout(AbsListView.java:2149)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
at android.view.View.layout(View.java:15125)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2317)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2023)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6223)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
这个问题的任何帮助?
答案 0 :(得分:0)
在处理来自凌空而非for循环的响应后,在适配器中设置数据 所以,在for循环之外调用listAllItme()方法。