2014-03-07 38 views
0
public void onClick(View view) { 
    TextView snt = (TextView) 
      view.findViewById(R.id.shape_name_text); 
    String Selected = snt.getText().toString(); 
    String triangle = getResources().getString(R.string.triangle); 
    if (Selected.equals(triangle)) { 


     Intent intent = new Intent(this, TriCalculator.class); 
     startActivity(intent); 
    } 

} 

我的意圖是顯示幾個錯誤,我不知道下一步該怎麼做。如何在Android中點擊開始新的活動?

+1

顯示哪些錯誤? – T3KBAU5

+1

什麼錯誤?顯示logcat – Piyush

+1

檢查IDE(ADT/Eclipse)中的錯誤日誌/窗口。他們應該向您提供有關錯誤的信息,並且需要修復。您可以通過雙擊這些錯誤條目跳轉到大多數IDE中的錯誤。 – Patric

回答

1

清單中的錯誤將是有益的,但這裏至少有兩種:

此行

TextView snt = (TextView) view.findViewById(R.id.shape_name_text); 

告訴它在View看點擊(可能是Button)爲您TextView它是顯然不在那裏。假設shape_name_text是在同一個layout as your按鈕just remove view`

TextView snt = (TextView) findViewById(R.id.shape_name_text); 

第二個問題是在這條線

Intent intent = new Intent(this, TriCalculator.class); 

thisListener如果你是在一個內部類。改變,要

Intent intent = new Intent(v.getContext(), TriCalculator.class); 

編輯

隨着黑帶在評論中指出,在回答問題的第一部分是假設你已經附着在View而不是ViewGroupOnClickListener其中包含了你View然後嘗試初始化。

另一個編輯

看來(通過刪除評論)恰當Intent進口未添加

import android.content.Intent; 
+1

只有在onviewListener設置在View實例上而不是ViewGroup上時,第一部分纔是真實的。 – Blackbelt

0

使用:

findViewById(R.id.shape_name_text); 

刪除視圖。

0
public void onClick(View view) { 
TextView snt = (TextView) 
     view.findViewById(R.id.shape_name_text); 
String Selected = snt.getText().toString(); 
String triangle = getResources().getString(R.string.triangle); 
if (Selected.equals(triangle)) { 


    Intent intent = new Intent(this, TriCalculator.class); 
    startActivity(intent); 
} 
} 

在你的代碼張貼以上,實際上是告訴你要調用另一個OnClickListener操作系統,通過傳遞this作爲參數爲您的新Intent。爲了它的工作,你需要將其更改爲類似:

Intent intent = new Intent(MainActivity.this, TriCalculator.class); 

Intent intent = new Intent(getAppplicationContext(), TriCalculator.class); 

(還有其他的選擇也)

:)希望這有助於!

相關問題