2014-01-17 48 views
0

我在speed = (TextView) findViewById(R.id.speed);中發現錯誤「速度無法解析或者它不是字段」Android中的findViewById錯誤

我的佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".SpeedometerActivity" > 

    <Button 
     android:id="@+id/download" 
     android:layout_width="150dp" 
     android:layout_height="70dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_marginBottom="13dp" 
     android:layout_marginLeft="300dp" 
     android:text="Begin Test" /> 

    <TextView 
     android:id="@+id/speed" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/meter" 
     android:layout_marginLeft="191dp" 
     android:layout_marginTop="55dp" 
     android:layout_toRightOf="@+id/meter" 
     android:text="TextView" 
     android:textSize="20dp" 
     android:textColor="#000000"/> 

</RelativeLayout> 

MainActivity

public class MainActivity extends Activity { 

    TextView speed; 
    Button download; 

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

     download = (Button)findViewById(R.id.download); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     h = new Handler(); 
     new speedTask().execute(); 
     speed = (TextView) findViewById(R.id.speed); 
    } 
} 

請人幫我解決這個問題。

+1

將類型添加到您的速度按鈕'(Button)speed =(Button)findViewById(R.id.speed);'順便說一句,您不能將Textview投射到按鈕。 – Arshak92

+1

速度是textview如何更改爲按鈕 – appukrb

+0

請粘貼您的完整MainActivity.Java文件。 –

回答

4
"speed cannot be resolved or it is not a field" 

檢查資源文件中是否有錯誤。按照黑帶的建議。如果您在資源文件中有錯誤,則不會生成R.java。修理它。還要檢查你是否有import android.R;如果是的話刪除它。

而且你有

<TextView 

    android:id="@+id/speed" 

它的一個TextView

它投射到按鈕

speed = (Button)findViewById(R.id.speed); 

應該

TextView speed =(TextView) findViewById(R.id.speed); 
+0

這將是一個不同的錯誤。 –

+0

@拉康丹丹我不同意。 OP正在抱怨編譯時錯誤。您正在討論運行時錯誤 – Blackbelt

+0

@blackbelt同意。編輯我的文章 – Raghunandan

2

你應該檢查R CLA你導入的SS是你的項目之一,而不是Android的。同樣,你正在投射到錯誤的對象,正如@Raghunandan所述正確的說明

+2

+1用於回答OP正面臨的實際問題。 –

+1

+1也是我在你面前回答的。我查看了代碼,但錯過了第一條語句,直到@鄒鄒指出我不會重新閱讀這篇文章。 – Raghunandan

1

在這裏,您將類型爲TextView的類型轉換爲Button。所以請改變你的代碼如下。

TextView speed = (Button)findViewById(R.id.speed); 

注:在這裏,你是不是能夠導入項目R檔,首先請檢查您的XML文件,並嘗試首先解決XML錯誤。

如果您已經導入,請從您的活動中刪除import android.R;

轉到MainActivity.Java文件並按CTRL + SHIFT + O。它會自動導入所有必需的軟件包。

+0

我已經按照你的說法做了,但它仍然顯示相同的錯誤 – user3153083

+0

因爲你說的我已經懂了,並且在XML中沒有錯誤,但顯示相同的錯誤 – user3153083

0

它創造了一個重​​復這樣給如下

Button speed1; 
speed1=(Button)findViewById(R.id.speed); 

很有效!