简体   繁体   中英

StartActivity in Custom ListView Adapter

I'm setting an onClick to my TextView called phone in my custom ListView . When the TextView is clicked, I would like for it to open up Android's Dial screen. I passed the Context of my Activity to my Custom Adapter Java Class but the app still keeps crashing every time I click on my TextView . The following code consists of the constructor as well as the last few methods in my Custom Adapter Java Class:

    public CustomAdapter(Context c,ArrayList<String>n,ArrayList<String>nums, ArrayList<String>e){
           context = c;
           phoneNumbers = nums;
           names = n;
           emails = e;
           inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

...

    public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    if (view == null){
        view = inflater.inflate(R.layout.contacts_custom_row,null);
        TextView phone = (TextView)view.findViewById(R.id.customRowContactNumber);

        phone.setOnClickListener(this);
    }
    return view;
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.customRowContactNumber:{
            Intent i = new Intent(Intent.ACTION_DIAL);
            context.startActivity(i);//I passed the context to my constructor
            break;
        }
    }

}

Please let me know what I'm doing wrong, thank you. Here is the log cat error:

09-21 17:48:49.451    2003-2003/com.markfeldman.mydoggydays E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.markf.dogdays, PID: 2003
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
        at android.app.ContextImpl.startActivity(ContextImpl.java:1238)
        at android.app.ContextImpl.startActivity(ContextImpl.java:1225)
        at android.content.ContextWrapper.startActivity(ContextWrapper.java:323)
        at com.markfeldman.mydoggydays.CustomAdapter.onClick(CustomAdapter.java:71)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        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:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

If you're sure here is where you want to start the activity you can just add

Intent i = new Intent(Intent.ACTION_DIAL);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

then start it as you were,

context.startActivity(i);

It looks like the context your passing isn't the context of an Activity which is why it's giving you the warning. If you're sure, you can do what @fixmycode suggested with changing the class declaration.

In your class declaration:

public CustomAdapter(Activity activity, ...){
    Activity myActivity = activity;
    ...
}

In your onClick method:

myActivity.startActivity(...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM