android资源ID#0x0

时间:2015-03-12 09:43:38

标签: android android-resources eclipse-adt

首先在这里检查了这个,我没有找到适合我的解决方案。 现在,所以我启动了我的应用程序,然后按下strat按钮,当它应该移动到另一个activirty它抛出这个错误。 日志

03-12 11:30:04.037: I/Timeline(28116): Timeline: Activity_launch_request      id:com.example.flagchallenge time:81168367
03-12 11:30:04.157: D/into(28116): com.example.flagchallenge.Flags@42730288
03-12 11:30:04.157: D/into(28116): 32
03-12 11:30:04.157: D/Result from pic function(28116): canada
03-12 11:30:04.157: W/ResourceType(28116): No package identifier when     getting value for resource number 0x00000000
03-12 11:30:04.157: E/ActivityThread(28116): Failed to find resource
03-12 11:30:04.157: E/ActivityThread(28116):     android.content.res.Resources$NotFoundException: Resource ID #0x0
03-12 11:30:04.157: E/ActivityThread(28116):    at     android.content.res.Resources.getValue(Resources.java:1195)
03-12 11:30:04.157: E/ActivityThread(28116):    at     android.content.res.Resources.getDrawable(Resources.java:729)
03-12 11:30:04.157: E/ActivityThread(28116):    at     android.content.res.Resources.getDrawable(Resources.java:711)
03-12 11:30:04.157: E/ActivityThread(28116):    at     com.example.flagchallenge.ClassicMode.pic_view(ClassicMode.java:132)
03-12 11:30:04.157: E/ActivityThread(28116):    at     com.example.flagchallenge.ClassicMode.onCreate(ClassicMode.java:60)
03-12 11:30:04.157: E/ActivityThread(28116):    at 

主要活动代码:

public class MainActivity extends Activity implements OnClickListener{
private static final String TAG = "Flags Challenge" ;//יצירת טאג למשחק לשימוש בםונקציות כמו exception
Button start;
Button time;
Button reguler;
DatabaseHandler db = new DatabaseHandler(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    start = (Button)findViewById(R.id.start_btn);
    time = (Button)findViewById(R.id.time);
    reguler = (Button)findViewById(R.id.reguler);
    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 void onClick(View v) {
    // TODO Auto-generated method stub
    start.setVisibility(start.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); 
        }
    });

    time.setOnClickListener(new View.OnClickListener() {

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

public void creat_rows()
{
    Log.d(TAG, "Inserting...");
    //image     id   name 
    db.addFlag(new Flags("albania",1,"albania"));
}
}

经典模式:

public class ClassicMode extends Activity{


String pic;//תמונה של הדגל
Button answer1;//תשובות
Button answer2;
Button answer3;
Button answer4;
TextView guess;//
TextView numOfGuess;//
DatabaseHandler db = new DatabaseHandler(this);
String fn;
int Nguess=3;//מסםר ניחושים
int Nguesstemp=Nguess;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    guess =(TextView)findViewById(R.id.guesses);
    numOfGuess=(TextView)findViewById(R.id.numOfGuesses);
    //numOfGuess.setText(Integer.toString(Nguess));
    numOfGuess.setText(String.valueOf(Nguesstemp));
    Flags f = new Flags();
    Random r = new Random();//הדגל שיבחר לשאלה
    int num = r.nextInt(70);//Up
    //Log.d("yes", "yes");
    //Random ra = new Random();//הדגלים שיהיו בתשובות
    //int numA = ra.nextInt(4);//Up

    f = db.getFlag(num);//הצגת הדגל הרנדומלי שיצא
    //  final Flags d = f; // trying to make it final but it dosent help
    //fn = f.getName().toString();
    pic = f.getImage().toString();
    pic_view(pic);//מעבר לפונקציה להשמת התמונה של הדגל במשחק
    //מערך ארבע כפתורים כנגד ארבע תשובות
    Button [] b = new Button[4];
    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());//הוספת התשובה הנכונה
    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));//השמת התשובות מהמהערך למערך הכפתורים
    }
}

@SuppressLint("NewApi")
public void resetQuiz()
{
    //finish();
    recreate();
    //numOfGuess.setText(String.valueOf(Nguess));
}

public void check(View v)
{
    Log.d("yes", fn);
    Button b = (Button)v;
    String text = b.getText().toString();
    if(text.equals(fn))
        resetQuiz();
    else
    {
        if(Nguess==1)
        {
            finish();//כאשר מספר הניחושים
        }
        Nguess--;
        Nguesstemp=Nguess;
        //numOfGuess=(TextView)findViewById(R.id.numOfGuesses);
        //numOfGuess.setText(Integer.toString(Nguess));
        numOfGuess.setText(String.valueOf(Nguesstemp));
    }

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    return super.onCreateOptionsMenu(menu);
}


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

}
}

1 个答案:

答案 0 :(得分:0)

请尝试更改pic_view(...)中的以下行

int imageResource = getResources().getIdentifier(uri, pic2, getPackageName());

int imageResource = getResources().getIdentifier(pic2, "drawable", getPackageName());