点击列表视图,从Fragment打开新的Activity
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了点击列表视图,从Fragment打开新的Activity相关的知识,希望对你有一定的参考价值。
我想从listView上的Click方法中打开新的Activity。我为我的适配器实现单独的文件,并尝试从click listener方法打开新的Activity。但是当我尝试打开此活动时,它会显示我的错误。错误显示我在线上有一个错误,我称之为:
((Activity) getContext()).startActivityForResult(intentBlock, position);
致命异常:主进程:esticrm.pl.esticall,PID:28410 java.lang.IllegalStateException:片段CallsFragment {7b054bc}未附加到android.support.v4.app.Fragment.startActivityForResult(Fragment.java:1026)上的Activity在android.view.View.performClick的esticrm.pl.esticall.CallListAdapter $ 3.onClick(CallListAdapter.java:137)的android.support.v4.app.Fragment.startActivityForResult(Fragment.java:1017)(View.java:6199 )在Android.os.Handler.dispatchMessage(Handler.java:95)的android.view.View $ PerformClick.run(View.java:23647)android.os.Handler.handleCallback(Handler.java:751)位于com.android.internal.os的java.lang.reflect.Method.invoke(Native Method)的android.app.ActivityThread.main(ActivityThread.java:6682)上的.os.Looper.loop(Looper.java:154) .ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
我设置onClickListener的适配器文件
public class CallListAdapter extends ArrayAdapter<CallList> {
public CallListAdapter(Context context, ArrayList<CallList> calls) {
super(context, 0, calls);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
CallList callList = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.call_list, parent, false);
}
block.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intentBlock = new Intent(view.getContext(), BlockCallActivity.class);
intentBlock.putExtra("number", num);
intentBlock.putExtra("authToken", authToken);
((Activity) getContext()).startActivityForResult(intentBlock, position);
}
});
}
}
将活动作为参数发布并调用activity.startActivity()
使用以下代码
public class CallListAdapter extends ArrayAdapter<CallList> {
Activity activity;
public CallListAdapter(Context context, ArrayList<CallList> calls,Activity activity) {
super(context, 0, calls);
this.activity = activity;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
CallList callList = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.call_list, parent, false);
}
block.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intentBlock = new Intent(view.getContext(), BlockCallActivity.class);
intentBlock.putExtra("number", num);
intentBlock.putExtra("authToken", authToken);
activity.startActivityForResult(intentBlock, position);
}
});
}
}
以上是关于点击列表视图,从Fragment打开新的Activity的主要内容,如果未能解决你的问题,请参考以下文章