萌新求助:关于button空指针问题

sinat_34315199 2016-03-16 06:46:10
代码大部分按照书上来的,开始用的imagebutton 后来改用button都报错。
报错行数大概在button.onclickliestener那一行,萌新不懂为什么会报错。QAQ明明xml里有这个控件的呀

代码如下:
主函数:
package com.example.call;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button bImageButton =(Button) findViewById(R.id.callphone);
SimpleAdapter adapter=new SimpleAdapter(this, getDate(), R.layout.activity_main,
new String[]{"name","number","img"},
new int[]{R.id.peoplename,R.id.peoplenumber,R.id.callphones});
setListAdapter(adapter);
bImageButton.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
Intent intent=new Intent(Intent.ACTION_CALL, Uri.parse("tel://1502203"));
startActivity(intent);
}});
}


public List<Map<String, Object>> getDate(){
List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
Map<String, Object> map=new HashMap<String, Object>();

map.put("name","sue" );
map.put("number", "13012259133");
map.put("img", R.drawable.imgcall_phone);
list.add(map);

map=new HashMap<String, Object>();
map.put("name","alice" );
map.put("number", "15022037651");
map.put("img", R.drawable.imgcall_phone);
list.add(map);

return list;

}
}


xml:
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:context="${packageName}.${activityClass}" >
<ImageButton
android:id="@+id/callphones"
android:layout_gravity="right"
android:layout_width="38dp"
android:layout_height="38dp"
android:src="@drawable/imgcall_phone"
android:background="#00000000"
android:layout_weight="1"

/>
<Button
android:id="@+id/callphone"
android:layout_gravity="right"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_weight="1"

/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="3" >


<TextView
android:id="@+id/peoplename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#563624"
android:textSize="22sp"
/>
<TextView
android:id="@+id/peoplenumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#c7a252"
android:textSize="13sp"
/>



</LinearLayout>



</LinearLayout>

...全文
262 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
摘星猿 2016-03-19
  • 打赏
  • 举报
回复
setContentView(R.layout.XXX) 没有调用
xingzhong128 2016-03-19
  • 打赏
  • 举报
回复
看来我确实写错了,楼主把onCreate里的代码换成下面的样子应该就可以了 SimpleAdapter adapter=new SimpleAdapter(this, getDate(), R.layout.activity_main, new String[]{"name","number","img"}, new int[]{R.id.peoplename,R.id.peoplenumber,R.id.callphones}){ @Override public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); Button bImageButton =(Button) view.findViewById(R.id.callphone); bImageButton.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { // TODO 自动生成的方法存根 Intent intent=new Intent(Intent.ACTION_CALL, Uri.parse("tel://1502203")); startActivity(intent); }}); return view; } };
o蝌蚪o 2016-03-18
  • 打赏
  • 举报
回复
你没有加载布局文件,findviewbuid的控件是从哪里来的?
走好每一步 2016-03-17
  • 打赏
  • 举报
回复
ListActivity 不需要setContentView(R.layout.xxxx)了。 而且楼主的activity_main是用来做listItem用的。 晕死,这么多人顶一楼。
JPF1024 2016-03-17
  • 打赏
  • 举报
回复
一楼说的对。。。。。。 在 super.onCreate(savedInstanceState);后面加一句:setContentView(R.layout.activity_main);试试。
moonFY 2016-03-17
  • 打赏
  • 举报
回复
同意一楼。。
早起的年轻人 2016-03-17
  • 打赏
  • 举报
回复
同意一楼。。。。。。。。
nuomici 2016-03-17
  • 打赏
  • 举报
回复
艾玛 setcontenview();让你吃了
唛头 2016-03-17
  • 打赏
  • 举报
回复
同意一楼。。新人常见错误之一
sparkhuu 2016-03-17
  • 打赏
  • 举报
回复
没有初始化当前activity的layout
。,,。, 2016-03-17
  • 打赏
  • 举报
回复
一楼已经给出了答案
Q_C 2016-03-16
  • 打赏
  • 举报
回复
你把这个包导入试一下 import android.view.View.OnClickListener
sinat_34315199 2016-03-16
  • 打赏
  • 举报
回复
还是不行呢 clean一次了也不行QAQ
xingzhong128 2016-03-16
  • 打赏
  • 举报
回复
你没有在super.onCreate()之后调用setContentView(R.layout.xxxx),也就是你上面那个xml文件

80,360

社区成员

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

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