Fenia Kechagia
Fenia Kechagia

Reputation: 83

can't instantiate class... no empty constructor

I want to show an alertdialog when i select a list item. The problem is the class instantiation .Logcat shows ...can't instantiate class .. no empty constructor. Thanks in advance

Part of the class where i build a BaseAdapter to show the list

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)

Upvotes: 2

Views: 627

Answers (3)

max59
max59

Reputation: 606

Try this:

list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

CustomDialogClass dialog = new CustomDialogClass(EditRouteIdiot.this);
  dialog.show();
 }
});

Upvotes: 0

Rakeeb Rajbhandari
Rakeeb Rajbhandari

Reputation: 5063

You should instantiate your dialog like this :

CustomDialogClass dialog = new CustomDialogClass(EditRouteIdiot.this);
dialog.show();

You would use intents to trigger activities most of the time.

Upvotes: 0

Duncan Hoggan
Duncan Hoggan

Reputation: 5100

Dialogs are not established in the same manner you do an activity. I suggest using a builder or the following.

CustomDialogClass yourDialog = new CustomDialogClass(EditRouteIdiot.this);
yourDialog.show();

Make sure to manage dismissing the instance of the CustomDialogClass too.

Upvotes: 1

Related Questions