Android 不明故障读取json?
我的应用程序崩溃了,我不明白为什么。问题似乎出现在json读取器块中。自从我修改了一些我不记得的东西后,它就开始工作了。请帮我检索错误Android 不明故障读取json?,android,json,Android,Json,我的应用程序崩溃了,我不明白为什么。问题似乎出现在json读取器块中。自从我修改了一些我不记得的东西后,它就开始工作了。请帮我检索错误 public class Home extends ActionBarActivity implements OnTaskComplete { LinearLayout wrapper = null; Context context = this; public Bitmap imageHandler; @Override
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);
}
}
}
公共类Home扩展了ActionBarActivity实现了OnTaskComplete{
LinearLayout包装器=null;
上下文=这个;
公共位图图像处理器;
@凌驾
公共无效调用函数(位图图像){
imageHandler=图像;
}
公营职位{
字符串id;
字符串标题;
字符串描述;
释放管柱;
公共字符串getTitle(){
返回标题;
}
公共字符串getDescription(){
返回说明;
}
公共字符串getRelease(){
返回释放;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公共void setRelease(字符串释放){
这个。释放=释放;
}
公共无效集合id(字符串id){
this.id=id;
}
公共字符串getId(){
返回id;
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//外
RelativeLayout outer=(RelativeLayout)findViewById(R.id.outer);
//卷轴
Scroller Scroller=新的滚动条(此);
scroller.setLayoutParams(新的RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_父级,RelativeLayout.LayoutParams.FILL_父级);
//包装纸
包装器=新的线性布局(此);
包装器。设置方向(线性布局。垂直);
outer.addView(滚动条);
scroller.addView(包装器);
字符串结果=null;
ArrayList focusOn=新的ArrayList();
试一试{
URL地址=新URL(“http://www.youth-stories.com/api/all.php");
URLDataReader=新的URLDataReader(上下文);
结果=reader.execute(address.get();
}捕获(IOE异常){
e、 printStackTrace();
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}
试一试{
JSONObject obj=新JSONObject(结果);
String success=(String)obj.getString(“success”);
JSONArray记录=obj.getJSONArray(“记录”);
for(int i=0;i03-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)