Android java.lang.NullPointerException

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
initAdData();
View view = inflater.inflate(R.layout.fragment_travel, container, false);

    dot0 = view.findViewById(R.id.v_dot0);
    dot1 = view.findViewById(R.id.v_dot1);
    dot2 = view.findViewById(R.id.v_dot2);
    dot3 = view.findViewById(R.id.v_dot3);
    dot4 = view.findViewById(R.id.v_dot4);
    tv_title = (TextView)view.findViewById(R.id.tv_title);
    adViewPager = (ViewPager)this.getActivity().findViewById(R.id.vp);

    return view;
}
    private void initAdData() {
    // 广告数据
    adList = getBannerAd();
    imageViews = new ArrayList<ImageView>();
    // 点
    dots = new ArrayList<View>();
    dotList = new ArrayList<View>();
    dots.add(dot0);
    dots.add(dot1);
    dots.add(dot2);
    dots.add(dot3);
    dots.add(dot4);
    ViewPager adViewPager = new ViewPager(getActivity());
    adViewPager.setAdapter(new MyAdapter());// 设置填充ViewPager页面的适配器
    // 设置一个监听器,当ViewPager中的页面改变时调用
    adViewPager.setOnPageChangeListener(new MyPageChangeListener());
    addDynamicView();
}
    private void addDynamicView() {
    // 动态添加图片和下面指示的圆点
    // 初始化图片资源
    for (int i = 0; i < adList.size(); i++) {
        ImageView imageView = new ImageView(getActivity());
        // 异步加载图片
        mImageLoader.displayImage(adList.get(i).getImgUrl(), imageView, options);
        imageView.setScaleType(ScaleType.CENTER_CROP);
        imageViews.add(imageView);
        dots.get(i).setVisibility(View.VISIBLE);//这行java.lang.NullPointerException
        dotList.add(dots.get(i));
    }
}

    dots.get(i).setVisibility(View.VISIBLE);//这行java.lang.NullPointerException

7个回答

initAdData()放在实例化后面调用。

qq_28026283
大白大白大白 希望采纳~
大约 3 年之前 回复
qq_28186229
崔峰鸣 非常感谢
大约 3 年之前 回复

dots在哪里定义的,是否初始化了。

Log一下吧, 看看dots集合里面有没有加上数据,应该是没取到数据,空指针,你就看 "."的前面,LOG的时候就LOG它就行了,

hanyi519
hanyi519 回复崔峰鸣: 还是上面我说的,就LOG找错就行,找“。”前面的东西,然后一点一点LOG
大约 3 年之前 回复
hanyi519
hanyi519 回复崔峰鸣: 就是空呗?那就看你dots加没加上数据,看看adList,这个是不是空,如果它是空,那你循环的时候也不起作用。知道在哪就顺着它找就行了,
大约 3 年之前 回复
qq_28186229
崔峰鸣 Log.v(dots.get(i).toString(), "=============================");您好,大神,打印这行空指针异常
大约 3 年之前 回复

[2011-09-14 13:44:16 - ddms]null
java.lang.NullPointerException
at com.android.ddmlib.Client.sendAndConsume(Client.java:572)
at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
......
答案就在这里:Android(java.lang.NullPointerException)
----------------------Hi,地球人,我是问答机器人小S,上面的内容就是我狂拽酷炫叼炸天的答案,除了赞同,你还有别的选择吗?

空指针异常
这种错误很常见
是数据传送时丢失,或未初始化其值,也可能是小细节,变量名拼写错误,所以其变量本身就为空,不存在有过数据;
这种在过程中丢失,或写错的导致在使用其值时,变量并没有值,报此异常

qq_28186229
崔峰鸣 您说的很对
大约 3 年之前 回复

一般看你日志
会给出空指针(就是变量为空出错的地方)
异常的文件,具体到行例如 StudentDao.java 34
类似的,就是StudentDao 文件,的第 34 行有个常量,被使用时,发现其为空值
往前检查一下,上一步,或拼写是否错误

日志显示Zygote: error closing descriptor
libcore.io.ErrnoException: close failed: EBADF (Bad file number)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
java.lang.NullPointerException Android develop
在用android studio开发app,现在用户注册登录系统连数据库,但是现在有问题,我想根据数据库里的用户表的不同用户类型,跳转不同activitiy,可是报错了,下面是代码rnLoginActivityrnpublic class LoginActivity extends Activity implements View.OnClickListener rnrn private TextView TVToRegister;rn private Button submit;rnrn private EditText username;rn private EditText code;rnrn private DBHelper demodb;rn private Cursor curosr;rn private Context mContext;rnrn SQLiteDatabase sqLiteDatabase;rnrn @Overridern protected void onCreate(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn setContentView(R.layout.activity_login);rnrn TVToRegister = (TextView) findViewById(R.id.tv_register);rn TVToRegister.setOnClickListener(new View.OnClickListener() rn @Overridern public void onClick(View v) rn Intent ToRegister = new Intent(LoginActivity.this,RegisterActivity.class);rn startActivity(ToRegister);rn rn );rnrn submit = (Button) findViewById(R.id.btn_submit);rn submit.setOnClickListener(this);rnrn username = (EditText) findViewById(R.id.edt_username);rn code = (EditText) findViewById(R.id.edt_code);rnrn demodb= new DBHelper(this);rn curosr=demodb.select();rnrn rnrnrn @Overridern public void onClick(View v) rn String UserName = username.getText().toString();rn String Code = code.getText().toString();rnrnrn if(username.getText().toString().equals("") || code.getText().toString().equals(""))rn rn Toast.makeText(getApplicationContext(),"user name and password can not be emprty!",Toast.LENGTH_SHORT).show();rn rn else rnrn boolean flag = demodb.login(UserName, Code);rn if (flag) rnrn [color=#FF0000] if(demodb.cate(sqLiteDatabase).getString(0).equals("保修")[/color])rn Intent reporter = new Intent(LoginActivity.this,reporter_new_request.class);rn startActivity(reporter);rn rn if(demodb.cate(sqLiteDatabase).getString(0).equals("维修"))rn Intent reparier1 = new Intent(LoginActivity.this,reparier.class);rn startActivity(reparier1);rnrn elsern Log.i("TAG", "Joo");rn Toast.makeText(LoginActivity.this, "JA", Toast.LENGTH_LONG).show();rn Intent login = new Intent(LoginActivity.this,reporter.class);rn startActivity(login);rn else rn Log.i("TAG", "Ei");rn Toast.makeText(LoginActivity.this, "NEJ", Toast.LENGTH_LONG).show();rn rn rnrn rnrnrnrnDBHelperrnpublic class DBHelper extends SQLiteOpenHelperrn private final static String DATABASE_NAME = "demo.db";rn private final static int DATABASE_VERSION = 1;rnrn private final static String TABLE_NAME = "request_list";rn public final static String ID = "ID";rn public final static String NAME = "Name";rn public final static String TITLE = "Title";rn public final static String DESCRIPTION = "Description";rn public final static String DEPARTMENT = "Department";rn public final static String CATEGORY = "Category";rnrn private final static String TABLE_NAME0 = "user_info";rn private final static String USERID = "ID";rn public final static String USERNAME0 = "Username";rn public final static String PASSWORD0 = "Password";rn public final static String NAME0 = "Name";rn public final static String EMAIL0 = "Email";rn public final static String MOBILE0 ="Mobile";rn public final static String WRITE = "Write";rn public final static String READ ="Read";rn public final static String VIEW ="View";rn public final static String TYPE = "Type";rnrnrn public DBHelper(Context context) rn super(context, DATABASE_NAME, null, DATABASE_VERSION);rn Log.e("Database operations", "Database created/opened");rnrn rnrnrnrn @Overridern public void onCreate(SQLiteDatabase db) rn String sql_reporter = "CREATE TABLE " + TABLE_NAME0 +rn " (" + USERID + " INTEGER primary key autoincrement, " +rn "" + NAME0 + " text, " +rn "" + EMAIL0 + " text, " +rn "" + MOBILE0 + " text, " +rn "" + TYPE + " text, "+rn "" + USERNAME0 + " text, "+rn "" + PASSWORD0 + " text, "+rn "" + WRITE + " text, "+rn "" + READ + " text, "+rn "" + VIEW + " text);";rnrn db.execSQL(sql_reporter);rn Log.e("Database operations", "Reporter Table created/opened");rnrn String sql = "CREATE TABLE " + TABLE_NAME +rn " (" + ID + " INTEGER primary key autoincrement, " +rn "" + NAME + " text, " +rn "" + TITLE + " text, " +rn "" + CATEGORY + " text, " +rn "" + DEPARTMENT + " text, " +rn "" + DESCRIPTION + " text);";rn db.execSQL(sql);rn Log.e("Database operations", "Require list Table created/opened");rnrnrn rnrn @Overridern public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) rn String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;rn db.execSQL(sql);rn onCreate(db);rn rnrn public Cursor select() rn SQLiteDatabase db = this.getReadableDatabase();rn Cursor cursor = dbrn .query(TABLE_NAME, null, null, null, null, null, null);rn return cursor;rn rnrn public boolean login(String user_name,String password)rn rnrn SQLiteDatabase sdb=this.getReadableDatabase();rn String sql="select * from user_info where Username=? and Password=?";rn Cursor cursor=sdb.rawQuery(sql, new String[]user_name, password);rn if(cursor.moveToFirst()==true)rn cursor.close();rn return true;rn rn return false;rnrn rnrn public Cursor getInformation(SQLiteDatabase db)rn rn Cursor cursor;rn String[] projection = ID,NAME,TITLE,DEPARTMENT,CATEGORY;rnrn cursor=db.query(TABLE_NAME,projection,null,null,null,null,null,null);rn return cursor;rn rnrn public Cursor cate(SQLiteDatabase db)rn Cursor cursor;rn String[] projection = TYPE;rn String selection = "Type=?";rnrn [color=#FF0000]cursor= db.query(TABLE_NAME0,projection,selection,null,null,null,null);[/color]rn return cursor;rn rn //增加操作rnrn public long UserInfo_insert (String reporter_name,String reporter_email, String reporter_mobile, String user_name,String user_password,String user_type)rn rn SQLiteDatabase db = this.getWritableDatabase();rnrn ContentValues cv = new ContentValues();rnrn cv.put(NAME0,reporter_name);rn cv.put(EMAIL0,reporter_email);rn cv.put(MOBILE0,reporter_mobile);rn cv.put(TYPE,user_type);rn cv.put(USERNAME0,user_name);rn cv.put(PASSWORD0,user_password);rnrnrn long row = db.insert(TABLE_NAME0, null, cv);rn return row;rn rnrn public long insert(String name,String title,String description,String department, String category)rn rn SQLiteDatabase db = this.getWritableDatabase();rn/* ContentValues */rn ContentValues cv = new ContentValues();rnrn cv.put(NAME,name);rn cv.put(TITLE, title);rnrnrn cv.put(DEPARTMENT,department);rn cv.put(CATEGORY,category);rn cv.put(DESCRIPTION,description);rnrn long row = db.insert(TABLE_NAME, null, cv);rn return row;rn rn //删除操作rn public void delete(int id)rn rn SQLiteDatabase db = this.getWritableDatabase();rn String where = ID + " = ?";rn String[] whereValue = Integer.toString(id) ;rn db.delete(TABLE_NAME, where, whereValue);rn rn //修改操作rn public void update(int id, String title,String name,String department, String category,String description)rn rn SQLiteDatabase db = this.getWritableDatabase();rn String where = ID + " = ?";rn String[] whereValue = Integer.toString(id) ;rnrn ContentValues cv = new ContentValues();rn cv.put(NAME,name);rn cv.put(TITLE, title);rn cv.put(DESCRIPTION,description);rnrn cv.put(DEPARTMENT,department);rn cv.put(CATEGORY,category);rn db.update(TABLE_NAME, cv, where, whereValue);rn rnrn报错说红色的那两句指向空指针错误。请问该如何解决
java.lang.NullPointerException
public List selectOne()rn List ls=null;rn DBConnection dbc=new DBConnection();rn Connection con=dbc.getDBConnection();rn Statement st=null;rn ResultSet rs=null;rn String sql="select * from newpro";rn if(con!=null)rn try rn st=con.createStatement();rn rs=st.executeQuery(sql);rn rn if(rs.next())rn dorn Pro pr=new Pro();rn pr.setPro_Id(rs.getInt(1));rn pr.setPro_Name(rs.getString("newpro_name"));rn pr.setPro_Photo(rs.getString("newpro_photo"));rn ls.add(pr);rn while(rs.next());rn rn catch (SQLException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn rn elsern System.out.print("error");rn rn rn return ls;rn
java.lang.NullPointerException ????????
==============连接数据库的Bean======================rnpackage bbsclass;rnimport java.sql.*;rnrnpublic class bbsconnrn String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";rn String sConnStr = "jdbc:odbc:bbs";rn Connection conn = null;rn ResultSet rs = null;rnrnpublic bbsconn() rntry rn Class.forName(sDBDriver); rnrncatch(java.lang.ClassNotFoundException e) rn System.err.println("bbsreg(): " + e.getMessage());rnrnrnrnpublic ResultSet executeQuery(String sql) rnrs = null;rntry rn conn = DriverManager.getConnection(sConnStr); rn Statement stmt = conn.createStatement();rn rs = stmt.executeQuery(sql);rn rncatch(SQLException ex) rn System.err.println("aq.executeQuery: " + ex.getMessage());rn System.err.println("aq.executeQuerystrSQL: " + sql);rnrnreturn rs;rnrn.....rnrn=========只是知执行一个查询的过程==========================rnimport bbsclass.bbsconn;rnimport java.io.PrintStream;rnimport java.sql.ResultSet;rnimport java.sql.SQLException;rnrnpublic class TestBbs implements java.io.Serializablernrn bbsconn bbsCon=null;rn ResultSet rs;rn rn public void TestBbs()rn rn bbsCon = new bbsconn();rn rs=null;rn rnrn public boolean search()rn rn ResultSet rs = null;rn tryrn rn rs = bbsCon.executeQuery("select * from user");rn if (rs.next())rn rn return true;rn rn elsern return false;rn rn catch (SQLException e)rn rn System.err.println(e.getMessage());rn System.out.println(e.getMessage());rn return false;rn rn rnrn public static void main(String[] args)rn rn TestBbs testBbs = new TestBbs();rn if (testBbs.search())rn rn System.out.print("有数据!");rn rn elsern rn System.out.print("没有数据!");rn rn rnrnrn执行TestBbs 的时候总是报:rnjava.lang.NullPointerExceptionrn at TestBbs.search(TestBbs.java:22)rn at TestBbs.main(TestBbs.java:40)rnException in thread "main" Normal Terminationrn我该怎么处理???? rnrnrnrnrnrnrnrn
java.lang.NullPointerException???
小弟我正在做个聊天程序,服务器已经连接上了,当运行登录界面后就出现如下错误,请各位大侠指点下,不胜感激……rnException in thread "AWT-EventQueue-0" java.lang.NullPointerExceptionrn at New.login_mouseClicked(New.java:125)rn at New$1.mouseClicked(New.java:60)rn at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)rn at java.awt.Component.processMouseEvent(Unknown Source)rn at javax.swing.JComponent.processMouseEvent(Unknown Source)rn at java.awt.Component.processEvent(Unknown Source)rn at java.awt.Container.processEvent(Unknown Source)rn at java.awt.Component.dispatchEventImpl(Unknown Source)rn at java.awt.Container.dispatchEventImpl(Unknown Source)rn at java.awt.Component.dispatchEvent(Unknown Source)rn at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)rn at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)rn at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)rn at java.awt.Container.dispatchEventImpl(Unknown Source)rn at java.awt.Window.dispatchEventImpl(Unknown Source)rn at java.awt.Component.dispatchEvent(Unknown Source)rn at java.awt.EventQueue.dispatchEvent(Unknown Source)rn at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)rn at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)rn at java.awt.EventDispatchThread.pumpEvents(Unknown Source)rn at java.awt.EventDispatchThread.pumpEvents(Unknown Source)rn at java.awt.EventDispatchThread.run(Unknown Source)rnrn
java.lang.NullPointerException?
报错“java.lang.NullPointerException“是怎么回事啊?rn
相关热词 c#检测非法字符 c#双屏截图 c#中怎么关闭线程 c# 显示服务器上的图片 api嵌入窗口 c# c# 控制网页 c# encrypt c#微信网页版登录 c# login 居中 c# 考试软件