0

私はある時からこの問題で立ち往生しています。これは私のコードです

public class PilgrimFriendActivity extends Activity {

    private String DOMAIN = "http://";

    private String LAWS_URL = "";
    private String LOCATIONS_URL = "";

    private ListView list;
    private TextView content;
    private Law law  ;
    private Location location;
    private ProgressDialog dialog;
    private SharedPreferences preferences;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        preferences = getSharedPreferences("com.hackontech.pilgrim_preferences", MODE_PRIVATE);
        this.DOMAIN += preferences.getString("DOMAIN","192.168.0.104");
        this.LAWS_URL = DOMAIN + "/pilgrimfriend/main/getAllLaws";
        this.LOCATIONS_URL = DOMAIN + "/pilgrimfriend/main/getAllLocations";

        dialog = new ProgressDialog(this);
        content = (TextView) findViewById(R.id.content);
        list = (ListView) findViewById(R.id.laws);
        this.lLaws();

    }


    public void lLaws() {

        final class RequestTask extends AsyncTask<String, String, String>{

            private String URL = null;

            @Override
            protected String doInBackground(String... uri) {
                String result = null;
                URL = uri[0];
                try {

                    //Create default http client
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpGet httpget = new HttpGet(URL);
                    Log.v("URL",URL);
                    HttpResponse response = httpclient.execute(httpget);
                    //Retrieve the response body or "entity"
                    HttpEntity entity = response.getEntity();
                    //Return the entity as String
                    result = EntityUtils.toString(entity);
                    Log.v("result",result.toString());

                } catch (Exception e) {
                    return e.getMessage();
                }

                return result;
            }

            @Override
            protected void onPostExecute(String result) {
                //super.onPostExecute(result);
                dialog.dismiss();
                try{

                    JSONArray jArray = new JSONArray(result);

                    JSONObject json_data;
                    ArrayList<String> items = new ArrayList<String>();
                    for(int i=0; i < jArray.length() ; i++) {
                        json_data = jArray.getJSONObject(i);
                        int id=json_data.getInt("id");
                        String title=json_data.getString("title");
                        String scholor =json_data.getString("scholor");
                        items.add(title+" ("+scholor+")");
                    }

                    content.setText(result);
                    ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(getApplicationContext(),  
                            android.R.layout.simple_list_item_1, items);
                    list.setAdapter(mArrayAdapter);

                    Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();

                }
                catch(JSONException e) {
                    //content.setText(e.getMessage());
                }
                //Do anything with response..
            }

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

                dialog.setTitle("Downloading Laws Data");
                dialog.show();
            }

        }

        new RequestTask().execute(LAWS_URL);
    }

私はすべてのメソッドが機能していますが、onPostExecute私が行ったようにビュー要素を変更するcontent.setText(result)と、アプリケーションがすぐに停止します。何が問題なのですか?

ここにあるLogCat

12-07 22:44:33.302: E/AndroidRuntime(778): FATAL EXCEPTION: main
12-07 22:44:33.302: E/AndroidRuntime(778): java.lang.NullPointerException
12-07 22:44:33.302: E/AndroidRuntime(778):  at com.hackontech.pilgrim.PilgrimFriendActivity$1RequestTask.onPostExecute(PilgrimFriendActivity.java:120)
12-07 22:44:33.302: E/AndroidRuntime(778):  at com.hackontech.pilgrim.PilgrimFriendActivity$1RequestTask.onPostExecute(PilgrimFriendActivity.java:1)
12-07 22:44:33.302: E/AndroidRuntime(778):  at android.os.AsyncTask.finish(AsyncTask.java:417)
12-07 22:44:33.302: E/AndroidRuntime(778):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
12-07 22:44:33.302: E/AndroidRuntime(778):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
12-07 22:44:33.302: E/AndroidRuntime(778):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-07 22:44:33.302: E/AndroidRuntime(778):  at android.os.Looper.loop(Looper.java:123)
12-07 22:44:33.302: E/AndroidRuntime(778):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-07 22:44:33.302: E/AndroidRuntime(778):  at java.lang.reflect.Method.invokeNative(Native Method)
12-07 22:44:33.302: E/AndroidRuntime(778):  at java.lang.reflect.Method.invoke(Method.java:507)
12-07 22:44:33.302: E/AndroidRuntime(778):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-07 22:44:33.302: E/AndroidRuntime(778):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-07 22:44:33.302: E/AndroidRuntime(778):  at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

0

何らかの理由で、onPostExecuteメソッドが終了すると、メソッドはすべての UI 更新を一度に実行します。

例えば:

@Override
protected Void doInBackground() {
    someTextView.setText("Do In Background Finished!");
}

@Override
protected void onPostExecute () {
    someTextView.setText("Post Execute Started");
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // ..
    }
    someTextView.setText("Post Execute Finished");
}

「Do In Background Finished!」と表示されます。5 秒後 (onPostExecute終了時) は、"Post Execute Started" でテキストを更新し、すぐに "Post Execute Finished" でテキストを更新します。

于 2015-05-06T14:01:20.950 に答える