button绑定监听器一直无法运行

kevin0304 2014-08-05 11:56:12
代码如下:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.os.Build;


public class MainActivity extends ActionBarActivity {

ProgressBar firstBar = null;
ProgressBar secondBar = null;
Button myButton = null;
int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

firstBar = (ProgressBar) findViewById(R.id.firstBar);
secondBar = (ProgressBar) findViewById(R.id.secondBar);
myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new ButtonListener()); //屏蔽这句就可以运行

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}

class ButtonListener implements OnClickListener {

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

if(i == 0)
{
firstBar.setVisibility(View.VISIBLE);
secondBar.setVisibility(View.VISIBLE);
}
else if(i<firstBar.getMax())
{
firstBar.setProgress(i);
firstBar.setSecondaryProgress(i+10);
}
else
{
firstBar.setVisibility(View.GONE);
secondBar.setVisibility(View.GONE);
}

i += 10;
}

}

看网上说的要导入
import android.view.View;
import android.view.View.OnClickListener;
这个包已经加入,可还是无法运行,新手,大家帮我能帮我分析分析么?
...全文
322 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
IT_木头 2014-08-10
  • 打赏
  • 举报
回复
引用 14 楼 kevin0304 的回复:
[quote=引用 13 楼 u013671350 的回复:] [quote=引用 12 楼 kevin0304 的回复:] [quote=引用 11 楼 kevin0304 的回复:] [quote=引用 9 楼 u013671350 的回复:] 楼主有
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
这段代码,项目中是不是下面有Fragment.xml 如果有的话,请参照下面http://blog.csdn.net/u013671350/article/details/37742673. 希望帮助到你
首先你可以按照我blog里的去掉fragment.xml,然后在加按钮应该没问题,其次你想在分布局fragment.xml加按钮有对应的语法,你直接写肯定不对的[/quote] 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; } }[/quote] 不建议新手用fragment.xml很多知识点,楼主看视频什么的,别人的代码还是用第一种办法
skgary 2014-08-10
  • 打赏
  • 举报
回复
我不是已经给你改好了嘛。
kevin0304 2014-08-10
  • 打赏
  • 举报
回复
引用 15 楼 skgary 的回复:
public 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)rootView.findViewById(R.id.cancel); mybutton.setText("我的第一个button"); return rootView; } } 楼主,你应该回炉重学一下java...
就是新手,不知道现在这个问题怎么解决
skgary 2014-08-10
  • 打赏
  • 举报
回复
改了第一行的类声明和findViewById那行。
skgary 2014-08-10
  • 打赏
  • 举报
回复
public 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)rootView.findViewById(R.id.cancel); mybutton.setText("我的第一个button"); return rootView; } } 楼主,你应该回炉重学一下java...
kevin0304 2014-08-10
  • 打赏
  • 举报
回复
引用 13 楼 u013671350 的回复:
[quote=引用 12 楼 kevin0304 的回复:] [quote=引用 11 楼 kevin0304 的回复:] [quote=引用 9 楼 u013671350 的回复:] 楼主有
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
这段代码,项目中是不是下面有Fragment.xml 如果有的话,请参照下面http://blog.csdn.net/u013671350/article/details/37742673. 希望帮助到你
首先你可以按照我blog里的去掉fragment.xml,然后在加按钮应该没问题,其次你想在分布局fragment.xml加按钮有对应的语法,你直接写肯定不对的[/quote] 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; } }
IT_木头 2014-08-10
  • 打赏
  • 举报
回复
引用 12 楼 kevin0304 的回复:
[quote=引用 11 楼 kevin0304 的回复:] [quote=引用 9 楼 u013671350 的回复:] 楼主有
if (savedInstanceState == null) {
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[/quote]
引用 11 楼 kevin0304 的回复:
[quote=引用 9 楼 u013671350 的回复:] 楼主有
if (savedInstanceState == null) {
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[/quote]
[/quote] 首先你可以按照我blog里的去掉fragment.xml,然后在加按钮应该没问题,其次你想在分布局fragment.xml加按钮有对应的语法,你直接写肯定不对的
kevin0304 2014-08-10
  • 打赏
  • 举报
回复
引用 11 楼 kevin0304 的回复:
[quote=引用 9 楼 u013671350 的回复:]
楼主有
if (savedInstanceState == null) {
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[/quote]

引用 11 楼 kevin0304 的回复:
[quote=引用 9 楼 u013671350 的回复:]
楼主有
if (savedInstanceState == null) {
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[/quote]
kevin0304 2014-08-10
  • 打赏
  • 举报
回复
引用 9 楼 u013671350 的回复:
楼主有
if (savedInstanceState == null) {
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
kevin0304 2014-08-10
  • 打赏
  • 举报
回复
引用 7 楼 u014635668 的回复:
你还是看看你的控件是否放在 activity_main里面的吧。
控件是在activity_main里面
sagittarius1988 2014-08-09
  • 打赏
  • 举报
回复
引用 4 楼 kevin0304 的回复:
[quote=引用 3 楼 sagittarius1988 的回复:] myButton 是null吧
这个在R.id中 找到myButton[/quote] 你调试试试,看是不是执行到这步时,myButton的值是什么
IT_木头 2014-08-09
  • 打赏
  • 举报
回复
楼主有
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
这段代码,项目中是不是下面有Fragment.xml 如果有的话,请参照下面http://blog.csdn.net/u013671350/article/details/37742673. 希望帮助到你
独木舟c 2014-08-09
  • 打赏
  • 举报
回复
你的绑定监听器不对吧,要么就直接在里面弄个类部类的形式,myButton.setOnClickListener(new ButtonListener(){ });要么就直接生成一个ButtonListener对象替换掉这句myButton.setOnClickListener(new ButtonListener())中的new ButtonListener();
android小于 2014-08-09
  • 打赏
  • 举报
回复
你还是看看你的控件是否放在 activity_main里面的吧。
杰哥哥啊 2014-08-09
  • 打赏
  • 举报
回复
是build不过吗? myButton.setOnClickListener(new ButtonListener()); 把new ButtonListener()改成(View.OnClickListener)(new ButtonListener()) 不过一般情况下是按1楼的写法写button响应时间的
kevin0304 2014-08-08
  • 打赏
  • 举报
回复
引用 3 楼 sagittarius1988 的回复:
myButton 是null吧
这个在R.id中 找到myButton
gao_chun 2014-08-06
  • 打赏
  • 举报
回复
1.建议一种方式,在button的xml文件中设置android:onclick="自己定义个方法,如:mBtnListener" 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(); }
sagittarius1988 2014-08-06
  • 打赏
  • 举报
回复
myButton 是null吧
skgary 2014-08-05
  • 打赏
  • 举报
回复
你难道没有用eclipse之类的工具呢? 应该是如下这样子的。。。。 myButton.setOnClickListener(new OnClickListener(){ public void onClick(View v){ ....... } );
一、实验名称:实验5 事件处理 二、实验日期: 三、实验目的: 基于监听的事件处理 基于回调的事件处理 四、实验用的仪器和材料:Windows+Eclipse+jdk+sdk+adt 五、实验的步骤和方法: 实验一:基于监听的事件处理机制 Activity.java package com.my; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class Week05Activity extends Activity implements OnClickListener{ EditText txt; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取button对象 Button btn1 = (Button)findViewById(R.id.button1); Button btn2 = (Button)findViewById(R.id.button2); Button btn3 = (Button)findViewById(R.id.button3); Button btn4 = (Button)findViewById(R.id.button4); Button btn5 = (Button)findViewById(R.id.button5); //获取edittext对象 txt = (EditText)findViewById(R.id.edittext); //定义一个单击事件的监听器(内部类) class MyClickListener implements OnClickListener{ public void onClick(View v) { EditText txt = (EditText)findViewById(R.id.edittext); txt.setText("内部类"); } } //定义一个单击事件的监听器(外部类) class ExtentEvent implements OnClickListener{ private Activity act; private EditText txt; public ExtentEvent(Activity act,EditText txt){ this.act=act; this.txt=txt; } public void onClick(View v) { txt.setText("外部类"); } } //为按钮绑定事件监听(内部类) btn1.setOnClickListener(new MyClickListener()); //为按钮绑定事件监听(外部类) btn2.setOnClickListener(new ExtentEvent(this,txt)); //直接使用Activity作为事件监听器 btn4.setOnClickListener(this); //匿名内部类 btn3.setOnClickListener(new OnClickListener(){ public void onClick(View v) { txt.setText("匿名内部类"); } }); } //直接绑定到标签 public void clickHandler(View source){ EditText et = (EditText)findViewById(R.id.edittext); et.setText("绑定到标签"); } //Activity本身作为事件监听起器类,实现的事件处理方法 public void onClick(View v) { txt.setText("Activity"); } } Main.xml

80,350

社区成员

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

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