Fragment中按钮跳转到activity怎么实现?

逍遥魂1995 2015-11-19 01:24:21
我写的代码
package com.myapp.app;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.SearchManager.OnCancelListener;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

/**
* A simple {@link Fragment} subclass.
*
*/
public class HomeFragment extends Fragment {

private Button button;

public HomeFragment() {
}

@SuppressLint("InflateParams")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_home, null); // 先解析file.xml布局,得到一个view
button = (Button) rootView.findViewById(R.id.button_onclick);

// 给button_ok添加点击事件
OnClickListener listener0 = null;
listener0 = new OnClickListener() {
public void onClick(View v) {
System.out.println("xxxxxxxxxx");
Intent intent0 = new Intent(getActivity()
.getApplicationContext(), HomeXq_PageActivity.class);
startActivity(intent0);
}
};
button.setOnClickListener(listener0);
return inflater.inflate(R.layout.fragment_home, null);
}
}

结果报错

还请各位大神指教啊,很急,我是小白。
...全文
1519 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tracy林立颖 2016-04-15
  • 打赏
  • 举报
回复 2
改成return rootview啊亲
XZhongJie 2016-04-12
  • 打赏
  • 举报
回复 1
1.把return inflater.inflate(R.layout.fragment_home, null) 改成return rootView 2.重写onActivityCreated方法 把这个写进去/给button_ok添加点击事件 button.setOnClickListener(listener0); OnClickListener listener0 = null; listener0 = new OnClickListener() { public void onClick(View v) { System.out.println("xxxxxxxxxx"); Intent intent0 = new Intent(getActivity(), HomeXq_PageActivity.class); startActivity(intent0); } }; 不要乱用getApplicationContext();
大理段正淳 2016-04-11
  • 打赏
  • 举报
回复
楼主,解决了吗?怎么解决的?
生如夏花cch 2015-11-27
  • 打赏
  • 举报
回复
getActivity().startActivity(intent);
lujiaqian99 2015-11-27
  • 打赏
  • 举报
回复
我也是这样写的,报错,是空指针 显示的是button的监听为空
andymar007andymar 2015-11-20
  • 打赏
  • 举报
回复
改成return rootview
jklwan 2015-11-20
  • 打赏
  • 举报
回复
引用 2 楼 qq_24846389 的回复:
@jklwanwin 现在没有报错,但是点击按钮之后没有反应啊。
然后fragment中的代码return inflater.inflate(R.layout.fragment_home, null);改成return rootView
ant_di 2015-11-20
  • 打赏
  • 举报
回复
// 给button_ok添加点击事件 OnClickListener listener0 = null; listener0 = new OnClickListener() { public void onClick(View v) { System.out.println("xxxxxxxxxx"); Intent intent0 = new Intent(getActivity() .getApplicationContext(), HomeXq_PageActivity.class); startActivity(intent0); } }; button.setOnClickListener(listener0); 改成: button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { System.out.println("xxxxxxxxxx"); Intent intent0 = new Intent(getActivity().getApplicationContext(), HomeXq_PageActivity.class); startActivity(intent0); } });
逍遥魂1995 2015-11-19
  • 打赏
  • 举报
回复
@jklwanwin 现在没有报错,但是点击按钮之后没有反应啊。
jklwan 2015-11-19
  • 打赏
  • 举报
回复
fragment_home布局中,button的android:onClick="xqPage"这一句代码删掉。 然后fragment中的代码return inflater.inflate(R.layout.fragment_home, null);改成return rootView

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧