java.lang.NullPointerException和The method setOnClickListener(View.OnClickListener) in the type View

java.lang.NullPointerException具体意思是空指针异常,最常见的问题就是没有初始化。

1.字符串等数据类型没有初始化
2.类实例(对象)有用具体的类初始化
3.没有判断是否为空
我自己的错误是没有实例化对象·
package com.example.xpa;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class SQLLiteActivity extends Activity {

private MyDataBaseHelper helper;


@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.sqllite);

//实例化对象·
helper = new MyDataBaseHelper(this,“bookstore.db”, null, 1);
// 构造方法的4个参数
// 1.上下文对象
// 2.数据库的名字
// 3.允许我们在查询数据的时候返回一个自定义的Cursor,一般都是传入null
// 4.表示当前数据库的版本号,用于对数据库进行升级操作。构建出SQLiteOpenHelper实例后,再调用它的getReadableDatabase()orgetWritableDatabase()就能创建数据库了

	Button btn1=(Button) findViewById(R.id.btn1);
	btn1.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
		//对象调用·
			****helper.getWritableDatabase()**;**
			
		}
	});
}

}

The method setOnClickListener(View.OnClickListener) in the type View is not

这种问题在调用文件上引入import android.view.View.OnClickListener;
解决!

猜你喜欢

转载自blog.csdn.net/weixin_44174536/article/details/90168487