2015-06-20 27 views
0

我正在創建一個練習Android編程的測驗。我能夠運行Android程序,它工作正常。但是現在,我添加了一個ContextMenu。如果我點擊任何項目,活動應重新啓動一個新值。但事實並非如此。如何調用活動以重新開始?

TextView Qsn; 
Button TopicTitle; 
Button Ybtn; 
Button Nbtn; 
String check; 

@Override protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.history); 
    Qsn = (TextView) findViewById(R.id.Question); 
    TopicTitle = (Button)findViewById(R.id.Topictxt); 
    Ybtn = (Button)findViewById(R.id.Yesbtn); 
    Nbtn = (Button)findViewById(R.id.Nobtn); 
    Intent intent = getIntent(); 
    Bundle bundle =intent.getExtras(); 
    check=bundle.getString("Bundlekey") ; 
    TopicTitle.setText(check); 
    Toast.makeText(getApplicationContext(), check, Toast.LENGTH_SHORT) .show();  
    registerForContextMenu(TopicTitle); 
    if (check.matches("History")) { 
     .... 
    } 
} 

該代碼檢查哪些主題已被選中,它工作正常,直到這裏。但是現在我添加了一個ContextMenu,所以如果用戶想要更改主題,他可以長按按鈕並選擇主題並更改他的新主題以獲得新問題。

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 
    getMenuInflater().inflate(R.menu.topic_selection_in_question, menu); 
} 

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    String Value_from_item ;   
    Value_from_item = (String) item.getTitle();  
    check = Value_from_item; 
    Intent intent = getIntent(); finish(); startActivity(intent); 
    return super.onContextItemSelected(item); 
} 

我在哪裏犯了錯誤?邏輯中是否有錯誤?我沒有收到運行時錯誤。一切正常。如果你能幫助我,我會很高興。提前致謝。

回答

1

,而不是這個代碼

Intent intent = getIntent(); finish(); startActivity(intent); 

使用

Intent intent = new Intent(youractivity.this, Youractivity.class); 
finish(); 
startActivity(intent);