你难道没有用eclipse之类的工具呢?
应该是如下这样子的。
myButton.setOnClickListener(new OnClickListener(){ public void onClick(View v){
.......
});
应该是如下这样子的。
myButton.setOnClickListener(new OnClickListener(){ public void onClick(View v){
.......
});
解决方案 »
- 【求助】android初级问题
- ndk-r7无法识别C代码中的va_list类型
- listview想单独更新一行的某一个textview的文字,应该怎么实现?
- An SDK Target must be specified.的另类故障,气死我啦!
- 关于OOM
- android如何用代码打来word pdf 进行浏览啊!!!
- 如何自定义HorizontalScrollView 位置
- android 蓝牙4.0 BLE 为什么回调onServicesDiscovered方法返回的status参数一直是129?
- setContentView();报错!!!
- 在Android中导入httpcore-4.3.2.jar包,调用EntityUtils.consume(HttpEntity)提示找不到该方法
- 大家平常开发用到哪些第三方包
- 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很多知识点,楼主看视频什么的,别人的代码还是用第一种办法