我正在尝试从服务器获取需要添加到列表视图的图像链接和文本。
这是来自服务器的结果:
{"result":
[{"url":"http:\/\/website.com\/ImageUpload\/pic\/85.png"},
{"name":"Vikram Sharma"},
{"url":"http:\/\/website.com\/ImageUpload\/pic\/67.png"},
{"name":"Sudhir Sharma"}]
}
参考:https://www.simplifiedcoding.net/android-upload-image-using-php-mysql-android-studio/
// ImageListView.java
public class ImageListView extends AppCompatActivity implements AdapterView.OnItemClickListener {
private ListView listView;
public static final String GET_IMAGE_URL="http://website.com/ImageUpload/getAllImages.php";
public GetAlImages getAlImages;
public static final String BITMAP_ID = "BITMAP_ID";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_list_view);
listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(this);
getURLs();
}
private void getImages(){
class GetImages extends AsyncTask<Void,Void,Void>{
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(ImageListView.this,"Downloading images...","Please wait...",false,false);
}
@Override
protected void onPostExecute(Void v) {
super.onPostExecute(v);
loading.dismiss();
System.out.println("Zinda 1");
//Toast.makeText(ImageListView.this,"Success",Toast.LENGTH_LONG).show();
CustomList customList = new CustomList(ImageListView.this,GetAlImages.imageURLs,GetAlImages.name,GetAlImages.bitmaps);
System.out.println("Zinda 2");
listView.setAdapter(customList);
}
@Override
protected Void doInBackground(Void... voids) {
try {
System.out.println("Zinda 3");
getAlImages.getAllImages();
System.out.println("Zinda 4");
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
GetImages getImages = new GetImages();
getImages.execute();
}
private void getURLs() {
class GetURLs extends AsyncTask<String,Void,String>{
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(ImageListView.this,"Loading...","Please Wait...",true,true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
getAlImages = new GetAlImages(s);
getImages();
}
@Override
protected String doInBackground(String... strings) {
BufferedReader bufferedReader = null;
try {
URL url = new URL(strings[0]);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while((json = bufferedReader.readLine())!= null){
sb.append(json+"\n");
}
return sb.toString().trim();
}catch(Exception e){
return null;
}
}
}
GetURLs gu = new GetURLs();
gu.execute(GET_IMAGE_URL);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(this, ViewFullImage.class);
intent.putExtra(BITMAP_ID,i);
startActivity(intent);
}
}
// CustomList.java
public class CustomList extends ArrayAdapter<String> {
private String[] urls;
private String[] name;
private Bitmap[] bitmaps;
private Activity context;
public CustomList(Activity context, String[] urls, String[] name,Bitmap[] bitmaps) {
super(context, R.layout.image_list_view, urls);
this.context = context;
this.urls= urls;
System.out.println("Singh 1");
this.name= name;
this.bitmaps= bitmaps;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.image_list_view, null, true);
TextView textViewURL = (TextView) listViewItem.findViewById(R.id.textViewURL);
System.out.println("Singh 2");
TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
System.out.println("Singh 3");
ImageView image = (ImageView) listViewItem.findViewById(R.id.imageDownloaded);
textViewURL.setText(urls[position]);
System.out.println("Singh 4");
textViewName.setText(name[position]);
System.out.println("Singh 5");
image.setImageBitmap(Bitmap.createScaledBitmap(bitmaps[position], 100, 50, false));
System.out.println("Singh 6");
return listViewItem;
}
}
// GetAlImages.java
public class GetAlImages {
public static String[] imageURLs;
public static String[] name;
public static Bitmap[] bitmaps;
public static final String JSON_ARRAY="result";
public static final String NAME = "name";
public static final String IMAGE_URL = "url";
private String json;
private JSONArray urls;
public GetAlImages(String json){
this.json = json;
try {
JSONObject jsonObject = new JSONObject(json);
urls = jsonObject.getJSONArray(JSON_ARRAY);
} catch (JSONException e) {
System.out.println("Matru 1");
e.printStackTrace();
}
}
private Bitmap getImage(JSONObject jo){
URL url = null;
// String name =null;
Bitmap image = null;
try {
url = new URL(jo.getString(IMAGE_URL));
// name = new String(jo.getString(NAME));
image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
System.out.println("MAtru 2");
e.printStackTrace();
} catch (IOException e) {
System.out.println("MAtru 3");
e.printStackTrace();
} catch (JSONException e) {
System.out.println("Matru 4");
e.printStackTrace();
}
return image;
}
public void getAllImages() throws JSONException {
bitmaps = new Bitmap[urls.length()];
imageURLs = new String[urls.length()];
name = new String[urls.length()];
for(int i=0;i<urls.length();i++){
name[i] = urls.getJSONObject(i).getString(NAME);
System.out.println("MAtru 5");
imageURLs[i] = urls.getJSONObject(i).getString(IMAGE_URL);
JSONObject jsonObject = urls.getJSONObject(i);
System.out.println("MAtru 6");
bitmaps[i]=getImage(jsonObject);
}
}
}
// Logcat获取错误详情
01-02 16:34:25.913 20237-20237/? I/Process: Sending signal. PID: 20237 SIG: 9
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: FATAL EXCEPTION: main
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: Process: com.package.name, PID: 20237
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: java.lang.NullPointerException
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:589)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at com.package.listview.listview.CustomList.getView(CustomList.java:45)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.widget.AbsListView.obtainView(AbsListView.java:2240)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.widget.ListView.onMeasure(ListView.java:1175)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.View.measure(View.java:16558)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.View.measure(View.java:16558)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.View.measure(View.java:16558)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.View.measure(View.java:16558)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.View.measure(View.java:16558)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.View.measure(View.java:16558)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.View.measure(View.java:16558)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1923)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1120)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1302)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1007)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5677)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:574)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:544)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5021)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
01-02 16:34:25.913 20237-20237/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
01-02 16:34:25.943 15342-15537/? I/ActivityManager: Process com.package.name (pid 20237) has died.
01-02 16:34:25.943 15342-15537/? W/ActivityManager: Force removing ActivityRecord{2e9ad3ac u0 com.package.name/com.package.listview.listview.ImageListView t15}: app died, no saved state
01-02 16:34:25.953 15342-15383/? W/InputDispatcher: channel '2ea211d8 com.package.name/com.package.listview.listview.ImageListView (server)' ~ Consumer closed input channel or an error occurred. events=0x9
01-02 16:34:25.953 15342-15383/? E/InputDispatcher: channel '2ea211d8 com.package.name/com.package.listview.listview.ImageListView (server)' ~ Channel is unrecoverably broken and will be disposed!
01-02 16:34:25.953 15342-15383/? W/InputDispatcher: channel '2e997ac0 com.package.name/com.package.name.Login (server)' ~ Consumer closed input channel or an error occurred. events=0x9
01-02 16:34:25.953 15342-15383/? E/InputDispatcher: channel '2e997ac0 com.package.name/com.package.name.Login (server)' ~ Channel is unrecoverably broken and will be disposed!
01-02 16:34:25.953 15342-15442/? W/InputDispatcher: Attempted to unregister already unregistered input channel '2ea211d8 com.package.name/com.package.listview.listview.ImageListView (server)'
01-02 16:34:25.953 15342-15509/? W/InputDispatcher: Attempted to unregister already unregistered input channel '2e997ac0 com.package.name/com.package.name.Login (server)'
答案 0 :(得分:0)
可以使用logcat
中的以下行识别问题 at com.package.listview.listview.CustomList.getView(CustomList.java:45)
这意味着在第45行的文件CustomList.java中,您可能使用了未正确初始化的内容或者甚至不存在的内容