移动到另一个Activity时使用Intent时出错

时间:2015-04-17 14:49:43

标签: java android android-intent

所以我想在Intent函数中使用ClassicMode(从Person类到check()类)转移到新类。我创建了一个新类但是当我到达startActivity(intent)行时出现错误但我无法弄清楚原因。

我使用Log.d尝试找到没有运气的问题。我希望你能帮助我。我将把堆栈跟踪放在下面,谢谢。

ClassicMode.java

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);
            //finish();//כאשר מספר הניחושים
            Intent person = new Intent(ClassicMode.this,Person.class);
            String extra = scorenum.getText().toString();
            Log.d("lll", "Get Extra");
            person.putExtra("Score", extra);
            Log.d("sss", "new activ");
            startActivity(person);
            Log.d("dasdas", "inside Activ");
            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);
            Log.d("kkk", "inside person");*/
            //return;
        }
        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

}

}

Person.java

public class Person extends Activity{

EditText name;
Button ok;
TextView enter;
String score;
Map<String,String>Names=new HashMap<String,String>();

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

    Intent intent = getIntent();
    score = intent.getStringExtra(score);
    name =(EditText)findViewById(R.id.namep);
    ok =(Button)findViewById(R.id.send);
    enter =(TextView)findViewById(R.id.entername);

    ok.setOnClickListener(SendActionListener);
}

private OnClickListener SendActionListener =  new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Names.put(name.getText().toString(),score);
    }
};
}

堆栈跟踪:

04-17 17:45:46.030: D/kkk(5537): inside if
04-17 17:45:46.030: D/lll(5537): Get Extra
04-17 17:45:46.030: D/sss(5537): new activ
04-17 17:45:46.030: I/Timeline(5537): Timeline: Activity_launch_request id:com.example.flagsgame time:93135823
04-17 17:45:46.030: D/AndroidRuntime(5537): Shutting down VM
04-17 17:45:46.030: W/dalvikvm(5537): threadid=1: thread exiting with uncaught exception (group=0x4162cdb8)
04-17 17:45:46.050: E/AndroidRuntime(5537): FATAL EXCEPTION: main
04-17 17:45:46.050: E/AndroidRuntime(5537): Process: com.example.flagsgame, PID: 5537
04-17 17:45:46.050: E/AndroidRuntime(5537): java.lang.IllegalStateException: Could not execute method of the activity
04-17 17:45:46.050: E/AndroidRuntime(5537):     at android.view.View$1.onClick(View.java:3830)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at android.view.View.performClick(View.java:4445)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at android.view.View$PerformClick.run(View.java:18446)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at android.os.Handler.handleCallback(Handler.java:733)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at android.os.Looper.loop(Looper.java:136)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at android.app.ActivityThread.main(ActivityThread.java:5146)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at java.lang.reflect.Method.invokeNative(Native Method)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at java.lang.reflect.Method.invoke(Method.java:515)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at dalvik.system.NativeStart.main(Native Method)
04-17 17:45:46.050: E/AndroidRuntime(5537): Caused by: java.lang.reflect.InvocationTargetException
04-17 17:45:46.050: E/AndroidRuntime(5537):     at java.lang.reflect.Method.invokeNative(Native Method)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at java.lang.reflect.Method.invoke(Method.java:515)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at android.view.View$1.onClick(View.java:3825)
04-17 17:45:46.050: E/AndroidRuntime(5537):     ... 11 more
04-17 17:45:46.050: E/AndroidRuntime(5537): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.flagsgame/com.example.flagsgame.Person}; have you declared this activity in your AndroidManifest.xml?
04-17 17:45:46.050: E/AndroidRuntime(5537):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at android.app.Activity.startActivityForResult(Activity.java:3424)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at android.app.Activity.startActivityForResult(Activity.java:3385)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at android.app.Activity.startActivity(Activity.java:3627)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at android.app.Activity.startActivity(Activity.java:3595)
04-17 17:45:46.050: E/AndroidRuntime(5537):     at com.example.flagsgame.ClassicMode.check(ClassicMode.java:188)
04-17 17:45:46.050: E/AndroidRuntime(5537):     ... 14 more

2 个答案:

答案 0 :(得分:1)

最可能的问题是Activity未在AndroidManifest.xml中声明。你的错误说:

  

您是否在AndroidManifest.xml中声明了此活动?

你应该先这样做。


this answer关于如何做到:

  

您将放在应用程序元素的中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your.package.name">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Activity1" android:label="@string/app_name"></activity>
        <activity android:name=".Activity2"></activity>
    </application>
    <uses-sdk android:minSdkVersion="4" />
</manifest>
     

.Activity2是您的第二项活动。

答案 1 :(得分:-1)

您的意图未在清单文件中声明。