我正在尝试从列表视图启动一项活动,但有一些错误
- 我能够填充 JSON 数据,但是当我单击列表中的行(用于启动活动)时出现错误
- 我在下面提到了 logcat 输出
有任何想法吗 ?
CopperChimney.java
public class CopperChimney extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.copperchimney_desc_screen);
Button btn1=(Button) findViewById(R.id.TopBackButton);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent emp1=new Intent(CopperChimney.this,MainActivity.class);
startActivity(emp1);
}
});
}
}
MainActivity.java
public class MainActivity extends Activity {
// url to make request
private static String url = "http://54.218.73.244:7000/";
List<Item> yourData = new ArrayList<Item>();
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Instantiating ProgressDialog with onCreate method
progressDialog=new ProgressDialog(MainActivity.this);
new ParsingAsync().execute();
}
private class ParsingAsync extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog=ProgressDialog.show(MainActivity.this, "", "Please Wait", true, false);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url);
try {
for (int i = 0; i < json.length(); i++) {
JSONObject c = json.getJSONObject(i);
// Storing each json item in variable
String NAME=c.getString("restaurantNAME");
yourData.add(new Item(NAME));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progressDialog.dismiss();
ListView yourListView = (ListView) findViewById(R.id.listViewID);
ListAdapter customAdapter = new ListAdapter(MainActivity.this, R.layout.itemlistrow, yourData);
yourListView.setAdapter(customAdapter);
yourListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
if(position == 0)
{
//code specific to first list item
Intent myIntent = new Intent(MainActivity.this,CopperChimney.class);
startActivity(myIntent);
}
}
});
}
}
}
显现
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CopperChimney"
android:label="@string/app_name" >
</activity>
</application>
日志猫
08-07 09:05:30.062: E/Buffer Error(408): Error converting result java.lang.NullPointerException
08-07 09:05:30.091: E/JSON Parser(408): Error parsing data org.json.JSONException: End of input at character 0 of
08-07 09:05:30.091: W/dalvikvm(408): threadid=9: thread exiting with uncaught exception (group=0x40015560)
08-07 09:05:30.111: E/AndroidRuntime(408): FATAL EXCEPTION: AsyncTask #1
08-07 09:05:30.111: E/AndroidRuntime(408): java.lang.RuntimeException: An error occured while executing doInBackground()
08-07 09:05:30.111: E/AndroidRuntime(408): at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-07 09:05:30.111: E/AndroidRuntime(408): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
08-07 09:05:30.111: E/AndroidRuntime(408): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
08-07 09:05:30.111: E/AndroidRuntime(408): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
08-07 09:05:30.111: E/AndroidRuntime(408): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-07 09:05:30.111: E/AndroidRuntime(408): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-07 09:05:30.111: E/AndroidRuntime(408): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-07 09:05:30.111: E/AndroidRuntime(408): at java.lang.Thread.run(Thread.java:1019)
08-07 09:05:30.111: E/AndroidRuntime(408): Caused by: java.lang.NullPointerException
08-07 09:05:30.111: E/AndroidRuntime(408): at com.MAIN_REF.findmybuffet.MainActivity$ParsingAsync.doInBackground(MainActivity.java:71)
08-07 09:05:30.111: E/AndroidRuntime(408): at com.MAIN_REF.findmybuffet.MainActivity$ParsingAsync.doInBackground(MainActivity.java:1)
08-07 09:05:30.111: E/AndroidRuntime(408): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-07 09:05:30.111: E/AndroidRuntime(408): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-07 09:05:30.111: E/AndroidRuntime(408): ... 4 more
08-07 09:05:32.721: E/WindowManager(408): Activity com.MAIN_REF.findmybuffet.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40521200 that was originally added here
08-07 09:05:32.721: E/WindowManager(408): android.view.WindowLeaked: Activity com.MAIN_REF.findmybuffet.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40521200 that was originally added here
08-07 09:05:32.721: E/WindowManager(408): at android.view.ViewRoot.<init>(ViewRoot.java:258)
08-07 09:05:32.721: E/WindowManager(408): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-07 09:05:32.721: E/WindowManager(408): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-07 09:05:32.721: E/WindowManager(408): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-07 09:05:32.721: E/WindowManager(408): at android.app.Dialog.show(Dialog.java:241)
08-07 09:05:32.721: E/WindowManager(408): at android.app.ProgressDialog.show(ProgressDialog.java:107)
08-07 09:05:32.721: E/WindowManager(408): at android.app.ProgressDialog.show(ProgressDialog.java:95)
08-07 09:05:32.721: E/WindowManager(408): at com.MAIN_REF.findmybuffet.MainActivity$ParsingAsync.onPreExecute(MainActivity.java:55)
08-07 09:05:32.721: E/WindowManager(408): at android.os.AsyncTask.execute(AsyncTask.java:391)
08-07 09:05:32.721: E/WindowManager(408): at com.MAIN_REF.findmybuffet.MainActivity.onCreate(MainActivity.java:42)
08-07 09:05:32.721: E/WindowManager(408): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-07 09:05:32.721: E/WindowManager(408): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-07 09:05:32.721: E/WindowManager(408): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-07 09:05:32.721: E/WindowManager(408): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-07 09:05:32.721: E/WindowManager(408): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-07 09:05:32.721: E/WindowManager(408): at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 09:05:32.721: E/WindowManager(408): at android.os.Looper.loop(Looper.java:123)
08-07 09:05:32.721: E/WindowManager(408): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-07 09:05:32.721: E/WindowManager(408): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 09:05:32.721: E/WindowManager(408): at java.lang.reflect.Method.invoke(Method.java:507)
08-07 09:05:32.721: E/WindowManager(408): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-07 09:05:32.721: E/WindowManager(408): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-07 09:05:32.721: E/WindowManager(408): at dalvik.system.NativeStart.main(Native Method)
谢谢,