startActivity(intent)在按钮片段上的片段中调用数字时显示错误

时间:2014-07-31 03:36:35

标签: android android-intent android-fragments

我需要点击ImageButton中自定义ListView中的Fragment来拨打电话号码。我已经给了

Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" +rowItem.getPhone()));
startActivity(intent);

但是在eclipse中出现了这样的错误:

  

方法startActivity(Intent)未定义新类型View.OnClickListener(){}

在我的应用程序中,这不是一个活动,因为它是一个片段,因为我正在使用导航抽屉。所以startactivity(intent)不会在片段中起作用。怎么能在碎片中工作呢?我也在Android Manifest中给出了适当的权限。所以我的应用程序没有权限问题。

以下是我的整个班级 CustomListViewAdapter 的内容,如下所示:

public class CustomListViewAdapter extends ArrayAdapter<RowItem>{

    Typeface tf;
    Context context;

    public CustomListViewAdapter(Context context, int resourceId,
            List<RowItem> items) {
        super(context, resourceId, items);
        this.context = context;
    }

    /*private view holder class*/
    private class ViewHolder {
        ImageView imageView;
        TextView name;
        TextView designation;
        TextView phonenumber;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        ImageButton call,sms;
        final RowItem rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_single, null);
            holder = new ViewHolder();
            tf=Typeface.createFromAsset(getContext().getAssets(),"MLKR0NTT.TTF");

            holder.name = (TextView) convertView.findViewById(R.id.text1);
            holder.designation = (TextView) convertView.findViewById(R.id.text);
            holder.phonenumber=(TextView)convertView.findViewById(R.id.text2);
            holder.imageView = (ImageView) convertView.findViewById(R.id.image);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.name.setText(rowItem.getName());
        holder.designation.setText(rowItem.getDesignation());
        holder.name.setTypeface(tf);
        holder.designation.setTypeface(tf);
        holder.phonenumber.setText(rowItem.getPhone());
        holder.imageView.setImageBitmap(rowItem.getImage());
        call=(ImageButton)convertView.findViewById(R.id.callbutton);
        call.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "Calling "+rowItem.getName(),Toast.LENGTH_LONG).show();
                // TODO Auto-generated method stub
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" +rowItem.getPhone()));

                // Here I need to start the activity for calling the number which is obtained using rowItem.getPhone(). 
                //But the method startActivity(intent) is undefined for the type new View.OnClickListener(){}
            }
        });
        return convertView;
    }
}

4 个答案:

答案 0 :(得分:2)

试试这个......

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent)

答案 1 :(得分:0)

您需要在上下文中调用它。如果这是Activity中的子类,请使用MyActivityName.this.startActivity()。如果它位于片段中,请使用getActivity().startActivity()

答案 2 :(得分:0)

问题是你试图从另一个类,onClick监听器调用它。您需要有一种方法来引用onClick类中的活动的封闭类。

首先为活动设置一个字段。

Activity activity;

然后在onCreate():

activity = this;

最后在你的侦听器的onClick方法中:

activity.startActivity(intent)

希望这有帮助,

乔治

答案 3 :(得分:0)

只需使用上下文变量..因为它是从您所在的activity / fragment传递的 打电话给你的适配器..

    context.startActivity(intent);