2011-08-30 3 views
1

대화 상자 창을 여는 옵션 메뉴 항목을 사용할 수 있습니까? 여기에 내가 가진 무엇 : 나는 점수 옵션 버튼을 선택하면Android : 옵션 메뉴에서 맞춤 팝업 대화 상자 열기

public class main extends Activity { 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu, menu); 
    return true; 
} 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    int score; 

    SharedPreferences stats = getSharedPreferences("TRHprefs", MODE_WORLD_READABLE); 

    score = stats.getInt("score", 0); 

    switch (item.getItemId()) { 

     case R.id.score: 

      Context mContext = getApplicationContext(); 
      Dialog dialog = new Dialog(mContext); 

      dialog.setContentView(R.layout.options_menu); 
      dialog.setTitle("Hero Stats"); 

      TextView b10 = (TextView) dialog.findViewById(R.id.tolevel); 
      b10.setText("Score: " + score); 

      dialog.setCancelable(true); 
      dialog.show(); 

          break; 
     case R.id.options:  
      //Options 

          break; 
     case R.id.quit: 
      //Quit 
          break; 
    } 
    return true; 
} 
} 

는 응용 프로그램의 힘은 닫습니다. 어떤 아이디어?

+0

logcat 출력을 제공해 주실 수 있습니까? 그냥 입력하지 않고 그냥 추측 할 수 있습니까? – EpicOfChaos

답변

1

당신은 쉽게 정확하게 잘못되어 가고 있지만, 코드를보고 내 기대가 어떤 결정하기 위해 확인하기 위해 로그 캣 출력을 포함해야 그 중 하나 레이아웃 파일 options_menu.xml과에 오류가 있습니다

  1. TextView b10 = (TextView) dialog.findViewById(R.id.tolevel);b10에 대해 null 값을 반환합니다. 이 경우 다음 줄에서 NullPointerException이 발생하고 응용 프로그램이 강제 종료됩니다.
  2. Dialog dialog = new Dialog(mContext);은 Activity가 아닌 ApplicationContext를 전달하기 때문에 실패합니다. Dialog dialog = new Dialog(this);을 사용해보세요.