public class MainActivity extends Activity {
private static final int GET_MOVIE = 1000;
private Button btn1;
String movie;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button1);
movie = btn1.getText().toString();
Log.e("kjw", "outlistener_main");
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MovieActivity.class);
i.putExtra("movie", movie);
startActivityForResult(i, GET_MOVIE);
}
});
}
public void OnActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == GET_MOVIE && resultCode == RESULT_OK) {
movie = data.getExtras().getString("return");
btn1.setText(movie);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
public class MovieActivity extends ListActivity {
private ProgressDialog pDialog;
// URL to get contacts JSON
private String url = "http://www.kobis.or.kr/kobisopenapi/webservice/rest/boxoffice/searchDailyBoxOfficeList.json?key=165fd244a33a11b50938eacd1fcbe6aa&targetDt=20140107";
Calendar calendar = Calendar.getInstance();
String curY = String.valueOf(calendar.get(Calendar.YEAR));
String curM = String.valueOf(calendar.get(Calendar.MONTH) + 1);
String curD = String.valueOf(calendar.get(Calendar.DATE) - 1);
String day = curY + curM + curD;
// JSON Node names
private static final String TAG_BOXOFFICERESULT = "boxOfficeResult";
private static final String TAG_DAILYBOXOFFICELIST = "dailyBoxOfficeList";
private static final String TAG_RNUM = "rnum";
private static final String TAG_MOVIENM = "movieNm";
private static final String TAG_OPENDT = "openDt";
// contacts JSONArray
JSONArray movies = null;
// Hashmap for ListView
ArrayList<HashMap<String, String> > movieList;
TextView t;
String movie;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Response", ">" + "a" );
setContentView(R.layout.activity_movie);
Log.d("Response", ">" + "b" );
movieList = new ArrayList<HashMap<String, String>>();
Log.d("Response", ">" + "c" );
// Calling async task to get json
new GetContacts().execute();
Log.d("Response", ">" + "d" );
t = (TextView) findViewById(R.id.movienm);
movie = t.getText().toString();
ListView lv = (ListView) findViewById(android.R.id.list);
Log.e("kjw", "outlistener");
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent i = getIntent();
movie = i.getStringExtra("movie");
i.putExtra("return", movie);
setResult(RESULT_OK, i);
finish();
}
});
}
/**
* Async task class to get json by making HTTP call
* */
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MovieActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
Log.d("Response", ">" + "1" );
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response", ">" + "2" );
if (jsonStr != null) {
Log.d("Response", ">" + "22" );
try {
Log.d("Response", ">" + "222" );
JSONObject jsonObj = new JSONObject(jsonStr);
Log.d("Response", ">" + "2222" );
JSONObject a = jsonObj.getJSONObject(TAG_BOXOFFICERESULT);
Log.d("Response", ">" + "22222" );
movies = a.getJSONArray(TAG_DAILYBOXOFFICELIST);
for (int i=0; i <movies.length(); i++) {
Log.d("Response", ">" + "222222" );
JSONObject c = movies.getJSONObject(i);
String rnum = c.getString(TAG_RNUM);
String movienm = c.getString(TAG_MOVIENM);
String opendt = c.getString(TAG_OPENDT);
// tmp hashmap for single contact
HashMap<String, String> movie = new HashMap<String, String>();
// adding each child node to HashMap key => value
movie.put(TAG_RNUM, rnum);
movie.put(TAG_OPENDT, opendt);
movie.put(TAG_MOVIENM, movienm);
Log.d("Response", ">" + "2222222" );
// adding contact to contact list
movieList.add(movie);
Log.d("Response", ">" + "2222222222" );
}
} catch (JSONException e) {
e.printStackTrace();
Log.d("Response: ", "> " + "2b");
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Log.d("Response", ">" + "2000");
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(MovieActivity.this, movieList, R.layout.list_item,
new String[] { TAG_RNUM, TAG_OPENDT, TAG_MOVIENM }, new int[] { R.id.rnum, R.id.opendt, R.id.movienm });
setListAdapter(adapter);
}
}
}
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
public ServiceHandler() {
}
/**
* Making service call
* @url - url to make request
* @method - http request method
* */
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/**
* Making service call
* @url - url to make request
* @method - http request method
* @params - http request params
* */
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
我的 Logcat 跟踪
01-09 22:13:08.625: E/AndroidRuntime(27625): 致命异常: main 01-09 22:13:08.625: E/AndroidRuntime(27625): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. example.test_06/com.example.test_06.MovieActivity}:java.lang.NullPointerException 01-09 22:13:08.625:E/AndroidRuntime(27625):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) 01 -09 22:13:08.625: E/AndroidRuntime(27625): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 01-09 22:13:08.625: E/AndroidRuntime(27625): 在 android.app .ActivityThread.access$700(ActivityThread.java:140) 01-09 22:13:08.625: E/AndroidRuntime(27625): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237) 01-09 22: 13:08.625: E/AndroidRuntime(27625): 在 android.os.Handler.dispatchMessage(Handler.java:99) 01-09 22:13:08.625: E/AndroidRuntime(27625): 在 android.os.Looper.loop(Looper.java:137) 01-09 22:13:08.625: E/AndroidRuntime(27625): 在android.app.ActivityThread.main(ActivityThread.java:4946) 01-09 22:13:08.625: E/AndroidRuntime(27625): 在 java.lang.reflect.Method.invokeNative(Native Method) 01-09 22:13 :08.625: E/AndroidRuntime(27625): at java.lang.reflect.Method.invoke(Method.java:511) 01-09 22:13:08.625: E/AndroidRuntime(27625): at com.android.internal。 os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036) 01-09 22:13:08.625: E/AndroidRuntime(27625): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803) 01 -09 22:13:08.625: E/AndroidRuntime(27625): at dalvik.system.NativeStart.main(Native Method) 01-09 22:13:08.625: E/AndroidRuntime(27625): Caused by: java.lang. NullPointerException 01-09 22:13:08.625:E/AndroidRuntime(27625): 在 com.example.test_06.MovieActivity.onCreate(MovieActivity.java:76) 01-09 22:13:08.625: E/AndroidRuntime(27625): 在 android.app.Activity.performCreate(Activity .java:5206) 01-09 22:13:08.625: E/AndroidRuntime(27625): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 01-09 22:13:08.625: E/AndroidRuntime(27625 ): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074) 01-09 22:13:08.625: E/AndroidRuntime(27625): ... 11 更多625: E/AndroidRuntime(27625): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074) 01-09 22:13:08.625: E/AndroidRuntime(27625): ... 11 更多625: E/AndroidRuntime(27625): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074) 01-09 22:13:08.625: E/AndroidRuntime(27625): ... 11 更多
我附加了另一个错误......我陷入了关于 3 小时 TT 的深入思考