我想在选择列表项时显示alertdialog。问题是类实例化.Logcat显示...不能实例化类..没有空构造函数。 提前致谢
我构建BaseAdapter以显示列表
的类的一部分protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
final ListView list = (ListView) findViewById(R.id.listViewDromoIdiot);
final class Adapter1 extends BaseAdapter{
public ArrayList<HashMap<String, String>> itinlist;
public Adapter1( ArrayList<HashMap<String, String>> itinlist) {
super();
this.itinlist = itinlist;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return itinlist.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return itinlist.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = getLayoutInflater();
convertView = (RelativeLayout) inflater
.inflate(R.layout.list_item51, parent,
false);
}
int pos = position +1;
String posi = String.valueOf(pos);
TextView number_n = (TextView) convertView.findViewById(R.id.number_n2);
number_n.setText(posi);
TextView startpoli2 = (TextView) convertView.findViewById(R.id.startpoli2);
startpoli2.setText(itinlist.get(position).get("startPoli1"));
TextView finalpoli2 = (TextView) convertView.findViewById(R.id.finalpoli2);
finalpoli2.setText(itinlist.get(position).get("finalPoli1"));
TextView eidosmetaf2 = (TextView) convertView.findViewById(R.id.eidosmetaf2);
eidosmetaf2.setText(itinlist.get(position).get("eidosmetaf1"));
TextView weight2 = (TextView) convertView.findViewById(R.id.weight2);
weight2.setText(itinlist.get(position).get("weight1"));
TextView depDate2 = (TextView) convertView.findViewById(R.id.depDate2);
depDate2.setText(itinlist.get(position).get("depDate1"));
TextView depTime1 = (TextView) convertView.findViewById(R.id.depTime1);
depTime1.setText(itinlist.get(position).get("depTime1"));
TextView specialservices1 = (TextView) convertView.findViewById(R.id.specialservices1);
specialservices1.setText(itinlist.get(position).get("specialservices1"));
TextView comments2 = (TextView) convertView.findViewById(R.id.comments1);
comments2.setText(itinlist.get(position).get("comments1"));
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent w = new Intent(EditRouteIdiot.this,CustomDialogClass.class);
EditRouteIdiot.this.startActivity(w);
}
});
return convertView;
}
}
Adapter1 adapter = new Adapter1(result);
list.setAdapter(adapter);
}
}
CustomDialogClass
public class CustomDialogClass extends Dialog implements
android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public ImageButton btn_add_route, btn_edit_route ,btn_del_route;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.alert_dialog);
btn_add_route = (ImageButton) findViewById(R.id.btn_add_route);
btn_edit_route = (ImageButton) findViewById(R.id.btn_edit_route);
btn_del_route = (ImageButton) findViewById(R.id.btn_del_route);
btn_add_route.setOnClickListener(this);
btn_edit_route.setOnClickListener(this);
btn_del_route.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_add_route:
Intent k =new Intent(c , Customer.class);
c.startActivity(k);
break;
case R.id.btn_edit_route:
Intent l =new Intent(c , EditRouteIdiot.class);
c.startActivity(l);
break;
case R.id.btn_del_route:
Intent u =new Intent(c , RemoveRouteIdiot.class);
c.startActivity(u);
break;
default:
break;
}
dismiss();
}
}
logcat的
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tranfer/com.example.tranfer.CustomDialogClass}: java.lang.InstantiationException: can't instantiate class com.example.tranfer.CustomDialogClass; no empty constructor
Caused by: java.lang.InstantiationException: can't instantiate class com.example.tranfer.CustomDialogClass; no empty constructor
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2036)
答案 0 :(得分:1)
对话框的建立方式与您开展活动的方式不同。 我建议使用建筑商或以下人员。
CustomDialogClass yourDialog = new CustomDialogClass(EditRouteIdiot.this);
yourDialog.show();
确保管理也解除了CustomDialogClass的实例。
答案 1 :(得分:0)
您应该像这样实例化对话框:
CustomDialogClass dialog = new CustomDialogClass(EditRouteIdiot.this);
dialog.show();
您可以使用意图在大多数时间触发活动。
答案 2 :(得分:0)
试试这个:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
CustomDialogClass dialog = new CustomDialogClass(EditRouteIdiot.this);
dialog.show();
}
});