加入fragment之后 setonclickListener 报错

Aeron_YES 2014-10-23 11:13:10
package com.aeron.cartoonapp.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

import com.aeron.cartoonapp.fragment.BaseFragment;
import com.aeron.cartoonapp.fragment.SlidingMenuFragment;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;


public class HomeActivity extends SlidingFragmentActivity implements OnClickListener {

ImageButton showMenuBtn;
private static SlidingMenu menu;
BaseFragment fragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frame_home_activity);


//装载视图
fragment = new BaseFragment();
getFragmentManager().beginTransaction().replace(R.id.homeactivity_frame, fragment).commit();


setBehindContentView(R.layout.slidingmenufragment_frame);
menu = new SlidingMenu(this);

Log.i("sadas", "fuck homeActivity");

showMenuBtn = (ImageButton)findViewById(R.id.showMenuBtn);

// showMenuBtn.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
//// Toast.makeText(HomeActivity.this, "dsads", Toast.LENGTH_SHORT).show();
// if(!menu.isMenuShowing())menu.showMenu();
// else menu.showContent();
// }
// });

// showMenuBtn.setOnClickListener(this);

inintSilidingMenu();
}


public void inintSilidingMenu() {
// TODO Auto-generated method stub
//menu属性

menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);

//menu视图
menu.setMenu(R.layout.slidingmenufragment_frame);
getFragmentManager().beginTransaction().replace(R.id.frame_slidingmenufragment, new SlidingMenuFragment()).commit();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

private final static int TIME_FINISH = 2000;
private long mExitTime = 0;
//再按一次推出程序实现
//实现原理:1.如果滑动菜单还在,则关闭滑动菜单
//2.在主界面,两次按后退键:间隔小于2S推出程序 否则继续提示
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

if(!menu.isMenuShowing()&&keyCode == KeyEvent.KEYCODE_BACK){
long currentTime = System.currentTimeMillis();
if(currentTime - mExitTime >TIME_FINISH){
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
mExitTime = System.currentTimeMillis();
}else{
finish();
System.exit(0);
}
return false;
}
else if(menu.isMenuShowing()&&keyCode == KeyEvent.KEYCODE_BACK)
{
menu.showContent();
return false;
}
return super.onKeyDown(keyCode, event);

}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}

}


package com.aeron.cartoonapp.fragment;

import com.aeron.cartoonapp.activity.R;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class BaseFragment extends Fragment {

//声明4个fragment
HomeFragment homefragment;
CategoryFragment categoryfragment;
HotFragment hotfragment;
AboutFragment aboutfragment;

//声明RadioGroup
RadioGroup rg;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//定义各个变量
homefragment = new HomeFragment();
categoryfragment = new CategoryFragment();
hotfragment = new HotFragment();
aboutfragment = new AboutFragment();

rg = (RadioGroup)getActivity().findViewById(R.id.tab_radiogroup_ID);

Log.i("sadas", "fuck onCreat");

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.i("sadas", "fuck onCreatView");
View view = inflater.inflate(R.layout.basefragment_frame, null);
return view;
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}

public class tabListener implements OnCheckedChangeListener{

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {



}

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i("fuck", " onViewCreated");
//onCreatView之后执行
//测试第一步:将HomeFragment加入视图

}

}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/frame_basefragment"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fff" >

<include layout="@layout/actionbar_home"/>

<include layout="@layout/tab_fragments_title"/>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/frame_four_containers_ID"
></FrameLayout>


</LinearLayout>



报错信息:


...全文
226 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Aeron_YES 2014-10-23
  • 打赏
  • 举报
回复
引用 3 楼 han1202012 的回复:
明显 showMenuBtn 是null 的; showMenuBtn 这个控件 定义在了 Fragment 中的吧, 在 Activity 中使用 findViewById 是获取不到 Fragment 中的组件的 ; 需要在 Fragment 对应的 onCreateView 方法, 使用 inflat 加载 rootView 布局后 使用 rootView,findViewById() 组件
谢谢
韩曙亮 2014-10-23
  • 打赏
  • 举报
回复
引用 5 楼 heaimnmn 的回复:
[quote=引用 3 楼 han1202012 的回复:] 明显 showMenuBtn 是null 的; showMenuBtn 这个控件 定义在了 Fragment 中的吧, 在 Activity 中使用 findViewById 是获取不到 Fragment 中的组件的 ; 需要在 Fragment 对应的 onCreateView 方法, 使用 inflat 加载 rootView 布局后 使用 rootView,findViewById() 组件
最近怎么没看到大神了,去研究什么东西去了,,,[/quote] 研究 TCP/IP 和 流媒体, 写博客去了, 最近发现 论坛 和 博客 兼顾不了 论坛玩一个月 博客玩一个月
哎,真难 2014-10-23
  • 打赏
  • 举报
回复
引用 3 楼 han1202012 的回复:
明显 showMenuBtn 是null 的; showMenuBtn 这个控件 定义在了 Fragment 中的吧, 在 Activity 中使用 findViewById 是获取不到 Fragment 中的组件的 ; 需要在 Fragment 对应的 onCreateView 方法, 使用 inflat 加载 rootView 布局后 使用 rootView,findViewById() 组件
最近怎么没看到大神了,去研究什么东西去了,,,
Aeron_YES 2014-10-23
  • 打赏
  • 举报
回复
40行不是绑定ID了吗
韩曙亮 2014-10-23
  • 打赏
  • 举报
回复
明显 showMenuBtn 是null 的; showMenuBtn 这个控件 定义在了 Fragment 中的吧, 在 Activity 中使用 findViewById 是获取不到 Fragment 中的组件的 ; 需要在 Fragment 对应的 onCreateView 方法, 使用 inflat 加载 rootView 布局后 使用 rootView,findViewById() 组件
哎,真难 2014-10-23
  • 打赏
  • 举报
回复
说你没有绑定showMenuBtn的ID
Aeron_YES 2014-10-23
  • 打赏
  • 举报
回复
53行注释不出问题 不注释就报错了

80,348

社区成员

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

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