调用活动时遇到以下错误 - 无法实例化活动。
简而言之,我有两个活动页面。第一个生成通过JSON数据填充的数组项列表,第二个(生成一个)提供有关上一个活动(活动一)上单击的项之一的更详细信息。
以下是我从第一个活动(数组列表)中点击某个项目后从logcat收到的错误消息。
08-04 22:13:40.667: E/AndroidRuntime(1243): at android.os.Looper.loop(Looper.java:136)
08-04 23:17:42.711: E/AndroidRuntime(2681): FATAL EXCEPTION: main
08-04 23:17:42.711: E/AndroidRuntime(2681): Process: com.dooba.beta, PID: 2681
08-04 23:17:42.711: E/AndroidRuntime(2681): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dooba.beta/com.dooba.beta.EventSingleItemActivity}: java.lang.InstantiationException: can't instantiate class com.dooba.beta.EventSingleItemActivity; no empty constructor
08-04 23:17:42.711: E/AndroidRuntime(2681): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
08-04 23:17:42.711: E/AndroidRuntime(2681): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-04 23:17:42.711: E/AndroidRuntime(2681): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-04 23:17:42.711: E/AndroidRuntime(2681): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-04 23:17:42.711: E/AndroidRuntime(2681): at android.os.Handler.dispatchMessage(Handler.java:102)
08-04 23:17:42.711: E/AndroidRuntime(2681): at android.os.Looper.loop(Looper.java:136)
08-04 23:17:42.711: E/AndroidRuntime(2681): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-04 23:17:42.711: E/AndroidRuntime(2681): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 23:17:42.711: E/AndroidRuntime(2681): at java.lang.reflect.Method.invoke(Method.java:515)
08-04 23:17:42.711: E/AndroidRuntime(2681): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-04 23:17:42.711: E/AndroidRuntime(2681): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-04 23:17:42.711: E/AndroidRuntime(2681): at dalvik.system.NativeStart.main(Native Method)
08-04 23:17:42.711: E/AndroidRuntime(2681): Caused by: java.lang.InstantiationException: can't instantiate class com.dooba.beta.EventSingleItemActivity; no empty constructor
08-04 23:17:42.711: E/AndroidRuntime(2681): at java.lang.Class.newInstanceImpl(Native Method)
08-04 23:17:42.711: E/AndroidRuntime(2681): at java.lang.Class.newInstance(Class.java:1208)
08-04 23:17:42.711: E/AndroidRuntime(2681): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
08-04 23:17:42.711: E/AndroidRuntime(2681): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
08-04 23:17:42.711: E/AndroidRuntime(2681): ... 11 more
以下是单项活动的代码
public class EventSingleItemActivity extends Activity {
public long id;
public Uri list_item_bac;
public String list_item_name;
public String list_item_description;
public String list_item_price;
public EventSingleItemActivity(long id, Uri list_item_bac, String list_item_name, String list_item_description,String list_item_price){
this.id = id;
this.list_item_bac = list_item_bac;
this.list_item_name = list_item_name;
this.list_item_description = list_item_description;
this.list_item_price = 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;
@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("id_item", id);//this is id of the item that you said it. for example id:5544323 from your JSON web service
intent.putExtra("position", position); //order position in listview 0-1-2-3...
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");
Uri uri = Uri.parse(obj.getString("list_item_bac"));
return new Events_List(id,list_item_name,list_item_description,list_item_price, uri);
}
}
提前感谢您的支持
更新
public class EventSingleItemActivity extends Activity {
// Declare Variables
String list_item_name;
String list_item_description;
String list_item_price;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events_single_item);
Intent i = getIntent();
list_item_name = i.getStringExtra("list_item_name");
list_item_description = i.getStringExtra("list_item_description");
list_item_price = i.getStringExtra("list_item_price");
TextView txtname = (TextView) findViewById(R.id.name);
TextView txtdescription = (TextView) findViewById(R.id.description);
TextView txtprice = (TextView) findViewById(R.id.price);
// Set results to the TextViews
txtname.setText(list_item_name);
txtdescription.setText(list_item_description);
txtprice.setText(list_item_price);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/price" />
<ImageView
android:id="@+id/image_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#000000"
android:padding="1dp" />
</RelativeLayout>
更新2
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 String list_item_name;
public String 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("list_item_name", list_item_name);
intent.putExtra("list_item_description", list_item_description);
intent.putExtra("list_item_price",list_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");
Uri uri = Uri.parse(obj.getString("list_item_bac"));
return new Events_List(id,list_item_name,list_item_description,list_item_price, uri);
}
}
答案 0 :(得分:3)
从EventSingleItemActivity
删除构造函数。而是通过调用onCreate()
来获取EventSingleItemActivity
getIntent()
Intent
的额外内容,以获取创建活动实例的{{1}}。