你难道没有用eclipse之类的工具呢?
应该是如下这样子的。
myButton.setOnClickListener(new OnClickListener(){ public void onClick(View v){
.......
});
应该是如下这样子的。
myButton.setOnClickListener(new OnClickListener(){ public void onClick(View v){
.......
});
解决方案 »
- 安卓开发 listview的每行有四个checkbox和一个button,,通过单击button得知选了哪几个checkbox
- 求书《android游戏开发入门》Mario.Zechner 中文的电子书
- 求思路 - 通过notification进入某个页面后,如何返回到主界面
- android中如何实现图片的水平翻转
- 关于spinner的一点小问题,请指教!
- 入手Android开发群
- 真机调试 子线程里的值看不到?
- ViewPager如何设置为包裹内容的模式
- 新建一个工程,res\layout\ 下没有XML布局文件,求解答
- 遍历sdcard只能拿到一些小的音频文件
- 大家平常开发用到哪些第三方包
- android的刷新是个什么概念
2.在Activity中 写上自己定义的方法 ,如public void mBtnListener(View v){
switch (v.getId){
case R.id.button的id :
//处理点击事件
break;
}
}
3.看了下面这段代码,这可是个fragment,如果是fragment的话,必须find到这个button的id,并且setonlickListener(),button才有效
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
这个在R.id中 找到myButton
这个在R.id中 找到myButton你调试试试,看是不是执行到这步时,myButton的值是什么
myButton.setOnClickListener(new ButtonListener());
把new ButtonListener()改成(View.OnClickListener)(new ButtonListener())
不过一般情况下是按1楼的写法写button响应时间的
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}这段代码,项目中是不是下面有Fragment.xml
如果有的话,请参照下面http://blog.csdn.net/u013671350/article/details/37742673.
希望帮助到你
是有Fragment_main.xml,但是把控件初始化在public static class PlaceholderFragment extends Fragment { }这个里面会报错,提示findViewById报错,去掉static不会报错但是绑定的监听器依旧无法运行。方便的话qq交流方便。1258716539
是有Fragment_main.xml,但是把控件初始化在public static class PlaceholderFragment extends Fragment { }这个里面会报错,提示findViewById报错,去掉static不会报错但是绑定的监听器依旧无法运行。方便的话qq交流方便。1258716539
是有Fragment_main.xml,但是把控件初始化在public static class PlaceholderFragment extends Fragment { }这个里面会报错,提示findViewById报错,
去掉static不会报错但是绑定的监听器依旧无法运行。方便的话qq交流方便。1258716539
是有Fragment_main.xml,但是把控件初始化在public static class PlaceholderFragment extends Fragment { }这个里面会报错,提示findViewById报错,去掉static不会报错但是绑定的监听器依旧无法运行。方便的话qq交流方便。1258716539
是有Fragment_main.xml,但是把控件初始化在public static class PlaceholderFragment extends Fragment { }这个里面会报错,提示findViewById报错,
去掉static不会报错但是绑定的监听器依旧无法运行。方便的话qq交流方便。1258716539首先你可以按照我blog里的去掉fragment.xml,然后在加按钮应该没问题,其次你想在分布局fragment.xml加按钮有对应的语法,你直接写肯定不对的
首先你可以按照我blog里的去掉fragment.xml,然后在加按钮应该没问题,其次你想在分布局fragment.xml加按钮有对应的语法,你直接写肯定不对的fragment.xml里面是添加了button按钮的<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10px"
tools:context="com.example.hiworld.MainActivity$PlaceholderFragment"
>
<TextView
android:id="@+id/label"
android:text="test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/label"
android:background="#00aaaa"
/>
<Button
android:id="@+id/ok"
android:text="ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edittext"
android:layout_alignParentRight="true"
android:layout_marginLeft="20px"
/>
<Button
android:id="@+id/cancel"
android:text="cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edittext"
android:layout_toLeftOf="@+id/ok"
android:layout_alignLeft="@+id/edittext"
/>
</RelativeLayout>然后在Fragment 里面初始化button就报错
public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() {
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
Button mybutton = (Button)findViewById(R.id.cancel); findViewById报错Cannot make a static reference to the non-static method findViewById(int) from the type Activity
mybutton.setText("我的第一个button");
return rootView;
}
}
}@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);Button mybutton = (Button)rootView.findViewById(R.id.cancel);
mybutton.setText("我的第一个button");return rootView;
}
}楼主,你应该回炉重学一下java...
首先你可以按照我blog里的去掉fragment.xml,然后在加按钮应该没问题,其次你想在分布局fragment.xml加按钮有对应的语法,你直接写肯定不对的fragment.xml里面是添加了button按钮的<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10px"
tools:context="com.example.hiworld.MainActivity$PlaceholderFragment"
>
<TextView
android:id="@+id/label"
android:text="test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/label"
android:background="#00aaaa"
/>
<Button
android:id="@+id/ok"
android:text="ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edittext"
android:layout_alignParentRight="true"
android:layout_marginLeft="20px"
/>
<Button
android:id="@+id/cancel"
android:text="cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edittext"
android:layout_toLeftOf="@+id/ok"
android:layout_alignLeft="@+id/edittext"
/>
</RelativeLayout>然后在Fragment 里面初始化button就报错
public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() {
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
Button mybutton = (Button)findViewById(R.id.cancel); findViewById报错Cannot make a static reference to the non-static method findViewById(int) from the type Activity
mybutton.setText("我的第一个button");
return rootView;
}
}
不建议新手用fragment.xml很多知识点,楼主看视频什么的,别人的代码还是用第一种办法