本文介绍了尝试在Android Studio中的空对象引用上调用虚拟方法'android.content.Context.getApplicationInfo()'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有试图在应用程序中对空对象引用调用虚拟方法'java.util.List com.example.demo.Database.Database.getName()'错误,并且解决了使用 Database database = new Database(this); 代码的错误.

I had the Attempt to invoke virtual method 'java.util.List com.example.demo.Database.Database.getName()' on a null object reference error in my application and I solved that error using Database database = new Database(this); code.

现在有一个新错误

这是我的MainActivity.java代码

Here my MainActivity.java code

公共类MainActivity扩展了AppCompatActivity {

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
SearchAdapter adapter;
private Context context;
MaterialSearchBar materialSearchBar;
List<String> suggestList = new ArrayList<>();
Database database = new Database(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView)findViewById(R.id.recycler_search);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);

    materialSearchBar =(MaterialSearchBar)findViewById(R.id.search_bar);
    materialSearchBar.setHint("Search");
    materialSearchBar.setCardViewElevation(10);
    LoadSuggestList();
    materialSearchBar.addTextChangeListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            List<String> suggest = new ArrayList<>();
            for(String search:suggestList)
            {
                if(search.toLowerCase().contains(materialSearchBar.getText().toLowerCase()))
                    suggest.add(search);
            }
            materialSearchBar.setLastSuggestions(suggest);
        }

        @Override
        public void afterTextChanged(Editable s) {


        }
    });
    materialSearchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener(){
        @Override
        public void onSearchStateChanged(boolean enabled) {
            if(!enabled)
            {
                adapter = new SearchAdapter(getBaseContext(),database.getPharmacy());
                recyclerView.setAdapter(adapter);
            }
                //recyclerView.setAdapter(adapter);
        }

        @Override
        public void onSearchConfirmed(CharSequence text) {
            startSearch(text.toString());
        }

        @Override
        public void onButtonClicked(int buttonCode) {

        }
    });

    adapter = new SearchAdapter(this,database.getPharmacy());
    recyclerView.setAdapter(adapter);
}

private void startSearch(String text){
    adapter = new SearchAdapter(this, database.getPharmacyByCity(text));
    recyclerView.setAdapter(adapter);
}
private void LoadSuggestList() {
    suggestList = database.getName();
    materialSearchBar.setLastSuggestions(suggestList);
}

这是我的Database.java代码

Here my Database.java code

public class Database extends SQLiteAssetHelper {

private static final String DB_NAME = "pharmacyDemo.db";
private static final int DB_VER = 1;

public Database(Context context) {
    super(context, DB_NAME, null, DB_VER);
}

public List<Pharmacy> getPharmacy(){
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qd = new SQLiteQueryBuilder();

    String[] sqlSelect ={"ID","Name","City","Phone"};
    String tableName = "Pharmacy_List";

    qd.setTables(tableName);
    Cursor cursor = qd.query(db,sqlSelect,null, null ,null , null , null);
    List<Pharmacy> result = new ArrayList<>();
    if(cursor.moveToFirst())
    {
        do{
            Pharmacy pharmacy = new Pharmacy();
            pharmacy.setID(cursor.getInt(cursor.getColumnIndex("ID")));
            pharmacy.setName(cursor.getString(cursor.getColumnIndex("Name")));
            pharmacy.setCity(cursor.getString(cursor.getColumnIndex("City")));
            pharmacy.setPhone(cursor.getString(cursor.getColumnIndex("Phone")));
            result.add(pharmacy);
        }
        while(cursor.moveToNext());
    }
    return result;
}

public List<String> getName(){
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qd = new SQLiteQueryBuilder();

    String[] sqlSelect ={"Name"};
    String tableName = "Pharmacy_List";

    qd.setTables(tableName);
    Cursor cursor = qd.query(db,sqlSelect,null, null ,null , null , null);
    List<String> result = new ArrayList<>();
    if(cursor.moveToFirst())
    {
        do{
            result.add(cursor.getString(cursor.getColumnIndex("Name")));
        }
        while(cursor.moveToNext());
    }
    return result;
}

public List<Pharmacy> getPharmacyByCity(String city){
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qd = new SQLiteQueryBuilder();

    String[] sqlSelect ={"ID","Name","City","Phone"};
    String tableName = "Pharmacy_List";

    qd.setTables(tableName);
    Cursor cursor = qd.query(db,sqlSelect,"City = ?", new String[]{city},null , null , null);
    List<Pharmacy> result = new ArrayList<>();
    if(cursor.moveToFirst())
    {
        do{
            Pharmacy pharmacy = new Pharmacy();
            pharmacy.setID(cursor.getInt(cursor.getColumnIndex("ID")));
            pharmacy.setName(cursor.getString(cursor.getColumnIndex("Name")));
            pharmacy.setCity(cursor.getString(cursor.getColumnIndex("City")));
            pharmacy.setPhone(cursor.getString(cursor.getColumnIndex("Phone")));
            result.add(pharmacy);
        }
        while(cursor.moveToNext());
    }
    return result;
}

推荐答案

尝试使用 context = this; 这样的方式在Database.java文件中证明上下文"的正确性:

Try justify "context" in your Database.java file using context = this; like this:

private static final String DB_NAME = "pharmacyDemo.db";
private static final int DB_VER = 1;

public Database(Context context) {
super(context, DB_NAME, null, DB_VER);
context = this;
}

这篇关于尝试在Android Studio中的空对象引用上调用虚拟方法'android.content.Context.getApplicationInfo()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 22:00