我的应用崩溃了,我不明白为什么。问题似乎出现在json阅读器块中。它起作用,因为我修改了一些我无法记住的东西。请帮我检查错误。
public class Home extends ActionBarActivity implements OnTaskComplete {
LinearLayout wrapper = null;
Context context = this;
public Bitmap imageHandler;
@Override
public void callBackFunction(Bitmap image) {
imageHandler = image;
}
public class Post{
String id;
String title;
String description;
String release;
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getRelease() {
return release;
}
public void setTitle(String title) {
this.title = title;
}
public void setDescription(String description) {
this.description = description;
}
public void setRelease(String release) {
this.release = release;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//OUTER
RelativeLayout outer = (RelativeLayout)findViewById(R.id.outer);
//SCROLLER
Scroller scroller = new Scroller(this);
scroller.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));
//WRAPPER
wrapper = new LinearLayout(this);
wrapper.setOrientation(LinearLayout.VERTICAL);
outer.addView(scroller);
scroller.addView(wrapper);
String result = null;
ArrayList<Post> focusOn = new ArrayList<Post>();
try {
URL address = new URL("http://www.youth-stories.com/api/all.php");
URLDataReader reader = new URLDataReader(context);
result = reader.execute(address).get();
}catch (IOException e){
e.printStackTrace();
} catch(InterruptedException e){
e.printStackTrace();
} catch (ExecutionException e){
e.printStackTrace();
}
try {
JSONObject obj = new JSONObject(result);
String success = (String) obj.getString("success");
JSONArray records = obj.getJSONArray("records");
for(int i = 0; i < records.length(); i++) {
Post tmp = new Post();
tmp.setId(records.getJSONObject(i).getString("id"));
tmp.setTitle(records.getJSONObject(i).getString("title"));
tmp.setDescription(records.getJSONObject(i).getString("contents"));
tmp.setRelease(records.getJSONObject(i).getString("data_post"));
focusOn.add(tmp);
}
}catch (JSONException e){
e.printStackTrace();
}
//wrapper
LinearLayout container = wrapper;
ProgressDialog dialog = new ProgressDialog(context);
dialog.setMessage("loading contents, please wait..");
dialog.setCancelable(false);
dialog.show();
for(int i = 0; i < focusOn.size(); i++) {
//item
LinearLayout item = new LinearLayout(getApplicationContext());
String select = focusOn.get(i).getId();
item.setId(new Integer(select));
item.setClickable(true);
//setUp new activity
final Intent intent = new Intent(getApplicationContext(),HomeOnSelect.class);
Bundle bundle = new Bundle();
int id = item.getId();
String strid = new Integer(id).toString();
bundle.putString("id", strid);
bundle.putString("title", focusOn.get(i).getTitle());
bundle.putString("contents", focusOn.get(i).getDescription());
bundle.putString("release", focusOn.get(i).getRelease());
intent.putExtras(bundle);
item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(intent);
}
});
container.addView(item);
item.setOrientation(LinearLayout.HORIZONTAL);
item.setPadding(0, 40, 0, 40);
item.setGravity(Gravity.CENTER_VERTICAL);
item.setBackgroundResource(R.drawable.postlayout);
//image
ImageView asset = new ImageView(getApplicationContext());
URL address = null;
try {
address = new URL("http://www.youth-stories.com/public/admin/CH_FocusOn/images/" + focusOn.get(i).getId() + "_thumb2.jpg");
URLImageReader reader = new URLImageReader(this, this, asset, dialog, i, focusOn.size());
reader.execute(address);
} catch (MalformedURLException e) {
e.printStackTrace();
}
item.addView(asset);
LinearLayout.LayoutParams imgSettings = new LinearLayout.LayoutParams(300, 300);
asset.setLayoutParams(imgSettings);
asset.setPadding(50,0,0,0);
//inside
LinearLayout contents = new LinearLayout(getApplicationContext());
contents.setOrientation(LinearLayout.VERTICAL);
contents.setPadding(55, 0, 100, 0);
item.addView(contents);
//title
TextView title = new TextView(getApplicationContext());
title.setText(focusOn.get(i).getTitle());
title.setTextAppearance(this, R.style.title);
contents.addView(title);
//description
TextView description = new TextView(getApplicationContext());
description.setText(focusOn.get(i).getDescription());
description.setTextAppearance(this, R.style.description);
contents.addView(description);
//date
TextView date = new TextView(getApplicationContext());
date.setText(focusOn.get(i).getRelease());
date.setTextAppearance(this, R.style.description);
contents.addView(date);
//div
LinearLayout div = new LinearLayout(getApplicationContext());
div.setLayoutParams(new LinearLayout.LayoutParams(200, 40));
div.setBackgroundColor(Color.parseColor("#00000000"));
container.addView(div);
}
}
}
崩溃日志
03-05 12:23:03.276 25591-25591/youth_stories.com.youth_stories E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: youth_stories.com.youth_stories, PID: 25591
java.lang.RuntimeException: Unable to start activity ComponentInfo{youth_stories.com.youth_stories/youth_stories.com.youth_stories.Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
at org.json.JSONTokener.nextValue(JSONTokener.java:94)
at org.json.JSONObject.<init>(JSONObject.java:156)
at org.json.JSONObject.<init>(JSONObject.java:173)
at youth_stories.com.youth_stories.Home.onCreate(Home.java:129)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:0)
你的java文件的第129行是什么? 这一定是因为你试图为一个不存在的JSON对象。您可能想要查看对象丢失的位置。