本文介绍了数据库错误:"NullPointerException:尝试在空对象引用上调用虚拟方法getApplicationInfo()..."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建一个简单的多活动列表应用程序以与SQLite一起练习.我试图从数据库中获取字符串,以在启动时显示在 listview
中.我遇到了错误
I'm building a simple multi-activity list app to practice with SQLite. I was trying to get the strings from the database to display in the listview
when this started. I'm getting the error
代码在这里
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPreferences;
ArrayList<String> listItem;
ArrayAdapter adapter;
ListView lv = (ListView) findViewById(R.id.list_view);
DatabaseHelper db = new DatabaseHelper(this);
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listItem = new ArrayList<>();
viewData();
context = this;
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, textInput.class));
}
});
}
// cast from sql database to the listview
private void viewData() {
Cursor res = db.viewData();
if (res.getCount() == 0 ){
Toast.makeText(this,"No Data!",Toast.LENGTH_SHORT).show();
}
else{
while (res.moveToNext()){
listItem.add(res.getString(1));
}
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,listItem);
lv.setAdapter(adapter);
}
}
}
这是数据库助手:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "item.db";
public static final String TABLE_NAME = "item_table";
public static final String ID = "item_id";
public static final String NAME = "item_name";
public static final String PRICE = "item_price";
public static final String URL = "item_url";
public DatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" ( item_id INTEGER PRIMARY
KEY AUTOINCREMENT,item_name TEXT,item_price INTEGER,item_url TEXT) ");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String name, String price, String url){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME, name);
contentValues.put(PRICE, price);
contentValues.put(URL, url);
long result = db.insert(TABLE_NAME,null,contentValues);
if(result == -1)
return false;
else
return true;
}
public Cursor viewData(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
这是textInput活动
This is the textInput Activity
public class textInput extends AppCompatActivity {
SharedPreferences sharedPreferences;
// Editor editor;
// Context _context;
// int PRIVATE_MODE = 0;
// private static final String PREF_NAME = "Items";
// public static final String NAME_LIST = "name";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_input);
//DatabaseHelper db;
//return's to main activity on press
Button addButton = (Button) findViewById(R.id.addButton);
final EditText name = (EditText) findViewById(R.id.itemName);
final EditText url = (EditText) findViewById(R.id.itemURL);
final EditText price = (EditText) findViewById(R.id.itemPrice);
// final SharedPreferences sharedPreferences =
getApplicationContext().getSharedPreferences("items", 0);
final DatabaseHelper db = new DatabaseHelper(this);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Checks to make sure that all fields are filled out, if yes
then returns to MainActivity
if (name.length() != 0 && price.length() != 0 && url.length()
!=0){
boolean isInserted =
db.insertData(name.getText().toString(),
price.getText().toString(),
url.getText().toString());
if(isInserted == true)
Toast.makeText(textInput.this, "New Item Added",
Toast.LENGTH_SHORT).show();
else
Toast.makeText(textInput.this, "Failed to Add",
Toast.LENGTH_SHORT).show();
//The graphic and the return to MainActivity
Intent returnToMain = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(returnToMain);
}
else{
Toast.makeText(textInput.this, "Please fill out all
fields!", Toast.LENGTH_SHORT).show();
}
}
});
调试(错误?)日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.xmaswishlist, PID: 25470
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.xmaswishlist/com.example.xmaswishlist.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2841)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
at android.content.Context.obtainStyledAttributes(Context.java:655)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
at com.example.xmaswishlist.MainActivity.<init>(MainActivity.java:36)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1180)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
我尝试使用以下方法解决此问题:
I have tried to fix this using the following:
- Android Studio尝试在空对象引用上调用虚拟方法android.database.Cursor"问题
- 数据库错误:尝试在空对象引用上调用虚拟方法'Cursor .getScene()'
- 'android.content.pm.ApplicationInfo android.content.Context通知Android通知时在空对象引用上添加.getApplicationInfo()"
推荐答案
您尝试在设置 setContentView
中的内容之前获取视图,这会导致 exception
.在 onCreate
内实例化 ListView
和 DatabaseHelper
.
You try to get view before setting the content in setContentView
, which cause the exception
. Instantiate ListView
and DatabaseHelper
inside onCreate
.
ListView lv;
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
lv = (ListView) findViewById(R.id.list_view);
db = new DatabaseHelper(this);
...
}
这篇关于数据库错误:"NullPointerException:尝试在空对象引用上调用虚拟方法getApplicationInfo()..."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!