2016-01-22 69 views
0

我在android Studio中製作應用程序,如果您在手機上打開應用程序,則會看到Launcer活動。我有一個按鈕,可以將您發送到遊戲所在的新活動。一旦我點擊開始按鈕,應用程序關閉,並不會去其他活動。這是爲什麼?新佈局不會彈出

這是我的啓動活動代碼:

package joenio.sirname; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 

public class SirName_launcher extends AppCompatActivity { 

public static Button button_start; 

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

public void StartButton(){ 
    button_start = (Button) findViewById(R.id.button_start); 

    button_start.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        Intent intent1 = new Intent("joenio.sirname.Game"); 
        startActivity(intent1); 
       } 

      } 
    ); 



} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_sir_name_launcher, menu); 
    return true; 

} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

這是第二個活動代碼:

package joenio.sirname; 


import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import java.util.ArrayList; 
import android.widget.Toast; 


public class Game extends AppCompatActivity { 
    public static EditText editText_surname; 
    public static TextView textView_name; 
    public static Button button_check; 

int x =0; //to keep track of qustions 






//Context editText_this = this; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_game); 
    Displayquestions(); 
} 
public void Displayquestions(){ 


    final ArrayList<String> mQuestionList = new ArrayList<>(); 


    mQuestionList.add("1+2"); 
    mQuestionList.add("6+8"); 
    mQuestionList.add("5 * 6"); 
    mQuestionList.add("8*5"); 
    mQuestionList.add("6+16"); 
    mQuestionList.add("18-5"); 



    textView_displayquestion.setText((mQuestionList.get(x)));//displayquestion is textview 




    final ArrayList<String> mAnswerList=new ArrayList<>(); 

    mAnswerList.add("3"); 
    mAnswerList.add("14"); 
    mAnswerList.add("30"); 
    mAnswerList.add("40"); 
    mAnswerList.add("22"); 
    mAnswerList.add("13"); 






    //button_check is the button when user click it will first check answer and than move to next question if answer is correct 


    button_check.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      //editText_this; 
      String answer = editText_ans.getText().toString(); 
      if (answer.equals(mAnswerList.get(x))) { 
       x = x + 1; 
       textView_displayquestion.setText(mQuestionList.get(x)); //answer is correct display next quesion 
       Toast.makeText(getApplication().getBaseContext(), 
         (R.string.Nice), Toast.LENGTH_SHORT).show(); 
      } else { 
       Toast.makeText(getApplication().getBaseContext(), 
         (R.string.tryagain), Toast.LENGTH_SHORT).show(); 

      } 
     } 
    }); 


    } 
} 
+1

Game活動,你可以發佈您的logcat的? – Kristo

回答

2

您不可以在初始化您的TextViewButton時發生這種情況,它必然會導致NullPointerException

改變這樣

TextView textView_displayquestion; 
Button button_check; 
EditText editText_ans; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_game); 
    textView_displayquestion = (TextView)findViewById(R.id.displayquestion); //change as per your id 
    button_check = (Button)findViewById(R.id.buttoncheck); //change as per your id 
    editText_ans = (EditText)findViewById(R.id.answer); //change as per your id 
    Displayquestions(); 
} 
+0

非常感謝你!我忘了那部分! –

2

在你按一下按鈕,更改爲下面的代碼。

Intent intent1 = new Intent(SirName_launcher.this, Game.class); 
startActivity(intent1); 

而且也是你的清單文件中添加新的遊戲活動了。

+0

謝謝你!對此,我真的非常感激! –