Android DB(DatabaseHandler)抛出E​​XCEPTION

时间:2015-04-20 07:36:00

标签: java android database

我正在使用DatabaseHandler。 DatabaseHandler是我在游戏中使用的所有图像(标志)的resposibole。每次我的活动刷新并加载另一个图像(我的数据库有218个图像)。在某些时候,我得到致命的异常或:

04-19 23:00:41.177: W/dalvikvm(10808): threadid=1: thread exiting with uncaught exception (group=0x41674db8)
04-19 23:00:41.177: E/AndroidRuntime(10808): FATAL EXCEPTION: main

我的代码:
MainActivity (包括数据库添加(插入到db):

public class MainActivity extends Activity implements OnClickListener {

private static final String TAG = "Flagss Game" ;//יצירת טאג למשחק לשימוש בםונקציות כמו exception

Button start;
Button time;
Button reguler;
Button sound;
Button tens;
boolean flag=true;
MediaPlayer mpBack;
DatabaseHandler db = new DatabaseHandler(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //List<Flagss> arrAdd= new ArrayList<Flagsss>();
    //**
    tens = (Button)findViewById(R.id.topten);
    tens.setOnClickListener(TopOnClickListener);
    //**
    start = (Button)findViewById(R.id.start_btn);
    time = (Button)findViewById(R.id.time);
    reguler = (Button)findViewById(R.id.reguler);
    sound = (Button)findViewById(R.id.sound);
    sound.setBackgroundColor(Color.TRANSPARENT);
    sound.setOnClickListener(SoundOnClickListener);
    mpBack = MediaPlayer.create(this, R.raw.background_sound);
    mpBack.start();
    start.setOnClickListener(this);
    db = new DatabaseHandler(this);

    creat_rows();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

//**
public OnClickListener TopOnClickListener = new OnClickListener(
        ) {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(MainActivity.this,TopTen.class);
        startActivity(intent);
    }
};
//**

public OnClickListener SoundOnClickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        mpBack.stop();
        if(flag)
        {
            sound.setBackgroundResource(R.drawable.speaker_off);
            flag = false;
        }
        else
        {
            mpBack.start();
            sound.setBackgroundResource(R.drawable.speaker_on);
        }
    }
};

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    start.setVisibility(start.INVISIBLE);
    tens.setVisibility(tens.INVISIBLE);
    time.setVisibility(time.VISIBLE);
    reguler.setVisibility(reguler.VISIBLE);

    //Intent s = new Intent(this,ClassicMode.class);
    //startActivity(s);

    reguler.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cm = new Intent(MainActivity.this,ClassicMode.class);
            //Intent c = new Intent(MainActivity.class ,ClassicMode.class);
            startActivity(cm); 
            mpBack.stop();
        }
    });

    time.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent tm = new Intent(MainActivity.this,TimeAttack.class);
            startActivity(tm);
            mpBack.stop();
            Log.d(TAG, "inside time");
        }
    });
}


public void creat_rows()
{
    Log.d(TAG, "Inserting...");
    //image     id   name 
    db.addFlag(new Flags("afghanistan",0,"Afghanistan"));
    db.addFlag(new Flags("albania",1,"albania"));
    db.addFlag(new Flags("algeria",2,"Algeria" ));
    db.addFlag(new Flags("american_samoa", 3,"American Samoa" ));
    db.addFlag(new Flags("andorra",  4,"Andorra" ));
    db.addFlag(new Flags("angola",5,"Angola"));
    db.addFlag(new Flags("antigua_and_barbuda", 6,"Antigua and Barbuda" ));
    db.addFlag(new Flags("argentina", 7,"Argentina" ));
    db.addFlag(new Flags("arrmenia", 8,"Arrmenia"));
    db.addFlag(new Flags("australia", 9,"Australia"));
    db.addFlag(new Flags( "austria",10,"Austria"));
    db.addFlag(new Flags("azerbaijan", 11,"Azerbaijan"));
    db.addFlag(new Flags("bahamas",12,"Bahamas" ));
    db.addFlag(new Flags("bahrain",13,"Bahrain"));
    db.addFlag(new Flags("bangladesh", 14,"Bangladesh"));
    db.addFlag(new Flags("barbados",15,"Barbados"));
    db.addFlag(new Flags("belarus", 16,"Belarus"));
    db.addFlag(new Flags("belgium",17,"Belgium"));
    db.addFlag(new Flags("belize",18,"Belize"));
    db.addFlag(new Flags("benin",19,"Benin"));
    db.addFlag(new Flags("bermuda",20,"Bermuda"));
    db.addFlag(new Flags("bhutan", 21,"Bhutan" ));
    db.addFlag(new Flags("bolivia",22,"Bolivia" ));
    db.addFlag(new Flags("bosnia", 23,"Bosnia"));
    db.addFlag(new Flags("botswana",  24,"Botswana" ));
    db.addFlag(new Flags("brazil", 25,"Brazil"));
    db.addFlag(new Flags("brunei",26,"Brunei"));
    db.addFlag(new Flags("bulgaria",27,"Bulgaria" ));
    db.addFlag(new Flags("burkina",28,"Burkina"));
    db.addFlag(new Flags("burundi", 29,"Burundi" ));
    db.addFlag(new Flags("cambodia",30,"Cambodia" ));
    db.addFlag(new Flags("cameroon",31,"Cameroon"));
    db.addFlag(new Flags("canada",32,"Canada" ));
    db.addFlag(new Flags("cape_verde",33,"Cape Verde"));
    db.addFlag(new Flags("cayman_islands",34,"Cayman Islands"));
    db.addFlag(new Flags("central_africa_republic", 35,"Central African Republic" ));
    db.addFlag(new Flags("chad",36,"Chad"));
    db.addFlag(new Flags("chile",37,"Chile"));
    db.addFlag(new Flags("china",38,"China"));
    db.addFlag(new Flags("colombia",39,"Colombia"));
    db.addFlag(new Flags("comoros",40,"Comoros"));
    db.addFlag(new Flags("congo",41,"Congo"));
    db.addFlag(new Flags("cook_islands",42,"Cook Islands" ));
    db.addFlag(new Flags("costa_rica",43,"Costa Rica"));
    db.addFlag(new Flags("croatia",44,"Croatia"));
    db.addFlag(new Flags("cuba",45,"Cuba"));
    db.addFlag(new Flags("cyprus", 46,"Cyprus"));
    db.addFlag(new Flags("czech",47,"Czech"));
    db.addFlag(new Flags("democratic_republic_congo",48,"Democratic Republic-Congo"));
    db.addFlag(new Flags("denmark", 49,"Denmark"));
    db.addFlag(new Flags("djibouti",50,"Djibouti"));
    db.addFlag(new Flags("dominica",51,"Dominica"));
    db.addFlag(new Flags("dominican_republic",52,"Dominican Republic"));
    db.addFlag(new Flags("east Timor",53,"East Timor"));
    db.addFlag(new Flags("ecuador",54,"Ecuador"));
    db.addFlag(new Flags("egypt",55,"Egypt"));
    db.addFlag(new Flags("el_salvador",56,"El Salvador"));
    db.addFlag(new Flags("england",57,"England"));
    db.addFlag(new Flags("equatorial_guinea",58,"Equatorial Guinea"));
    db.addFlag(new Flags("eritrea",59,"Eritrea"));
    db.addFlag(new Flags("estonia",60,"Estonia"));
    db.addFlag(new Flags("ethiopia",61,"Ethiopia" ));
    db.addFlag(new Flags( "falkland_islands",62,"Falkland Islands" ));
    db.addFlag(new Flags("fiji", 63,"Fiji"));
    db.addFlag(new Flags("finland",64,"Finland"));
    db.addFlag(new Flags("france",65,"France" ));
    db.addFlag(new Flags("french_polynesia",66,"French Polynesia"));
    db.addFlag(new Flags("gabon", 67,"Gabon" ));
    db.addFlag(new Flags("gambia",68,"Gambia" ));
    db.addFlag(new Flags("georgia",69,"Georgia"));
    db.addFlag(new Flags("germany",70,"Germany"));
    db.addFlag(new Flags("ghana", 71,"Ghana"));
    db.addFlag(new Flags("greece",72,"Greece"));
    db.addFlag(new Flags("greenland",73,"Greenland"));
    db.addFlag(new Flags("grenada",74,"Grenada" ));
    db.addFlag(new Flags("guam",75,"Guam" ));
    db.addFlag(new Flags("guatemala",76,"Guatemala"));
    db.addFlag(new Flags("guinea_bissau",77,"Guinea Bissau"));
    db.addFlag(new Flags("guinea",78,"Guinea"));
    db.addFlag(new Flags("guyana",79,"Guyana"));
    db.addFlag(new Flags("haiti",80,"Haiti"));
    db.addFlag(new Flags("honduras",81,"Honduras"));
    db.addFlag(new Flags("hungary",82,"Hungary"));
    db.addFlag(new Flags("iceland",83,"Iceland"));
    db.addFlag(new Flags("india",84,"India"));
    db.addFlag(new Flags("indonesia",85,"Indonesia" ));
    db.addFlag(new Flags("iran",86,"Iran" ));
    db.addFlag(new Flags("iraq",87,"Iraq" ));
    db.addFlag(new Flags("ireland",88,"Ireland"));
    db.addFlag(new Flags("israel",89,"Israel"));
    db.addFlag(new Flags("italy",90,"Italy"));
    db.addFlag(new Flags("ivory_coast",91,"Ivory Coast"));
    db.addFlag(new Flags("jamaica",92,"Jamaica"));
    db.addFlag(new Flags("japan",93,"Japan"));
    db.addFlag(new Flags("jordan",94,"Jordan" ));
    db.addFlag(new Flags("kazakhstan",95,"Kazakhstan"));
    db.addFlag(new Flags("kenya",96,"Kenya" ));
    db.addFlag(new Flags("kiribati",97,"Kiribati"));
    db.addFlag(new Flags("Kosovo",98,"kosovo"));
    db.addFlag(new Flags("kuwait",99,"Kuwait"));
    db.addFlag(new Flags("kyrgyzstan",100,"Kyrgyzstan"));
    db.addFlag(new Flags("laos",101,"Laos" ));
    db.addFlag(new Flags("latvia",102,"Latvia"));
    db.addFlag(new Flags("lesotho",104,"Lesotho" ));
    db.addFlag(new Flags("liberia",105,"Liberia"));
    db.addFlag(new Flags("libya",106,"Libya"));
    db.addFlag(new Flags("liechtenstein",107,"Liechtenstein"));
    db.addFlag(new Flags("lithuania",108,"Lithuania" ));
    db.addFlag(new Flags("luxembourg",109,"Luxembourg"));
    db.addFlag(new Flags("macau",110,"Macau"));
    db.addFlag(new Flags("macedonia",111,"Macedonia"));
    db.addFlag(new Flags("madagascar",112,"Madagascar"));
    .
    .//more flags till ID218
    .
    db.addFlag(new Flags("venezuela",211,"Venezuela"));
    db.addFlag(new Flags("vietnam",212,"Vietnam"));
    db.addFlag(new Flags("virgin_islands",213,"Virgin Islands"));
    db.addFlag(new Flags("wales",214,"Wales"));
    db.addFlag(new Flags("westren_sahara",215,"Westren Sahara" ));
    db.addFlag(new Flags("yemen",216,"Yemen"));
    db.addFlag(new Flags("zambia",217,"Zambia"));
    db.addFlag(new Flags("zimbabwe",218,"Zimbabwe"));


}

DatabaseHandler (数据库上的所有功能):

public class DatabaseHandler extends SQLiteOpenHelper{

private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "flagsManager";

// Contacts table name
private static final String TABLE_FLAGS = "flag";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_IMAGE = "image";




public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_FLAGS + " ( "
            + KEY_ID + " INTEGER  PRIMARY KEY," + KEY_NAME + " VARCHAR(64)," 
            +KEY_IMAGE +" VARCHAR(64) ) ";
    db.execSQL(CREATE_CONTACTS_TABLE);


}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_FLAGS);

    onCreate(db);
}


void addFlag(Flags flag) {//הכנסת מידע למערך
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_IMAGE, flag.getImage());              //Image name/string
    values.put(KEY_ID, flag.getId());                   //Country Id
    values.put(KEY_NAME, flag.getName());              // Country Name

    db.insert(TABLE_FLAGS, null, values);

    db.close();                                           // Closing database connection
}

Flags getFlag(int id) {
    List<Flags> flagList = new ArrayList<Flags>();
    String selectQuery = "SELECT  * FROM " + TABLE_FLAGS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {

            Flags flag = new Flags();
            flag.setId(Integer.parseInt(cursor.getString(0)));
            flag.setName(cursor.getString(1));
            flag.setImage(cursor.getString(2));

            //     flag.setPhoneNumber(cursor.getString(3));
            flagList.add(flag);
        } while (cursor.moveToNext());
    }

    Log.d("into ",flagList.get(id)+"" );
    Log.d("into ",id+"" );

    return flagList.get(id);
}

public List<Flags> getAllFlags() {
    List<Flags> flagList = new ArrayList<Flags>();

    String selectQuery = "SELECT  * FROM " + TABLE_FLAGS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {

            Flags flag = new Flags();//String image, int id, String name
            flag.setImage(cursor.getString(2));
            flag.setId(Integer.parseInt(cursor.getString(0)));
            flag.setName(cursor.getString(1));

            //     contact.setPhoneNumber(cursor.getString(3));
            flagList.add(flag);
        } while (cursor.moveToNext());
    }
    return flagList;
}


public int updateContact(Flags flag) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, flag.getName());
    values.put(KEY_IMAGE, flag.getImage());

    return db.update(TABLE_FLAGS, values, KEY_ID + " = ?",
            new String[] { String.valueOf(flag.getId()) });
}

public void deleteContact(Flags flag) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_FLAGS, KEY_ID + " = ?",
            new String[] { String.valueOf(flag.getId()) });
    db.close();
}


public int getContactsCount() {
    String countQuery = "SELECT  * FROM " + TABLE_FLAGS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    return cursor.getCount();
}

}

经典模式(游戏本身)://您可以看到pic_view是从数据库导入图像并将其放入游戏中

public class ClassicMode extends Activity implements AnimationListener{//מהמשחק עצמו

String pic;//תמונה של הדגל
Button answer1;//תשובות
Button answer2;
Button answer3;
Button answer4;
Button hint;
TextView guess;
TextView numOfGuess;
TextView score;
TextView scorenum;
MediaPlayer mpHint;
MediaPlayer mpNext;
MediaPlayer mpWrong;
Animation animationfadein;
Animation animationfadeout;
String fn;
Guesses G;
Score s;
Button [] b = new Button[4];
DatabaseHandler db = new DatabaseHandler(this);
List<String>WrongAnswers=new ArrayList<String>();

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    score =(TextView)findViewById(R.id.score);
    scorenum =(TextView)findViewById(R.id.scorenum);
    scorenum.setText(String.valueOf(s.score));
    guess =(TextView)findViewById(R.id.guesses);
    numOfGuess=(TextView)findViewById(R.id.numOfGuesses);
    numOfGuess.setText(String.valueOf(Guesses.numOfGuesses));
    hint =(Button)findViewById(R.id.hint);
    hint.setOnClickListener(hintOnClickListener);
    mpHint = MediaPlayer.create(this,R.raw.hint_sound);
    mpNext = MediaPlayer.create(this, R.raw.next_flag);
    mpWrong = MediaPlayer.create(this, R.raw.wrong_answer);
    animationfadein = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    animationfadeout = AnimationUtils.loadAnimation(this, R.anim.fade_out);

    Flags f = new Flags();
    Random r = new Random();//הדגל שיבחר לשאלה
    int num = r.nextInt(160);//Up

    f = db.getFlag(num);//הצגת הדגל הרנדומלי שיצא
    fn = f.getName().toString();
    pic = f.getImage().toString();
    pic_view(pic);//מעבר לפונקציה להשמת התמונה של הדגל במשחק

    //מערך ארבע כפתורים כנגד ארבע תשובות

    b[0] = (Button)findViewById(R.id.button1);
    b[1] = (Button)findViewById(R.id.button2);
    b[2] = (Button)findViewById(R.id.button3);
    b[3] = (Button)findViewById(R.id.button4);

    List<String>Answers=new ArrayList<String>();//מערך תשובות

    Answers.add(f.getName().toString());//הוספת התשובה הנכונה
    for(int i=1;i<4;i++)
    {
        num = r.nextInt(200);
        String valToAdd1 = db.getFlag(num).getName().toString();
        if(!Answers.contains(valToAdd1)){
            WrongAnswers.add(valToAdd1);
            Answers.add(valToAdd1);
        }

    }

    /*num = r.nextInt(30);
    Answers.add(db.getFlag(num).getName().toString());//הוספת 3 תשובות רנדומליות
    num = r.nextInt(30);
    Answers.add(db.getFlag(num).getName().toString());
    num = r.nextInt(30);
    Answers.add(db.getFlag(num).getName().toString());*/

    Collections.shuffle(Answers);//ערבוב התשובות

    for(int i=0;i<Answers.size();i++)
    {
        b[i].setText(Answers.get(i));//השמת התשובות מהמהערך למערך הכפתורים
        b[i].startAnimation(animationfadein);
    }
}//end of OnCreat


public boolean onOptionsItemSelected(MenuItem item){//actionbar activity
    Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
    startActivityForResult(myIntent, 0);
    return true;
}

@SuppressLint("NewApi")
public void resetQuiz()
{
    recreate();
}

private OnClickListener hintOnClickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        mpHint.start();
        if(Guesses.numOfGuesses==1)
        {
            G.setNumOfGuesses(3);
            finish();//כאשר מספר הניחושים
            return;
        }
        else
            G.numOfGuesses--;

        int invisblecount=0;
        for(int i=0;i<b.length;i++){
            if(invisblecount<2){
                String buttonText = b[i].getText().toString();  
                if(buttonText.equals(WrongAnswers.get(0))||buttonText.equals(WrongAnswers.get(1)))
                {
                    b[i].startAnimation(animationfadeout);
                    b[i].setVisibility(View.INVISIBLE);
                    invisblecount++;
                }
            }
        }
    }
};

public void check(View v)
{
    Log.d("yes", fn);
    Button b = (Button)v;
    String text = b.getText().toString();
    if(text.equals(fn))
    {
        mpNext.start();
        s.score+=5;
        resetQuiz();
    }
    else
    {
        mpWrong.start();
        if(Guesses.numOfGuesses==1)
        {
            Log.d("kkk", "inside if");
            G.setNumOfGuesses(3);
            Toast toast = Toast.makeText(getApplicationContext(), "No more guesses :( Lets see what you made so far...", Toast.LENGTH_SHORT);
            toast.show();
            Intent person = new Intent(ClassicMode.this,Person.class);
            String extra = scorenum.getText().toString();
            person.putExtra("Score", extra);
            startActivity(person);
        }
        Guesses.numOfGuesses--;
        if(s.score>0)
            s.score-=5;
        scorenum.setText(String.valueOf(s.score));
        numOfGuess.setText(String.valueOf(Guesses.numOfGuesses));
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.second, menu);
    return true;
}

private void pic_view(String pic2) {
    // TODO Auto-generated method stub
    //גישה לדגל לפי שמו וייבוא התמונה
    Log.d("Result from pic function " , pic2);
    ImageView imageView = (ImageView)findViewById(R.id.imageView1);
    String uri ="@drawable/";
    uri += pic2;    
    int imageResource = getResources().getIdentifier(uri, pic2, getPackageName());//הצוות התמונה 
    Drawable res= getResources().getDrawable(imageResource);//ציור התמונה
    imageView.setImageDrawable(res);
}


@Override
public void onAnimationEnd(Animation animation) {
    // TODO Auto-generated method stub
    Toast.makeText(getBaseContext(), "Animatoin Stoped", Toast.LENGTH_SHORT).show();
}


@Override
public void onAnimationRepeat(Animation animation) {
    // TODO Auto-generated method stub

}


@Override
public void onAnimationStart(Animation animation) {
    // TODO Auto-generated method stub

}

}

最后一个堆栈跟踪:(包括致命错误):

04-19 23:00:40.967: D/yes(10808): Botswana
04-19 23:00:41.157: D/into(10808): com.example.flagsgame.Flags@424c4fc0
04-19 23:00:41.157: D/into(10808): 154
04-19 23:00:41.157: D/Result from pic function(10808): pitcairn_islands
04-19 23:00:41.167: W/ResourceType(10808): No package identifier when getting value for resource number 0x00000000
04-19 23:00:41.167: E/ActivityThread(10808): Failed to find resource
04-19 23:00:41.167: E/ActivityThread(10808): android.content.res.Resources$NotFoundException: Resource ID #0x0
04-19 23:00:41.167: E/ActivityThread(10808):    at android.content.res.Resources.getValue(Resources.java:1195)
04-19 23:00:41.167: E/ActivityThread(10808):    at android.content.res.Resources.getDrawable(Resources.java:729)
04-19 23:00:41.167: E/ActivityThread(10808):    at android.content.res.Resources.getDrawable(Resources.java:711)
04-19 23:00:41.167: E/ActivityThread(10808):    at com.example.flagsgame.ClassicMode.pic_view(ClassicMode.java:213)
04-19 23:00:41.167: E/ActivityThread(10808):    at com.example.flagsgame.ClassicMode.onCreate(ClassicMode.java:82)
04-19 23:00:41.167: E/ActivityThread(10808):    at android.app.Activity.performCreate(Activity.java:5231)
04-19 23:00:41.167: E/ActivityThread(10808):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-19 23:00:41.167: E/ActivityThread(10808):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
04-19 23:00:41.167: E/ActivityThread(10808):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
04-19 23:00:41.167: E/ActivityThread(10808):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3762)
04-19 23:00:41.167: E/ActivityThread(10808):    at android.app.ActivityThread.access$900(ActivityThread.java:144)
04-19 23:00:41.167: E/ActivityThread(10808):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
04-19 23:00:41.167: E/ActivityThread(10808):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-19 23:00:41.167: E/ActivityThread(10808):    at android.os.Looper.loop(Looper.java:136)
04-19 23:00:41.167: E/ActivityThread(10808):    at android.app.ActivityThread.main(ActivityThread.java:5146)
04-19 23:00:41.167: E/ActivityThread(10808):    at java.lang.reflect.Method.invokeNative(Native Method)
04-19 23:00:41.167: E/ActivityThread(10808):    at java.lang.reflect.Method.invoke(Method.java:515)
04-19 23:00:41.167: E/ActivityThread(10808):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
04-19 23:00:41.167: E/ActivityThread(10808):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
04-19 23:00:41.167: E/ActivityThread(10808):    at dalvik.system.NativeStart.main(Native Method)
04-19 23:00:41.177: D/AndroidRuntime(10808): Shutting down VM
04-19 23:00:41.177: W/dalvikvm(10808): threadid=1: thread exiting with uncaught exception (group=0x41674db8)
04-19 23:00:41.177: E/AndroidRuntime(10808): FATAL EXCEPTION: main
04-19 23:00:41.177: E/AndroidRuntime(10808): Process: com.example.flagsgame, PID: 10808
04-19 23:00:41.177: E/AndroidRuntime(10808): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flagsgame/com.example.flagsgame.ClassicMode}: android.content.res.Resources$NotFoundException: Resource ID #0x0
04-19 23:00:41.177: E/AndroidRuntime(10808):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3762)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at android.app.ActivityThread.access$900(ActivityThread.java:144)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at android.os.Looper.loop(Looper.java:136)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at  android.app.ActivityThread.main(ActivityThread.java:5146)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at java.lang.reflect.Method.invokeNative(Native Method)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at java.lang.reflect.Method.invoke(Method.java:515)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at dalvik.system.NativeStart.main(Native Method)
04-19 23:00:41.177: E/AndroidRuntime(10808): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
04-19 23:00:41.177: E/AndroidRuntime(10808):    at android.content.res.Resources.getValue(Resources.java:1195)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at android.content.res.Resources.getDrawable(Resources.java:729)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at android.content.res.Resources.getDrawable(Resources.java:711)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at com.example.flagsgame.ClassicMode.pic_view(ClassicMode.java:213)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at com.example.flagsgame.ClassicMode.onCreate(ClassicMode.java:82)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at android.app.Activity.performCreate(Activity.java:5231)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-19 23:00:41.177: E/AndroidRuntime(10808):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
04-19 23:00:41.177: E/AndroidRuntime(10808):    ... 12 more

很抱歉有很长的代码和堆栈跟踪,但我提供了所需的所有信息。

1 个答案:

答案 0 :(得分:0)

ClassicMode.java,第82行 - 您正尝试将0设置为资源值。我只能猜测,但检查一下你是不是在做这样的事情:

int value; //It sets to 0
someTextView.setText(value); //It thinks you want to set a resource as the text, but there is no resource with id 0

如果要将数值插入TextView,请始终使用 "" + numericalValueString.valueOf(numericalValue)

更新:

好的,现在我们了解更多。首先,在冒险接收0作为资源ID的问题之前,你应该创建一个条件:

if(imageResource !=0)
{
    //create Drawable and put it into a View
}

这将修复崩溃。至于第二个问题,您需要使用文档检查传递给getResources().getIdentifier(uri, pic2, getPackageName());的值,并找出方法返回0的原因。