我得到了一个nullpointer异常

时间:2014-06-14 03:58:57

标签: android android-asynctask

我想保存ArrayList,以便我可以用于其他区域 出于这个原因,我正在返回imageurl

06-14 00:06:43.660: D/AndroidRuntime(14293): Shutting down VM
06-14 00:06:43.660: W/dalvikvm(14293): threadid=1: thread exiting with uncaught exception (group=0x40a601f8)
06-14 00:06:43.664: E/AndroidRuntime(14293): FATAL EXCEPTION: main
06-14 00:06:43.664: E/AndroidRuntime(14293): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.unccphotos/com.example.unccphotos.MainActivity}: java.lang.NullPointerException
06-14 00:06:43.664: E/AndroidRuntime(14293):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-14 00:06:43.664: E/AndroidRuntime(14293):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-14 00:06:43.664: E/AndroidRuntime(14293):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-14 00:06:43.664: E/AndroidRuntime(14293):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-14 00:06:43.664: E/AndroidRuntime(14293):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 00:06:43.664: E/AndroidRuntime(14293):    at android.os.Looper.loop(Looper.java:137)
06-14 00:06:43.664: E/AndroidRuntime(14293):    at android.app.ActivityThread.main(ActivityThread.java:4424)
06-14 00:06:43.664: E/AndroidRuntime(14293):    at java.lang.reflect.Method.invokeNative(Native Method)
06-14 00:06:43.664: E/AndroidRuntime(14293):    at java.lang.reflect.Method.invoke(Method.java:511)
06-14 00:06:43.664: E/AndroidRuntime(14293):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-14 00:06:43.664: E/AndroidRuntime(14293):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-14 00:06:43.664: E/AndroidRuntime(14293):    at dalvik.system.NativeStart.main(Native Method)
06-14 00:06:43.664: E/AndroidRuntime(14293): Caused by: java.lang.NullPointerException
06-14 00:06:43.664: E/AndroidRuntime(14293):    at com.example.unccphotos.MainActivity.onCreate(MainActivity.java:36)
06-14 00:06:43.664: E/AndroidRuntime(14293):    at android.app.Activity.performCreate(Activity.java:4465)
06-14 00:06:43.664: E/AndroidRuntime(14293):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-14 00:06:43.664: E/AndroidRuntime(14293):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-14 00:06:43.664: E/AndroidRuntime(14293):    ... 11 more


public class MainActivity extends Activity {
    ProgressDialog pd;

    ArrayList<String> imageurl = new ArrayList<String>();
    String url = "http://liisp.uncc.edu/~mshehab/api/photos.txt";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pd.setCancelable(false);

        new AsyncImage().execute(url);
    }
    private class AsyncImage extends AsyncTask<String, Void, ArrayList<String>> {


        protected ArrayList<String> doInBackground(String...params) {


            try {   
                 URL url = new URL(MainActivity.this.url);
                 HttpURLConnection con = (HttpURLConnection) url.openConnection();  
                 con.setRequestMethod("GET");   
                 con.connect();         
                int statusCode = con.getResponseCode(); 
                 if (statusCode == HttpURLConnection.HTTP_OK) { 
                     BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));   
                     String result ;    
                     while((result = in.readLine()) != null){                       
                    result=result+"\n"+result;
                    imageurl.add(result);
                    Log.d("result", imageurl.toString());

                    return imageurl;

                     }
            }}
                    catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }catch(NullPointerException e){
                        e.printStackTrace();
                    } catch (Exception e) {

                    } 

                    return imageurl;


        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd= ProgressDialog.show(MainActivity.this, "","Retreiving image URls...");
        }
@Override
    protected void onPostExecute(ArrayList<String> imageurl) {

            super.onPostExecute(imageurl);
            pd.dismiss();
        }

    }
}

4 个答案:

答案 0 :(得分:0)

在初始化之前,您似乎尝试使用ProgressDialog pd

pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); // is this line 36?

这是来自logcat的线索:

Caused by: java.lang.NullPointerException 06-14 00:06:43.664: E/AndroidRuntime(14293): at com.example.unccphotos.MainActivity.onCreate(MainActivity.java:36)

答案 1 :(得分:0)

Try to put this code:
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setCancelable(false);

After this line:
pd= ProgressDialog.show(MainActivity.this, "","Retreiving image URls...");

Resulted code:
@Override
protected void onPreExecute() {
    super.onPreExecute();
    pd= ProgressDialog.show(MainActivity.this, "","Retreiving image URls...");
    pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    pd.setCancelable(false);
}

答案 2 :(得分:0)

OK!它正在抛出NullPointerException,因为您在初始化之前使用pd。知道onCreate()之前会调用onPreExecute()。 从onCreate()

中删除这些行
setContentView(R.layout.activity_main);
//pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); <---delete
//pd.setCancelable(false); <--delete

并在AsyncTask onPreexecute() {/ p>}中进行以下更改:

@Override
protected void onPreExecute() {
    super.onPreExecute();
    pd= ProgressDialog.show(MainActivity.this, "","Retreiving image URls...");
    pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    pd.setCancelable(false);
    pd.show();
}

希望这会有所帮助:)

答案 3 :(得分:0)

我认为由于进度对话框而出现空指针exaption。你必须在初始化之前设置它的属性。

从oncreate方法

中删除以下两行
 pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
 pd.setCancelable(false);

在asyncTecute mehtod中添加asyncTask,如下所示

  @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd= ProgressDialog.show(MainActivity.this, "","Retreiving image URls...");
        pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pd.setCancelable(false);
    }

希望它能起作用。