andriod 异常处理 异常处理upload 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 TextView myTextView=(TextView)findViewById(R.id.myTextview);Button myButton=(Button)findViewById(R.id.myButton); myTextView.setText("我的第一个textView");myButton.setText("确定");我在程序中删除这两条代码后错误就没有了~ 报了空指针错误,难道是你的xml文件写得有问题。自己检查一下xml文件中Button和TextView的id有没有问题。应该是这儿的问题。 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.hollow.MainActivity$PlaceholderFragment" > <TextView android:id="@+id/myTextview" android:layout_width="wrap_content" android:layout_height="wrap_content" ></TextView> <Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> 那几行代码不要写在oncreate里,放到fragment的oncreateview里。 myTextView.setText("wo"); myButton.setText("queding");当我删除这两条代码的时候,程序可以运行 我试了下:报错了Cannot make a static reference to the non-static method findViewById(int) from the type Activity 我试了下:报错了Cannot make a static reference to the non-static method findViewById(int) from the type Activity 我一般这么做。只留Activity_main类和对应的xml,把fragment对应的类和xml删除。另外所有组件定义到activity的xml中。这个样子,获取组件肯定OK的。 http://bbs.csdn.net/topics/390764851 目测是setContentView();这句代码问题,不信不看看。 获取myTextView之后,判断是否为空加上try catch 查看具体异常原因 http://bbs.csdn.net/topics/390742254,因为两个控件是空指针,所以你删除对两个控件实现的方法的时候就不会报错了,,, android 如何从 3.55mm耳机接口 读取数据,功能 有些类似于Square 接口的区别 安卓与VC如何实现文件传输 Socket关闭后,接收函数仍然阻塞,怎么让它返回? 问一个android的程序劫持问题 怎样使androidscrollview动态请求数据 android消息推送问题,急求大侠赐教!!! 菜鸟求大神,上下滑动绘制,折线问题 Fragment 切换 怎么保证手机端拍好的照片都是正的 小白求救! 断断续续弄了几天myeclipse11的android插件,还是没搞定
Button myButton=(Button)findViewById(R.id.myButton);
myTextView.setText("我的第一个textView");
myButton.setText("确定");我在程序中删除这两条代码后错误就没有了~
自己检查一下xml文件中Button和TextView的id有没有问题。应该是这儿的问题。
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hollow.MainActivity$PlaceholderFragment" > <TextView
android:id="@+id/myTextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
myButton.setText("queding");
当我删除这两条代码的时候,程序可以运行
我试了下:报错了Cannot make a static reference to the non-static method findViewById(int) from the type Activity
这个样子,获取组件肯定OK的。
加上try catch 查看具体异常原因