解决Attempt to invoke virtual method '...ListAdapter'on a null object reference

解决:Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference

在使用GirdView和ListView时经常会报这个错,仔细看错误信息发现是 NullPointerException,debug发现GirdView为null。

为什么会报这个错,原因是你使用的是this.findViewById()来取得GirdView,但事实是这样获取不到该对象。

所以正确的做法是:

使用布局填充器LayoutInflater的infalte方法。


View view= this.getLayoutInflater().inflate((GirdView 所在layout文件的资源id,例:R.layout.main), null);
GirdView gridView = view.findViewById(R.id.gridview );


ok ,这样就解决了。


发布了6 篇原创文章 · 获赞 9 · 访问量 3万+
展开阅读全文

android Adapter空指针问题 求大神看看 解决不了

06-17

是这样的。我是在做一个相当于一个手机卫士,里面有通讯录和黑名单两个功能。这个代码是我做的黑名单里的。![图片说明](https://img-ask.csdn.net/upload/201706/17/1497695379_112155.png) 想实现的功能是点黑名单按钮,可以出现这个黑名单的Activity。通讯录做了一个Listview,点通讯录按钮,可以导入系统通讯录,然后设置点击监听,长按item可以发短信,点击可以打电话。 现在的问题是,无论是通讯录还是黑名单,一点按钮就报错,报错的内容就是空指针。不知道怎么觉得。哭唧唧。 这个是代码。无论尝试什么方法,都报错空指针。 package com.jinwen.pengu.communication; import android.app.Activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class Stop extends Activity { private ListView iv_main; private BlackNumberAdapter adapter; private BlackNumberDao dao; private List<BlackNumber> data; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv_main = (ListView) findViewById(R.id.iv_main); adapter = new BlackNumberAdapter(); dao = new BlackNumberDao(this); data = dao.getAll(); iv_main.setAdapter(adapter); } public void add(View v){ } class BlackNumberAdapter extends BaseAdapter{ @Override public int getCount() { return data.size(); } @Override public Object getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView ==null){ convertView = View.inflate(Stop.this,android.R.layout.simple_list_item_1,null); } BlackNumber blackNumber = data.get(position); TextView textView = (TextView) convertView.findViewById(android.R.id.text1); textView.setText(blackNumber.getNumber()); return convertView; } } } 错误: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.jinwen.pengu.communication.Stop.onCreate(Stop.java:31) 问答

zookeeper链接不上2181端口~防火墙已关~

04-18

2016-04-18 22:03:05,165 INFO [main] zookeeper.ZooKeeper: Initiating client connection, connectString=slave1:2181,master:2181 sessionTimeout=90000 watcher=master:160200x0, quorum=slave1:2181,master:2181, baseZNode=/hbase 2016-04-18 22:03:05,175 INFO [main-SendThread(slave1:2181)] zookeeper.ClientCnxn: Opening socket connection to server slave1/192.168.1.11:2181. Will not attempt to authenticate using SASL (unknown error) 2016-04-18 22:03:05,178 WARN [main-SendThread(slave1:2181)] zookeeper.ClientCnxn: Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect java.net.ConnectException: 拒绝连接 at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:739) at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361) at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081) ....... 2016-04-18 22:03:22,107 WARN [main] zookeeper.RecoverableZooKeeper: Possibly transient ZooKeeper, quorum=slave1:2181,master:2181, exception=org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /hbase 2016-04-18 22:03:22,107 INFO [main-SendThread(master:2181)] zookeeper.ClientCnxn: Opening socket connection to server master/192.168.1.10:2181. Will not attempt to authenticate using SASL (unknown error) 2016-04-18 22:03:22,107 ERROR [main] zookeeper.RecoverableZooKeeper: ZooKeeper create failed after 4 attempts 2016-04-18 22:03:22,107 ERROR [main] master.HMasterCommandLine: Master exiting java.lang.RuntimeException: Failed construction of Master: class org.apache.hadoop.hbase.master.HMaster at org.apache.hadoop.hbase.master.HMaster.constructMaster(HMaster.java:2002) at org.apache.hadoop.hbase.master.HMasterCommandLine.startMaster(HMasterCommandLine.java:203) at org.apache.hadoop.hbase.master.HMasterCommandLine.run(HMasterCommandLine.java:139) at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:70) at org.apache.hadoop.hbase.util.ServerCommandLine.doMain(ServerCommandLine.java:126) at org.apache.hadoop.hbase.master.HMaster.main(HMaster.java:2016) Caused by: org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /hbase at org.apache.zookeeper.KeeperException.create(KeeperException.java:99) at org.apache.zookeeper.KeeperException.create(KeeperException.java:51) at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:783) at org.apache.hadoop.hbase.zookeeper.RecoverableZooKeeper.createNonSequential(RecoverableZooKeeper.java:575) at org.apache.hadoop.hbase.zookeeper.RecoverableZooKeeper.create(RecoverableZooKeeper.java:554) at org.apache.hadoop.hbase.zookeeper.ZKUtil.createWithParents(ZKUtil.java:1258) at org.apache.hadoop.hbase.zookeeper.ZKUtil.createWithParents(ZKUtil.java:1236) at org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher.createBaseZNodes(ZooKeeperWatcher.java:179) at org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher.<init>(ZooKeeperWatcher.java:172) at org.apache.hadoop.hbase.regionserver.HRegionServer.<init>(HRegionServer.java:531) at org.apache.hadoop.hbase.master.HMaster.<init>(HMaster.java:333) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.apache.hadoop.hbase.master.HMaster.constructMaster(HMaster.java:1997) ... 5 more 2016-04-18 22:03:22,107 WARN [main-SendThread(master:2181)] zookeeper.ClientCnxn: Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect java.net.ConnectException: 拒绝连接 at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:739) at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361) at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081) 问答

android adapter 提示空指针异常

03-24

``` public class ContactInfoParser extends Activity { ListView mListView; List<String> contactList = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mListView = (ListView) findViewById(R.id.list_contact); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, contactList); mListView.setAdapter(adapter); readContacts(); } private void readContacts() { Cursor cursor = null; try{ cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { String displayname = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String diplaynumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); contactList.add(displayname + "\n" + diplaynumber); } }catch (Exception e){ e.printStackTrace(); }finally { if (cursor != null) { cursor.close(); } } } } ``` 上面这段代码从其他activity跳转过来,执行到mListView.setAdapter(adapter);这句话的时候,提示一直报错空指针异常。 报错代码如下: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liujing.shanzhaisafe/com.liujing.shanzhaisage.fangdaomokuai.dialog.ContactInfoParser}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.liujing.shanzhaisage.fangdaomokuai.dialog.ContactInfoParser.onCreate(ContactInfoParser.java:24) 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览