我在尝试加载Activity Single Item活动时收到了意外错误。简而言之,此活动假设将JSON数据填充到应用程序中。
以下是收到的logcat消息:
08-06 21:49:11.711: E/AndroidRuntime(3006): FATAL EXCEPTION: main
08-06 21:49:11.711: E/AndroidRuntime(3006): Process: com.dooba.beta, PID: 3006
08-06 21:49:11.711: E/AndroidRuntime(3006): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dooba.beta/com.dooba.beta.EventSingleItemActivity}: java.lang.NullPointerException: uriString
08-06 21:49:11.711: E/AndroidRuntime(3006): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-06 21:49:11.711: E/AndroidRuntime(3006): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-06 21:49:11.711: E/AndroidRuntime(3006): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-06 21:49:11.711: E/AndroidRuntime(3006): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-06 21:49:11.711: E/AndroidRuntime(3006): at android.os.Handler.dispatchMessage(Handler.java:102)
08-06 21:49:11.711: E/AndroidRuntime(3006): at android.os.Looper.loop(Looper.java:136)
08-06 21:49:11.711: E/AndroidRuntime(3006): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-06 21:49:11.711: E/AndroidRuntime(3006): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 21:49:11.711: E/AndroidRuntime(3006): at java.lang.reflect.Method.invoke(Method.java:515)
08-06 21:49:11.711: E/AndroidRuntime(3006): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-06 21:49:11.711: E/AndroidRuntime(3006): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-06 21:49:11.711: E/AndroidRuntime(3006): at dalvik.system.NativeStart.main(Native Method)
08-06 21:49:11.711: E/AndroidRuntime(3006): Caused by: java.lang.NullPointerException: uriString
08-06 21:49:11.711: E/AndroidRuntime(3006): at android.net.Uri$StringUri.<init>(Uri.java:467)
08-06 21:49:11.711: E/AndroidRuntime(3006): at android.net.Uri$StringUri.<init>(Uri.java:457)
08-06 21:49:11.711: E/AndroidRuntime(3006): at android.net.Uri.parse(Uri.java:429)
08-06 21:49:11.711: E/AndroidRuntime(3006): at com.dooba.beta.EventSingleItemActivity.onCreate(EventSingleItemActivity.java:26)
08-06 21:49:11.711: E/AndroidRuntime(3006): at android.app.Activity.performCreate(Activity.java:5231)
08-06 21:49:11.711: E/AndroidRuntime(3006): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-06 21:49:11.711: E/AndroidRuntime(3006): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-06 21:49:11.711: E/AndroidRuntime(3006): ... 11 more
以下是事件单项活动代码
public class EventSingleItemActivity extends Activity {
// Declare Variables
String list_item_name;
String list_item_description;
String list_item_price;
String single_list_item_description;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events_single_item);
Intent i = getIntent();
Uri urisingle = Uri.parse(i.getStringExtra("single_list_item_bac"));
list_item_name = i.getStringExtra("list_item_name");
single_list_item_description = i.getStringExtra("single_list_item_description");
list_item_price = i.getStringExtra("list_item_price");
TextView txtname = (TextView) findViewById(R.id.name);
TextView txtsdescription = (TextView) findViewById(R.id.sdescription);
TextView txtprice = (TextView) findViewById(R.id.price);
// Set results to the TextViews
txtname.setText(list_item_name);
txtsdescription.setText(single_list_item_description);
txtprice.setText(list_item_price);
}
}
以下是主要活动页面
public class EventsActivity extends Activity{
private static final String URL_WEB_SERVICE = "http://dooba.ca/analytics/ed.php";
private GridView gv;
private ArrayList<Events_List> container;
private ArrayList<Events_List> items;
public Uri list_item_bac;
public Uri single_list_item_bac;
public String list_item_name;
public String list_item_description;
public String single_list_item_description;
public String list_item_price;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.events_list_layout);
gv = (GridView) findViewById(R.id.gridview);
container = new ArrayList<Events_List>();
//download JSON
listDownload();
GridView s = (GridView) findViewById(R.id.gridview);
s.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(EventsActivity.this,EventSingleItemActivity.class);
intent.putExtra("single_list_item_bac",container.get(position).getSingle_list_item_bac());
intent.putExtra("list_item_name", container.get(position).getList_item_title());
intent.putExtra("single_list_item_description", container.get(position).getSingle_list_item_description());
intent.putExtra("list_item_price",container.get(position).getList_item_price());
startActivity(intent); //start Activity
}
});
}
public void listDownload(){
RequestQueue volley = Volley.newRequestQueue(this);
JsonObjectRequest json = new JsonObjectRequest(Method.GET, URL_WEB_SERVICE, null, ResponseListener(), ErrorListener());
volley.add(json);
}
private Response.Listener<JSONObject> ResponseListener() {
return new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
//your JSON Array
JSONArray array = response.getJSONArray("list_item");
for(int i = 0; i < array.length(); i++){
container.add(convertirAnuncio(array.getJSONObject(i)));
}
} catch (JSONException e) {
e.printStackTrace();
}
gv.setAdapter(new AdapterEvents(getApplicationContext(),container));
}
};
};
private Response.ErrorListener ErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) { }
};
}
//object JSON
private final Events_List convertirAnuncio(JSONObject obj) throws JSONException {
long id = obj.getLong("id"); //id
String list_item_name = obj.getString("list_item_name");
String list_item_description = obj.getString("list_item_description");
String list_item_price = obj.getString("list_item_price");
String single_list_item_description = obj.getString("single_list_item_description");
Uri uri = Uri.parse(obj.getString("list_item_bac"));
return new Events_List(id,single_list_item_description,single_list_item_bac, list_item_name,list_item_description,list_item_price, uri);
}
}
以下是事件列表代码
public class Events_List {
public long id;
public String list_item_title;
public String list_item_price;
public Uri single_list_item_bac;
public String list_item_description;
public String single_list_item_description;
public Uri url;
public Events_List(long id, String single_list_item_description, Uri single_list_item_bac, String list_item_title, String list_item_description, String list_item_price, Uri url){
this.id = id;
this.list_item_title = list_item_title;
this.list_item_description = list_item_description;
this.single_list_item_description = single_list_item_description;
this.list_item_price = list_item_price;
this.single_list_item_bac = single_list_item_bac;
this.url = url;
}
public String getList_item_title()
{
return this.list_item_title;
}
public Uri getSingle_list_item_bac()
{
return this.single_list_item_bac;
}
public String getList_item_price()
{
return this.list_item_price;
}
public String getList_item_description()
{
return this.list_item_description;
}
public String getSingle_list_item_description()
{
return this.single_list_item_description;
}
}
非常感谢任何帮助。
答案 0 :(得分:1)
执行这样的空检查以避免异常:
Uri urisingle = Uri.parse(i.getStringExtra("single_list_item_bac"));
if(urisingle!=null){
doSomething()....
}
答案 1 :(得分:0)
当您有意发送数据时,您应该首先检查:
if(Uri != null)
// you can sent image as string
in.putExtra("image", imageUri);