0
您能否檢查我的代碼並告訴我我錯過了什麼?它迫使關閉設備。AsyncTask無法正常工作
SplashScreen.java
package com.example.citylist;
public class SplashScreen extends Activity {
JSONObject jsonobject;
JSONArray jsonarray;
ProgressDialog progressBar;
CityList list;
public TextView tv;
ArrayList<String> myList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Hide the status bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash_screen);
new DownloadJSON().execute();
}
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// progressBar = new ProgressDialog(MainActivity.this);
// progressBar.setTitle("City List");
// progressBar.setMessage("Loading city list...");
// progressBar.setIndeterminate(false);
// progressBar.show();
}
@Override
protected Void doInBackground(Void... params) {
jsonarray = JSONfunctions.getJSONfromURL("http://m.kentkart.com/new/services/?cmd=getCityList", "city");
list = new CityList();
try {
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject row;
row = jsonarray.getJSONObject(i);
CityInfo item = new CityInfo();
item.setId(row.optString(ServiceConstant.id));
item.setName(row.optString(ServiceConstant.name));
list.addItem(item);
}
}
catch (JSONException e) {
Log.e("json","hey");
}
return null;
}
@Override
protected void onPostExecute(Void args) {
myList = new ArrayList<String>();
for (int i=0; i<list.getLength(); i++)
{
myList.add(list.getItem(i).getName().toString());
}
Intent i = new Intent(getApplicationContext(), CityListView.class);
i.putStringArrayListExtra("name", myList);
startActivity(i);
finish();
}
}
}
Android清單
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"></permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light.NoTitleBar" >
<activity
android:name="com.example.citylist.SplashScreen"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.citylist.CityListView"></activity>
</application>
logcat的
CityListView
package com.example.citylist;
public class CityListView extends Activity {
ArrayList<String> myList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Hide the status bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.city_list);
ListView theListView = (ListView) findViewById(R.id.cityList);
Intent i = getIntent();
myList = i.getExtras().getStringArrayList("myList");
ListAdapter theAdapter = new ArrayAdapter<String>
(CityListView.this, android.R.layout.simple_list_item_1, myList);
theListView.setAdapter(theAdapter);
//liste elemanlarına tıklanıldığında oluşmasını istediğimiz durumu yazıyoruz
theListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//seçilen liste elemanını ekrana toast mesajı ile yazdırıyoruz.
String chosenCity = "You selected "
+parent.getItemAtPosition(position);
Toast.makeText(CityListView.this, chosenCity, Toast.LENGTH_SHORT).show();
}
});
}
}
下面是logcat的消息:
06-25 11:59:53.095: E/AndroidRuntime(7326): FATAL EXCEPTION: main
06-25 11:59:53.095: E/AndroidRuntime(7326): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.citylist/com.example.citylist.CityListView}: java.lang.NullPointerException
06-25 11:59:53.095: E/AndroidRuntime(7326): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
06-25 11:59:53.095: E/AndroidRuntime(7326): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
06-25 11:59:53.095: E/AndroidRuntime(7326): at android.app.ActivityThread.access$700(ActivityThread.java:140)
06-25 11:59:53.095: E/AndroidRuntime(7326): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
06-25 11:59:53.095: E/AndroidRuntime(7326): at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 11:59:53.095: E/AndroidRuntime(7326): at android.os.Looper.loop(Looper.java:137)
06-25 11:59:53.095: E/AndroidRuntime(7326): at android.app.ActivityThread.main(ActivityThread.java:4921)
06-25 11:59:53.095: E/AndroidRuntime(7326): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 11:59:53.095: E/AndroidRuntime(7326): at java.lang.reflect.Method.invoke(Method.java:511)
06-25 11:59:53.095: E/AndroidRuntime(7326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
06-25 11:59:53.095: E/AndroidRuntime(7326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
06-25 11:59:53.095: E/AndroidRuntime(7326): at dalvik.system.NativeStart.main(Native Method)
06-25 11:59:53.095: E/AndroidRuntime(7326): Caused by: java.lang.NullPointerException
06-25 11:59:53.095: E/AndroidRuntime(7326): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
06-25 11:59:53.095: E/AndroidRuntime(7326): at android.widget.ListView.setAdapter(ListView.java:466)
06-25 11:59:53.095: E/AndroidRuntime(7326): at com.example.citylist.CityListView.onCreate(CityListView.java:38)
06-25 11:59:53.095: E/AndroidRuntime(7326): at android.app.Activity.performCreate(Activity.java:5206)
06-25 11:59:53.095: E/AndroidRuntime(7326): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-25 11:59:53.095: E/AndroidRuntime(7326): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
06-25 11:59:53.095: E/AndroidRuntime(7326): ... 11 more
安置自己的logcat的錯誤? – Hariharan
可以請你發佈json響應嗎? –
'CityListView'必須有一些例外 – injecteer