Android监听器的空指针问题,求大神指教!在线等!谢谢!

lusia1234 2016-03-11 11:26:06
package mei.activity02;

import android.os.Bundle;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;


public class Activity02 extends Activity {
private Button myButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity02);
myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new MyButtonListener());
}

class MyButtonListener implements OnClickListener{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Activity02.this, OtherActivity.class);
startActivity(intent);

}

}

一直提示红色部分空指针!布局文件之类的都检查了,有myButton,OtherActivity也已经注册!不知道为什么,求高手指点!
...全文
158 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lusia1234 2016-03-11
  • 打赏
  • 举报
回复
引用 5 楼 ganshenml 的回复:
日志级别是verbose。。。。。 改成debug及以上试试,代码我这里运行时OK的
一直提示我myButton.setOnClickListener(new MyButtonListener());这一行是空指针,无语啊!
ganshenml 2016-03-11
  • 打赏
  • 举报
回复
日志级别是verbose。。。。。 改成debug及以上试试,代码我这里运行时OK的
lusia1234 2016-03-11
  • 打赏
  • 举报
回复
引用 1 楼 ChococlateTan 的回复:
贴错误信息吖
已经贴错误代码啦,求指教!
lusia1234 2016-03-11
  • 打赏
  • 举报
回复
又重新写了一次代码,还是这个错误
lusia1234 2016-03-11
  • 打赏
  • 举报
回复
package work.activity08;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class Activity08 extends Activity {
private Button myButton = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity08);
myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new MyButtonListener());
}
class MyButtonListener implements OnClickListener{

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Activity08.this, OtherActivity.class);
startActivity(intent);

}

}
ChocolateTan 2016-03-11
  • 打赏
  • 举报
回复
贴错误信息吖
lusia1234 2016-03-11
  • 打赏
  • 举报
回复
引用 11 楼 xiaoxiao701111 的回复:
android:id="@+id/myButton"
哈哈,确实是这里出错了,谢谢谢谢!问题已解决!
xiaoxiao701111 2016-03-11
  • 打赏
  • 举报
回复
android:id="@+id/myButton"
lusia1234 2016-03-11
  • 打赏
  • 举报
回复
引用 9 楼 ganshenml 的回复:
android:name="@+id/myButton" 这是个什么鬼?ID?
对呀,id,没毛病啊
ganshenml 2016-03-11
  • 打赏
  • 举报
回复
android:name="@+id/myButton" 这是个什么鬼?ID?
lusia1234 2016-03-11
  • 打赏
  • 举报
回复
引用 7 楼 a511341250 的回复:
activity08.xml 文件也贴一下看看
引用 7 楼 a511341250 的回复:
activity08.xml 文件也贴一下看看
<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" tools:context=".Activity08" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> <Button android:name="@+id/myButton" android:layout_width="match_parent" android:layout_height="wrap_content"/> </RelativeLayout>
ESC尛蜜蜂 2016-03-11
  • 打赏
  • 举报
回复
activity08.xml 文件也贴一下看看

80,362

社区成员

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

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