0

这是我在这里的第一篇文章,所以如果您需要更多详细信息,请告诉我。

我正在尝试搜索 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.

我有以下四个主要类: HomeScreen:

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");           

            }
        }); 

    }}

}

数据屏幕:

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 while 更改为一段时间,但现在我在 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: 线程退出未捕获异常 (group=0x40a71930) 06 -11 20:23:15.699: E/AndroidRuntime(1268): 致命异常: main 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:

我已经使用 for 循环删除了按钮分配,并用单个按钮 Germany 替换并尝试了,但我仍然在 logCat 中遇到相同的错误,任何想法的家伙。

亲切的问候

4

3 回答 3

1

null链接到SQLiteOpenHelper构造函数时,请不要作为第一个参数传递。相反,请传递Context您在自己的构造函数中给出的。

于 2013-06-10T21:08:49.993 回答
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)

}
于 2013-06-10T21:12:29.270 回答
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);
于 2013-06-10T22:18:34.600 回答