在服务器的列表视图中显示文本和图像
Posted
技术标签:
【中文标题】在服务器的列表视图中显示文本和图像【英文标题】:Show text and images in listview from server 【发布时间】:2016-04-05 21:15:15 【问题描述】:我正在尝试在 listview 上显示图像和文本,但它在 CustomList.java 上给了我一些空指针异常错误,即image.setImageBitmap(Bitmap.createScaledBitmap(bitmaps[position], 100, 50, false));
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-01 10:35:43.882 5415-5415/? I/System.out: Singh 2
01-01 10:35:43.882 5415-5415/? I/System.out: Singh 3
01-01 10:35:43.882 5415-5415/? D/androidRuntime: Shutting down VM
01-01 10:35:43.882 5415-5415/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x5cceeb20)
01-01 10:35:43.882 1893-1990/? D/BstCommandProcessor-Application: Application crash has been observed.
01-01 10:35:43.882 1893-1990/? W/BstCommandProcessor-Application: in sendHttpRequest, requestType is of CRASH_APP type but one of the requiredInfo is NULL, crashedApp = com.bluestacks.BstCommandProcessor.BstCrashedAppInfo@2e7ce858
01-01 10:35:43.882 1893-1990/? D/BstCommandProcessor-Application: in sendHttpRequest, request to send to (fqdn): http://10.0.2.2:2861/AppCrashedInfo
01-01 10:35:43.882 1893-1990/? D/BstCommandProcessor-Application: data: "packageName":"com.package.name","shortPackageName":"com.package.name","versionCode":1,"versionName":"1.0"
01-01 10:35:43.882 5415-5415/? I/Process: Sending signal. PID: 5415 SIG: 9
01-01 10:35:43.882 5415-5415/? D/AndroidRuntime: procName from cmdline: com.package.name
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: in writeCrashedAppName, pkgName :com.package.name
01-01 10:35:43.882 5415-5415/? D/AndroidRuntime: file written successfully with content: com.package.name StringBuffer : ;com.package.name
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: FATAL EXCEPTION: main
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: Process: com.package.name, PID: 5415
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: java.lang.NullPointerException
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:589)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at com.package.listview.CustomList.getView(CustomList.java:44)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.widget.AbsListView.obtainView(AbsListView.java:2263)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.widget.ListView.onMeasure(ListView.java:1175)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.View.measure(View.java:16558)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.View.measure(View.java:16558)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.View.measure(View.java:16558)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.View.measure(View.java:16558)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.View.measure(View.java:16558)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.View.measure(View.java:16558)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.View.measure(View.java:16558)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1923)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1120)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1302)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1007)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5677)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:574)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:544)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5021)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
01-01 10:35:43.882 5415-5415/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
01-01 10:35:43.892 1708-1905/? I/ActivityManager: Process com.package.name (pid 5415) has died.
01-01 10:35:43.892 1708-1905/? W/ActivityManager: Force removing ActivityRecord2e94b214 u0 com.package.name/com.package.listview.ImageListView t10: app died, no saved state
01-01 10:35:43.892 1708-1719/? I/WindowState: WIN DEATH: Window2e9ee6cc u0 com.package.name/com.package.listview.ImageListView
01-01 10:35:43.892 1708-1905/? D/ActivityManager: TopActivityInfo, pkgName: com.package.name activityName: com.package.name/.Login callingPackage: bstSpecialAppKeyboardHandlingEnabled = false
01-01 10:35:43.892 1708-1922/? I/WindowState: WIN DEATH: Window2e949cb4 u0 Loading... EXITING
01-01 10:35:43.902 1708-1752/? W/InputDispatcher: channel '2e729764 com.package.name/com.package.name.Login (server)' ~ Consumer closed input channel or an error occurred. events=0x9
01-01 10:35:43.902 1708-1752/? E/InputDispatcher: channel '2e729764 com.package.name/com.package.name.Login (server)' ~ Channel is unrecoverably broken and will be disposed!
01-01 10:35:43.902 1908-1908/? D/GuidanceScreen: event === app_launch
【问题讨论】:
【参考方案1】:尝试在这样的适配器 getView() 方法中使用 convertView 而不是 listViewItem,
@Override
public View getView(int position, View convertView, ViewGroup parent)
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.image_list_view, null);
TextView textViewURL = (TextView) convertView.findViewById(R.id.textViewURL);
System.out.println("Singh 2");
TextView textViewName = (TextView) convertView.findViewById(R.id.textViewName);
System.out.println("Singh 3");
ImageView image = (ImageView) convertView.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 convertView;
【讨论】:
它一直在加载中,请稍候...消息并没有显示任何结果,上面的代码是好的。我添加了 textview 字符串名称。参考来自simplifiedcoding.net/… 它使应用程序崩溃,仅此而已!以上是关于在服务器的列表视图中显示文本和图像的主要内容,如果未能解决你的问题,请参考以下文章