Java 如何在扩展ArrayAdapter的自定义Listadapter中启动intent

Java 如何在扩展ArrayAdapter的自定义Listadapter中启动intent,java,android,Java,Android,我想开始一个新的活动,发送短信给一些号码。我已尝试此代码,但我的活动崩溃。我得到NullPointerException。亲切地指导我如何开始新的活动。我还在清单文件中添加了sms权限 public class ListAdapter extends ArrayAdapter<ParseUser> { private Context context; public ListAdapter(Context context, int resource, List<ParseU

我想开始一个新的活动,发送短信给一些号码。我已尝试此代码,但我的活动崩溃。我得到NullPointerException。亲切地指导我如何开始新的活动。我还在清单文件中添加了sms权限

  public class ListAdapter extends ArrayAdapter<ParseUser> {
private  Context context;
public ListAdapter(Context context, int resource, List<ParseUser> user1) {
    super(context, resource, user1);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.list_item_layout, null);
    }

    final ParseUser p = (ParseUser) getItem(position);

    if(p!=null){
        TextView username = (TextView)v.findViewById(R.id.username);
        TextView bloodType = (TextView)v.findViewById(R.id.bloodtype);
        //TextView longitude = (TextView)v.findViewById(R.id.longitude);
        //TextView latitude = (TextView)v.findViewById(R.id.latitude);
        //TextView mobile = (TextView)v.findViewById(R.id.mobile);
        Button msgButton = (Button)v.findViewById(R.id.message_me);
        final String number = p.getString("Mobile");

        if(username!=null){
            username.setText(p.getString("Name"));
        }
        if(bloodType!=null){
            bloodType.setText(p.getString("Bloodtype"));
        }

        msgButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent a = new Intent(getContext(),SmsActivity.class);
                a.putExtra("number",number);
                context.startActivity(a);

            }
        });
    }



    return v;
}
}

你能换这个吗

  public void onClick(View v) {
                Intent a = new Intent(this.ListAdapter ,SmsActivity.class);
改为

 Intent a = new Intent(context,SmsActivity.class);
问题就在这里:context.startActivitya;。您正在使用上下文字段,但从未分配它。您可以在构造函数中分配它:

public ListAdapter(Context context, int resource, List<ParseUser> user1) {
    super(context, resource, user1);
    this.context = context;
}

或者您可以使用getContext而不是context:getContext.startActivitya;。在这种情况下,您不需要完整的上下文字段。

您能提供您的日志吗?我认为问题出在这一行Intent a=new IntentgetContext,SmsActivity.class;getContext应该被context替换
public ListAdapter(Context context, int resource, List<ParseUser> user1) {
    super(context, resource, user1);
    this.context = context;
}