如何在扩展View
而不扩展AppCompatActivity
的类中使用startActivityForResult
方法?
public class ContentsElement extends View {
public ContentsElement(final Context context, final String text, final int requestCode, FlexboxLayout cl) {
super(context);
final TextView mu = new TextView(getContext());
mu.setText(text);
cl.addView(mu);
mu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast toast = Toast.makeText(getContext(),
mu.getText(), Toast.LENGTH_SHORT);
toast.show();
Context context = getContext();
Intent i = new Intent(context, MainActivity.class);
startActivityForResult(i, requestCode);
}
});
}
}
用修复startActivityForResult(i, requestCode);
((Activity)context).startActivityForResult(i, requestCode);