这是我在这里发表的第一篇文章,如果您需要更多详情,请告诉我。
我正在尝试搜索SQLite数据库并将数据加载到屏幕上并继续从我的logCat中收到以下错误:
06-10 20:44:22.838: E/Trace(875): error opening trace file: No such file or directory (2)
06-10 20:44:24.627: I/Choreographer(875): Skipped 30 frames! The application may be doing too much work on its main thread.
06-10 20:44:24.727: D/gralloc_goldfish(875): Emulator without GPU emulation detected.
06-10 20:44:26.956: D/AndroidRuntime(875): Shutting down VM
06-10 20:44:26.956: W/dalvikvm(875): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-10 20:44:27.006: E/AndroidRuntime(875): FATAL EXCEPTION: main
06-10 20:44:27.006: E/AndroidRuntime(875): java.lang.NullPointerException
06-10 20:44:27.006: E/AndroidRuntime(875): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
06-10 20:44:27.006: E/AndroidRuntime(875): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
06-10 20:44:27.006: E/AndroidRuntime(875): at com.Jamie.dosanddonts.DosAndDontsDbAdapter.findCountry(DosAndDontsDbAdapter.java:331)
06-10 20:44:27.006: E/AndroidRuntime(875): at com.Jamie.dosanddonts.Core.searchDatabase(Core.java:64)
06-10 20:44:27.006: E/AndroidRuntime(875): at com.Jamie.dosanddonts.HomeScreen$1.onClick(HomeScreen.java:39)
06-10 20:44:27.006: E/AndroidRuntime(875): at android.view.View.performClick(View.java:4204)
06-10 20:44:27.006: E/AndroidRuntime(875): at android.view.View$PerformClick.run(View.java:17355)
06-10 20:44:27.006: E/AndroidRuntime(875): at android.os.Handler.handleCallback(Handler.java:725)
06-10 20:44:27.006: E/AndroidRuntime(875): at android.os.Handler.dispatchMessage(Handler.java:92)
06-10 20:44:27.006: E/AndroidRuntime(875): at android.os.Looper.loop(Looper.java:137)
06-10 20:44:27.006: E/AndroidRuntime(875): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-10 20:44:27.006: E/AndroidRuntime(875): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 20:44:27.006: E/AndroidRuntime(875): at java.lang.reflect.Method.invoke(Method.java:511)
06-10 20:44:27.006: E/AndroidRuntime(875): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-10 20:44:27.006: E/AndroidRuntime(875): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-10 20:44:27.006: E/AndroidRuntime(875): at dalvik.system.NativeStart.main(Native Method)
06-10 20:44:32.236: I/Process(875): Sending signal. PID: 875 SIG: 9
06-10 20:44:34.827: D/gralloc_goldfish(892): Emulator without GPU emulation detected.
我有以下四个主要类: 主屏幕:
package com.Jamie.dosanddonts;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HomeScreen extends Activity {
Core myCore;
DosAndDontsDbAdapter myDb;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.myDb = new DosAndDontsDbAdapter(this);
this.myCore = new Core(myDb);
setContentView(R.layout.activity_home_screen);
Button france = null,germany = null,italy = null,spain = null,unitedKingdom = null;
final Button buttons[] = {france, germany, italy, spain, unitedKingdom};
int ids[] = {R.id.France,R.id.Germany,R.id.Italy,R.id.Spain,R.id.United_Kingdom};
for(int i=0;i<buttons.length;i++)
{
buttons[i]=(Button) findViewById(ids[i]);
buttons[i].setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent nextScreen = new Intent(getApplicationContext(), DataScreen.class);
startActivity(nextScreen);
//int temp = v.getId();
myCore.searchDatabase("Germany");
}
});
}}
}
DataScreen:
package com.Jamie.dosanddonts;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
public class DataScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_screen);
// Show the Up button in the action bar.
setupActionBar();
Button homeButton = (Button) this.findViewById(R.id.homeButton);
TextView textView = (TextView) this.findViewById(R.id.Country_name);
textView.setText("help");
homeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent nextScreen = new Intent(getApplicationContext(), HomeScreen.class);
startActivity(nextScreen);
}
});
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.data_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
DosAndDontsDbAdapter:
package com.Jamie.dosanddonts;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DosAndDontsDbAdapter extends SQLiteOpenHelper {
private static final String DATABASE_TABLE = "dosAndDonts";
private static final String COUNTRY = "Country name";
private static final String INDICATOR = "Indicator";
private static final String DESCRIPTION = "Description";
public DosAndDontsDbAdapter(Context context) {
super(null, DATABASE_TABLE, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("Create table " + DATABASE_TABLE + " (" + COUNTRY
+ " TEXT, " + INDICATOR + " TEXT, " + DESCRIPTION
+ " TEXT);");
db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COUNTRY, "France");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "Shake hands when you greet someone.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "France");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "Do say 'bonjour' or 'bonsoir'.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "France");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "Flowers should be given in odd numbers but not 13.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "France");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "Always smile");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "France");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "Always arrive on time for dinner.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "France");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "When eating Do not rest your elbows on the table.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "France");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Don't refer to someone by their first name unless you are a close friend.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "France");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Never offer cheap wine.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "France");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Do not begin eating until the hostess says 'bon appetit' ");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "France");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Do not cut salad with a knife and fork. Fold the lettuce on to your fork. ");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "United_Kingdom");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "Shake hands when greeting and leaving someone.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "United_Kingdom");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "When you have finished eating lay your knide and fork parallel across your plate.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "United_Kingdom");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "When in a pub pay for a round of drinks for everyone in your group.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "United_Kingdom");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "When eating The fork is held tines down so food is scooped on to the back of the fork.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "United_Kingdom");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "When eating, Table manners are Continental.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "United_Kingdom");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Never get in their personal space.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "United_Kingdom");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "It's inapropriate to ask personal questions.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "United_Kingdom");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "They'll become unconfortalbe if engage in prolonged eye contact.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "United_Kingdom");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "When eating don't rest your elbows on the table.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "United_Kingdom");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "DON'T make the V for victory sign with your palm facing yourself.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Italy");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "When greeting The usual handshake with direct eye contact.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Italy");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "Have calling card made.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Italy");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "If you bring wine, make sure it is a good vintage.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Italy");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "If you are invited to a meal, bring gift-wrapped such as wine or chocolates. ");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Italy");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "Table manners are Continental.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Italy");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Never give your business card in lieu of a calling card in a social situation.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Italy");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Do not give chrysanthemums as they are used at funerals.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Italy");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Do not keep your hands in your lap during the meal.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Italy");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Do not give red flowers as they indicate secrecy. ");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Italy");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Do not give yellow flowers as they indicate jealousy.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Germany");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "A quick, firm handshake is the traditional greeting.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Germany");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "Titles are very important and denote respect.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Germany");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "When entering a room, shake hands with everyone individually.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Germany");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "Yellow roses or tea roses are always well received. ");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Germany");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "If you bring wine, it should be imported.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Germany");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Do not give lilies or chrysanthemums as they are used at funerals. ");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Germany");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Never arrive early, aslways on time.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Germany");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Do not begin eating until the hostess starts.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Germany");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Do not rest your elbows on the table.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Germany");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Do not cut lettuce in a salad. Fold it using your knife and fork.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Spain");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "Many men use a two-handed shake.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Spain");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "Always bring a gift to someones house.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Spain");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "Always keep your hands visible when eating.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Spain");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "Use utensils to eat most food.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Spain");
cv.put(INDICATOR, "Do");
cv.put(DESCRIPTION, "If you have not finished eating, cross your knife and fork on your plate.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Spain");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Do not begin eating until the hostess starts. ");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Spain");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Do not get up until the guest of honour does. ");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Spain");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Never toast first always allow the host to.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Spain");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "When at dinner don't sit unless invited to do so.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
cv.put(COUNTRY, "Spain");
cv.put(INDICATOR, "Dont");
cv.put(DESCRIPTION, "Don't go bare cheasted anywhere other than the beach or poolside.");
db.insert(DATABASE_TABLE, COUNTRY, cv);
cv.clear();
db.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
Country findCountry(String country)
{
int index = 0;
SQLiteDatabase db=this.getReadableDatabase();
List <DosAndDonts> dosNDonts = new ArrayList <DosAndDonts>();
Cursor cursor = db.query(DATABASE_TABLE, new String[] { COUNTRY,
INDICATOR, DESCRIPTION }, COUNTRY + "=?",
new String[] { String.valueOf(country) }, null, null, null, null);
Country returnedCountry = new Country();
returnedCountry.setName(cursor.getString(0));
do
{
dosNDonts.add(new DosAndDonts(cursor.getString(index + 1),
cursor.getString(index + 2)));
index = index + 3;
//returnedCountry.setCountryDosAndDonts(countryDosAndDonts)
}while(cursor.moveToNext());
returnedCountry.setCountryDosAndDonts(dosNDonts);
//Cursor cur = db.rawQuery("FIND "+country+" as _id from "+DATABASE_TABLE,new String [] {});
return returnedCountry;
}
}
核心:
package com.Jamie.dosanddonts;
import java.util.List;
import android.widget.TextView;
public class Core {
private DosAndDontsDbAdapter myDosAndDontsDbAdapter;
private TextView temp;
private Country currentCountry;
public Core(DosAndDontsDbAdapter myDb) {
myDosAndDontsDbAdapter = myDb;
}
public Core() {
// TODO Auto-generated constructor stub
}
public void displayResult()
{
TextView country = (TextView) temp.findViewById(R.id.Country_name);
TextView do1 = (TextView) temp.findViewById(R.id.Do1);
TextView do2 = (TextView) temp.findViewById(R.id.Do2);
TextView do3 = (TextView) temp.findViewById(R.id.Do3);
TextView do4 = (TextView) temp.findViewById(R.id.Do4);
TextView do5 = (TextView) temp.findViewById(R.id.Do5);
TextView dont1 = (TextView) temp.findViewById(R.id.Dont1);
TextView dont2 = (TextView) temp.findViewById(R.id.Dont2);
TextView dont3 = (TextView) temp.findViewById(R.id.Dont3);
TextView dont4 = (TextView) temp.findViewById(R.id.Dont4);
TextView dont5 = (TextView) temp.findViewById(R.id.Dont5);
TextView dos[] = { do1, do2, do3, do4, do5 };
TextView donts[] = { dont1, dont2, dont3, dont4, dont5 };
int doCounter = 0;
int dontCounter = 0;
int index = 0;
List <DosAndDonts> dosNDonts = currentCountry.getCountryDosAndDonts();
country.setText(currentCountry.getName());
while (index < dosNDonts.size())
{
if (dosNDonts.get(index).getIndicator().equals("Do"))
{
dos[doCounter].setText(dosNDonts.get(index).getDescription());
doCounter++;
}
else
{
donts[dontCounter].setText(dosNDonts.`enter code here`get(index).getDescription());
dontCounter++;
}
index++;
}
}
public void searchDatabase(String searchedCountry) {
currentCountry = myDosAndDontsDbAdapter.findCountry(searchedCountry);
//TextView country = (TextView) temp.findViewById(R.id.Country_name);
//country.setText(searchedCountry);
this.displayResult();
}
}
enter code here
我已经通过了“德国”这个论点,试图进一步找出错误,但没有运气。因此,用户在homeScreen上选择一个国家,然后调用核心类来搜索数据库中的国家/地区信息并将其加载到dataScreen上,当我删除搜索数据库代码时,应用程序在屏幕之间移动很好但是搜索数据库代码应用程序加载主屏幕,但一旦我点击某个国家/地区就会显示logCat错误,任何帮助都会很棒。
嗨大家好
感谢您的回答,所以我做了一些更改,即不要在适配器类上将参数传递为null并在一段时间内更改do,但现在我在logCat中遇到以下错误:
06-11 20:23:06.520:D / gralloc_goldfish(1268):未检测到GPU仿真的仿真器。 06-11 20:23:10.620:I / Choreographer(1268):跳过50帧!应用程序可能在其主线程上做了太多工作。 06-11 20:23:15.629:D / AndroidRuntime(1268):关闭VM 06-11 20:23:15.629:W / dalvikvm(1268):threadid = 1:线程退出未捕获异常(组= 0x40a71930) 06-11 20:23:15.699:E / AndroidRuntime(1268):致命异常:主要 06-11 20:23:15.699:E / AndroidRuntime(1268):java.lang.IllegalStateException:递归调用getDatabase 06-11 20:23:15.699:E / AndroidRuntime(1268):在android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:204) 06-11 20:23:15.699:E / AndroidRuntime(1268):在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) 06-11 20:23:15.699:E / AndroidRuntime(1268):at com.Jamie.dosanddonts.DosAndDontsDbAdapter.onCreate(DosAndDontsDbAdapter.java:33) 06-11 20:23:15.699:E / AndroidRuntime(1268):在android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252) 06-11 20:23:15.699:E / AndroidRuntime(1268):在android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188) 06-11 20:23:15.699:E / AndroidRuntime(1268):at com.Jamie.dosanddonts.DosAndDontsDbAdapter.findCountry(DosAndDontsDbAdapter.java:331) 06-11 20:23:15.699:E / AndroidRuntime(1268):at com.Jamie.dosanddonts.Core.searchDatabase(Core.java:64) 06-11 20:23:15.699:E / AndroidRuntime(1268):at com.Jamie.dosanddonts.HomeScreen $ 1.onClick(HomeScreen.java:39) 06-11 20:23:15.699:E / AndroidRuntime(1268):在android.view.View.performClick(View.java:4204) 06-11 20:23:15.699:E / AndroidRuntime(1268):在android.view.View $ PerformClick.run(View.java:17355) 06-11 20:23:15.699:E / AndroidRuntime(1268):在android.os.Handler.handleCallback(Handler.java:725) 06-11 20:23:15.699:E / AndroidRuntime(1268):在android.os.Handler.dispatchMessage(Handler.java:92) 06-11 20:23:15.699:E / AndroidRuntime(1268):在android.os.Looper.loop(Looper.java:137) 06-11 20:23:15.699:E / AndroidRuntime(1268):在android.app.ActivityThread.main(ActivityThread.java:5041) 06-11 20:23:15.699:E / AndroidRuntime(1268):at java.lang.reflect.Method.invokeNative(Native Method) 06-11 20:23:15.699:E / AndroidRuntime(1268):at java.lang.reflect.Method.invoke(Method.java:511) 06-11 20:23:15.699:E / AndroidRuntime(1268):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 06-11 20:23:15.699:E / AndroidRuntime(1268):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-11 20:23:15.699:E / AndroidRuntime(1268):at dalvik.system.NativeStart.main(Native Method) 06-11 20:23:16.689:D / dalvikvm(1268):GC_CONCURRENT释放137K,10%免费2618K / 2900K,暂停9ms + 47ms,总计540ms
我已经使用for循环删除了按钮分配并替换为单个按钮德国并尝试了,我仍然在logCat中获得相同的错误,任何想法的人。
亲切的问候chapinch
答案 0 :(得分:1)
链接到null
构造函数时,请不要将SQLiteOpenHelper
作为第一个参数传递。相反,请传递您在自己的构造函数中给出的Context
。
答案 1 :(得分:1)
您不调用cursor.moveToFirst(),因此您首次尝试使用游标尝试访问行-1。另一种方法是将do-while循环更改为while循环:
while(cursor.moveToNext())
{
dosNDonts.add(new DosAndDonts(cursor.getString(index + 1),
cursor.getString(index + 2)));
index = index + 3;
//returnedCountry.setCountryDosAndDonts(countryDosAndDonts)
}
答案 2 :(得分:0)
CommonsWare是正确的,NullPointerException
是你调用super(null ...)而不是通过你给出的上下文的结果。
这就是原因:
// mContext is null
224 db = mContext.openOrCreateDatabase(mName, mEnableWriteAheadLogging ?
225 Context.MODE_ENABLE_WRITE_AHEAD_LOGGING : 0,
226 mFactory, mErrorHandler);