当我运行我的代码时,它会工作几秒钟并自动关闭。它显示消息很遗憾您的应用已关闭。这是在我添加了带按钮的布局后开始发生的。
这是我的日志猫
07-21 04:41:14.320: E/AndroidRuntime(1764): FATAL EXCEPTION: main
07-21 04:41:14.320: E/AndroidRuntime(1764): Process: com.example.adaderana, PID: 1764
07-21 04:41:14.320: E/AndroidRuntime(1764): java.lang.NullPointerException
07-21 04:41:14.320: E/AndroidRuntime(1764): at com.example.adaderana.MainActivity$DownloadJSON.onPostExecute(MainActivity.java:140)
07-21 04:41:14.320: E/AndroidRuntime(1764): at com.example.adaderana.MainActivity$DownloadJSON.onPostExecute(MainActivity.java:1)
07-21 04:41:14.320: E/AndroidRuntime(1764): at android.os.AsyncTask.finish(AsyncTask.java:632)
07-21 04:41:14.320: E/AndroidRuntime(1764): at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-21 04:41:14.320: E/AndroidRuntime(1764): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
07-21 04:41:14.320: E/AndroidRuntime(1764): at android.os.Handler.dispatchMessage(Handler.java:102)
07-21 04:41:14.320: E/AndroidRuntime(1764): at android.os.Looper.loop(Looper.java:137)
07-21 04:41:14.320: E/AndroidRuntime(1764): at android.app.ActivityThread.main(ActivityThread.java:4998)
07-21 04:41:14.320: E/AndroidRuntime(1764): at java.lang.reflect.Method.invokeNative(Native Method)
07-21 04:41:14.320: E/AndroidRuntime(1764): at java.lang.reflect.Method.invoke(Method.java:515)
07-21 04:41:14.320: E/AndroidRuntime(1764): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
07-21 04:41:14.320: E/AndroidRuntime(1764): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
07-21 04:41:14.320: E/AndroidRuntime(1764): at dalvik.system.NativeStart.main(Native Method)
07-21 04:41:14.350: W/ActivityManager(371): Force finishing activity com.example.adaderana/.MainActivity
07-21 04:41:14.390: D/gralloc(51): Registering a buffer in the process that created it. This may cause memory ordering problems.
07-21 04:41:14.390: E/libEGL(51): called unimplemented OpenGL ES API
07-21 04:41:14.390: E/libEGL(51): called unimplemented OpenGL ES API
07-21 04:41:14.390: E/libEGL(51): called unimplemented OpenGL ES API
07-21 04:41:14.390: E/libEGL(51): called unimplemented OpenGL ES API
07-21 04:41:14.390: E/SurfaceFlinger(51): glCheckFramebufferStatusOES error 1717000432
07-21 04:41:14.390: E/SurfaceFlinger(51): got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot
07-21 04:41:14.400: E/libEGL(51): called unimplemented OpenGL ES API
07-21 04:41:14.400: E/libEGL(51): called unimplemented OpenGL ES API
07-21 04:41:14.400: W/WindowManager(371): Screenshot failure taking screenshot for (246x410) to layer 21015
07-21 04:41:14.710: I/Choreographer(371): Skipped 56 frames! The application may be doing too much work on its main thread.
07-21 04:41:14.990: W/ActivityManager(371): Activity pause timeout for ActivityRecord{b3edf2f8 u0 com.example.adaderana/.MainActivity t16 f}
07-21 04:41:15.010: I/Choreographer(371): Skipped 38 frames! The application may be doing too much work on its main thread.
07-21 04:41:15.370: I/Choreographer(371): Skipped 37 frames! The application may be doing too much work on its main thread.
07-21 04:41:15.420: I/Choreographer(371): Skipped 109 frames! The application may be doing too much work on its main thread.
07-21 04:41:15.430: I/Choreographer(534): Skipped 75 frames! The application may be doing too much work on its main thread.
07-21 04:41:18.560: I/Choreographer(371): Skipped 30 frames! The application may be doing too much work on its main thread.
07-21 04:41:18.780: I/Choreographer(371): Skipped 54 frames! The application may be doing too much work on its main thread.
07-21 04:41:19.080: I/Choreographer(371): Skipped 40 frames! The application may be doing too much work on its main thread.
07-21 04:41:19.320: I/Choreographer(371): Skipped 58 frames! The application may be doing too much work on its main thread.
07-21 04:41:19.520: I/Choreographer(371): Skipped 52 frames! The application may be doing too much work on its main thread.
07-21 04:41:29.710: W/ActivityManager(371): Activity destroy timeout for ActivityRecord{b3edf2f8 u0 com.example.adaderana/.MainActivity t16 f}
我的主要活动
package com.example.adaderana;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String TITLE = "title";
static String AUTHOR = "author";
static String THUMBNAIL = "thumbnail";
static String CONTENTS = "contents";
static String IMAGE = "image";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from list_main.xml
setContentView(R.layout.button);
Button b = (Button) findViewById(R.id.button2);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, Video.class);
startActivityForResult(myIntent, 0);
// new DownloadJSON().execute();
}
});
Button a = (Button) findViewById(R.id.button1);
a.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DownloadJSON().execute();
}
});
// Execute DownloadJSON AsyncTask
// new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Derana News");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://www.adaderana.mobi/apple/ipad.php?q=topcat&cat=36");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("TopNewsGivenCat");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("title", jsonobject.getString("title"));
map.put("author", jsonobject.getString("author"));
map.put("thumbnail", jsonobject.getString("thumbnail"));
map.put("contents", jsonobject.getString("contents"));
map.put("image", jsonobject.getString("image"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
答案 0 :(得分:3)
您将按钮布局设置为内容setContentView(R.layout.button);
您的评论说// Locate the listview in listview_main.xml
那么findViewById(R.id.listview);
将如何获得列表视图?
请修复您的xml并在onCreate()上使用listview = (ListView) findViewById(R.id.listview);
。