Fragment跳转到Activity的问题

public class HomeFragment extends Fragment {

    private ImageView tiao1;
    
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.home_layout,null);
        initView();
        return mView;
    }
    private class ButtonListener implements View.OnClickListener {
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tiao1:
                    Intent intent= new Intent(getActivity(),Web_Activity.class);
                    startActivity(intent);
                    break;
            }
        }
    }
private void initView() {
    tiao1=(ImageView)mView.findViewById(R.id.tiao1);
    tiao1.getBackground().setAlpha(0);//注意调的这个R.id.tiao1控件中必须要有android:background="任意选择一个颜色"
    tiao1.setOnClickListener(new ButtonListener());
    }

}

 

如何从一个Activity跳转到Fragment中

1.首先这个Activity如果内部加载的是viewpager+fragment

在fragement 中的onresume中接收

 

已标记关键词 清除标记
``` public class Fragment_searchall extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_searchall, container, false); Button b =(Button)getActivity().findViewById(R.id.button); b.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ Intent intent = new Intent(getActivity(), Activity_settings.class); startActivity(intent); } }); return view; } } ``` Fragment布局文件就一个按钮,Activity已经在AndroidMainfest注册 这是控制台报错信息 ``` 04-15 02:12:38.158 9946-9946/com.level1.yukinoshita.swan0 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.level1.yukinoshita.swan0, PID: 9946 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.level1.yukinoshita.fragment.Fragment_searchall.onCreateView(Fragment_searchall.java:27) at android.app.Fragment.performCreateView(Fragment.java:2220) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148) at android.app.BackStackRecord.run(BackStackRecord.java:793) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 04-15 02:12:42.958 9946-9946/com.level1.yukinoshita.swan0 I/Process: Sending signal. PID: 9946 SIG: 9 ```
©️2020 CSDN 皮肤主题: 像素格子 设计师:CSDN官方博客 返回首页