gpt4 book ai didi

java - Android 应用程序在设备和模拟器上崩溃(线程问题?)

转载 作者:行者123 更新时间:2023-12-02 05:47:09 25 4
gpt4 key购买 nike

我制作了一个应用程序,它解析 JSON 文件,然后使用该 JSON 文件中的 URL 在 GridView 。但是,我认为问题在于应用程序在 JsonParsing 类中查找 main 方法,因为当我尝试在模拟器上运行它时,系统甚至不会打印其中的第一行代码我的 onCreate() 方法。这可能是与线程有关的问题吗?

05-30 10:21:50.660: I/System.out(867):  We made it this far
05-30 10:21:51.090: D/dalvikvm(867): GC_FOR_ALLOC freed 106K, 6% free 3237K/3412K, paused 47ms, total 48ms
05-30 10:21:51.100: I/dalvikvm-heap(867): Grow heap (frag case) to 4.299MB for 1127536-byte allocation
05-30 10:21:51.350: D/dalvikvm(867): GC_FOR_ALLOC freed 0K, 4% free 4338K/4516K, paused 126ms, total 127ms
05-30 10:21:51.860: D/AndroidRuntime(867): Shutting down VM
05-30 10:21:51.860: W/dalvikvm(867): threadid=1: thread exiting with uncaught exception (group=0xb1a97ba8)
05-30 10:21:51.870: E/AndroidRuntime(867): FATAL EXCEPTION: main
05-30 10:21:51.870: E/AndroidRuntime(867): Process: com.example.bitmapdisplay, PID: 867
05-30 10:21:51.870: E/AndroidRuntime(867): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bitmapdisplay/com.example.bitmapdisplay.MainActivity}: java.lang.NullPointerException
05-30 10:21:51.870: E/AndroidRuntime(867): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-30 10:21:51.870: E/AndroidRuntime(867): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-30 10:21:51.870: E/AndroidRuntime(867): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-30 10:21:51.870: E/AndroidRuntime(867): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-30 10:21:51.870: E/AndroidRuntime(867): at android.os.Handler.dispatchMessage(Handler.java:102)
05-30 10:21:51.870: E/AndroidRuntime(867): at android.os.Looper.loop(Looper.java:136)
05-30 10:21:51.870: E/AndroidRuntime(867): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-30 10:21:51.870: E/AndroidRuntime(867): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 10:21:51.870: E/AndroidRuntime(867): at java.lang.reflect.Method.invoke(Method.java:515)
05-30 10:21:51.870: E/AndroidRuntime(867): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-30 10:21:51.870: E/AndroidRuntime(867): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-30 10:21:51.870: E/AndroidRuntime(867): at dalvik.system.NativeStart.main(Native Method)
05-30 10:21:51.870: E/AndroidRuntime(867): Caused by: java.lang.NullPointerException
05-30 10:21:51.870: E/AndroidRuntime(867): at com.example.bitmapdisplay.MainActivity.fillGridView(MainActivity.java:83)
05-30 10:21:51.870: E/AndroidRuntime(867): at com.example.bitmapdisplay.MainActivity.onCreate(MainActivity.java:71)
05-30 10:21:51.870: E/AndroidRuntime(867): at android.app.Activity.performCreate(Activity.java:5231)
05-30 10:21:51.870: E/AndroidRuntime(867): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-30 10:21:51.870: E/AndroidRuntime(867): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-30 10:21:51.870: E/AndroidRuntime(867): ... 11 more

这是我的 Java 代码(抱歉评论数量较少)

package com.example.bitmapdisplay;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.fasterxml.jackson.core.JsonParseException;

import android.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;


public class MainActivity extends Activity {

/**
* Bitmap items na dimage displaying
*/
Bitmap image;
BitmapDrawable bd;
ImageView temp;
ProgressDialog pd;

/**
* JSON URL
*/
URL url;

/**
* Data from JSON file
*/
ArrayList<String> urls;
ArrayList<ImageView> images;

JsonParsing obj;

int counter;
@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println(" We made it this far");
super.onCreate(savedInstanceState);
setContentView(com.example.bitmapdisplay.R.layout.image_container);

counter = 0;
images = new ArrayList<ImageView>();


new TheTask().execute();

fillGridView();
}

/**
* Fills the ListView
*/
private void fillGridView() {

ArrayAdapter<ImageView> adapter = new ArrayAdapter<ImageView>(this, com.example.bitmapdisplay.R.layout.image_container,images);

GridView grid = (GridView) findViewById(com.example.bitmapdisplay.R.id.gvImages);

grid.setAdapter(adapter);

}

public class URLArray extends AsyncTask<String, Void, Void> {

@Override
protected Void doInBackground(String... json_link_str) {

try {
URL json_link = new URL("http://api.tumblr.com/v2/blog/humansofnewyork.com/posts?api_key=7ag2CJXOuxuW3vlVS5wQG6pYA6a2ZQcSCjzZsAp2pDbVwf3xEk&notes_info=true&filter=text");

JsonParsing parse_images = new JsonParsing(json_link, "humansofnewyork");
try {
parse_images.parseFile();
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
obj = parse_images;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

protected Void onPostExecute(Void...voids ) {
urls = obj.urlList;
return null;
}

}


class TheTask extends AsyncTask<Void,Void,Void>
{

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd.show();
}


@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try
{
int counter = 0;
for (ImageView temp : images) {

image = downloadBitmap(urls.get(counter));
temp.setImageBitmap(image);
images.add(temp);
counter++;
}
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
if(image!=null)
{
fillGridView();
}

}
}
private Bitmap downloadBitmap(String url) {
// initilize the default HTTP client object
final DefaultHttpClient client = new DefaultHttpClient();

//forming a HttoGet request
final HttpGet getRequest = new HttpGet(url);
try {

HttpResponse response = client.execute(getRequest);

//check 200 OK for success
final int statusCode = response.getStatusLine().getStatusCode();

if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode +
" while retrieving bitmap from " + url);
return null;

}

final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
// getting contents from the stream
inputStream = entity.getContent();

// decoding stream data back into image Bitmap that android understands
image = BitmapFactory.decodeStream(inputStream);


} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
// You Could provide a more explicit error message for IOException
getRequest.abort();
Log.e("ImageDownloader", "Something went wrong while" +
" retrieving bitmap from " + url + e.toString());
}

return image;
}


}

图像容器布局 XML 文件。它只是应该用于放置 GridView 图像:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >


</ImageView>

最佳答案

您尚未初始化在 TheTask 异步任务的 onPreExecute 中使用的 pd

onPreExecute中,在pd.show之前添加pd = new ProgressDialog(MainActivity .this)

关于java - Android 应用程序在设备和模拟器上崩溃(线程问题?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23955636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com