findviewbyid-FindViewbyID空指针问题

问题描述

FindViewbyID空指针问题

先看类:

public class MainActivity extends Activity {

    Canvas canvas;
    public int barometerRotationAngle=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        Resources res = getResources();
         canvas=new Canvas();

        final Altimeter altimeter=(Altimeter) this.findViewById(R.id.altimeter);
        final Button buttonup=(Button) this.findViewById(R.id.buttonup);

        buttonup.setOnClickListener(new OnClickListener()
        {

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

                barometerRotationAngle+=10;
                altimeter.setBarometerRotationAngle(barometerRotationAngle);

            }

        });

    }

        }

    }

程序在 altimeter.setBarometerRotationAngle(barometerRotationAngle);这里出现了空指针错误提示,但是alimeter类已经成功显示了图片,也就是说,他的onDraw()方法已被调用了。为什么在Onclick里面会变成了空指针?

解决方案

不知道你的图片怎么显示的,你先确认
final Altimeter altimeter=(Altimeter) this.findViewById(R.id.altimeter);是否找到了对象

解决方案二:

最简单就是自己跟踪一下,这也不复杂,看是这行报错了还是onDraw中报错了。

解决方案三:

应该是找到对象了吧,否则,altimeter里面的onDraw不会被调用到。我画图片的方法就在onDraw里面。启动程序后,图片会显示出来,但是一按按钮就报空指针错误

时间: 2024-08-28 11:59:54

findviewbyid-FindViewbyID空指针问题的相关文章

编码-findviewbyid 空指针的问题

问题描述 findviewbyid 空指针的问题 各位大侠,最近发现一个问题个人觉得非常诡异. 在oncreate 中 listView_test = (ListView) this.findViewById(R.id.listcardinfo); 为null. 但是 同一个页面中的一个button的 onClick方法确可以找到这个listview,并且赋值. 附上XML <?xml version="1.0" encoding="utf-8"?> &

Android Study之findViewById变迁之路

前言 今天我们一块来聊聊项目常用的findViewById,这个东西可以简单理解为:初始化控件,实例化控件,方便进行其他操作.一般来说,我们通常这么写: private void initView() {     TextView tvTest = (TextView) findViewById(R.id.id_test);  }   上面的例子很简单,只是初始化一个TextView,但是在实际项目中,每个Activity,Fragment或者Adapter中有n个控件,每个控件都需要我们实例化

Android中findViewById获取控件返回为空问题怎么解决_Android

在Android程序中,有时候需要加载非原来activity中xml布局中的控件,来使Android程序的界面更加丰富. 我本身是在使用ViewFlipper中遇到的问题. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); inflater=(LayoutInflater)getSystemService(LAYO

android-为什么自定义图像视图的 findviewbyId 返回 null

问题描述 为什么自定义图像视图的 findviewbyId 返回 null 通过设定zooming 和 boundaruy 限制我可以在屏幕上拖动和缩放 imageview .但是我想应用为多个图像.为此我在 main.xml中使用customview.但问题是findviewbyId 总返回null.问题出在哪里呢? NewzoomlimitActivity .java public class NewzoomlimitActivity extends Activity { /** Calle

如何在Fragment中使用findViewById呢?

如果你为Fragment在XML文件中创建了什么控件,但findViewById方法却只能被用在Activity类中,所以,有没有办法在Fragment中使用它呢? 当然可以,使用getView()方法就OK了,因为这个方法最终会返回当前fragment的根视图. Button btn = (Button) getView().findViewById(R.id.btn); 但是你应该知道要在此之前使用onCreateView来创建视图吧. @Nullable @Override public

android中findViewById方法参数问题

问题描述 android中findViewById方法参数问题 android中findViewById( R.id.myLayout); 这个方法参数的数据类型是什么,为什么省略了数据类型的声明,路径是什么,谢谢解答 解决方案 int 类型 public View findViewById(@IdRes int id) { return getDecorView().findViewById(id); } 各种资源都会在R文件中定义, 有R.id.xxx 引用在XML中定义了控件ID的 R.l

布局-为什么我这个findviewbyid返回是空的

问题描述 为什么我这个findviewbyid返回是空的 布局如下: fragment_menu.xml主要布局 android:id="@+id/vNavigation" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:background="

android中方法setContentView和方法findViewById的区别和联系是什么

问题描述 android中方法setContentView和方法findViewById的区别和联系是什么 android中方法setContentView和方法findViewById的区别和联系是什么 解决方案 前者是设置文本属性,后者是用来获取ID号. 解决方案二: setContentView()传入的 是一个layout文件,用来设置布局文件findViewByID用来绑定控件,传入的是一个id 解决方案三: setContentView是用来设置内容布局的.findViewById用

service-在服务器中findViewById方法

问题描述 在服务器中findViewById方法 应用中可以有一个服务器,通过两个Activity来启动,想要修改启动服务器时的图片,用findViewById方法,但是报错: The method findViewById(int) is undefined for the type Main 为什么会这样?请指教.谢谢 解决方案 针对你的问题 : 首先弄清findviewByid(int) 好Layout.inflate(int)的区别. 解决办法: (1) 首先通过Layout.infla

textview-android 没有继承activity的类调用 findViewById

问题描述 android 没有继承activity的类调用 findViewById AioVaccination是继承了Activity的类,但是我在下面调用的TextView queueNoTV = (TextView) mainActivity.findViewById(R.id.queueNo);MarqueeText mtChildName = (MarqueeText) mainActivity.findViewById(R.id.childName);TextView Myimia