2011-09-05 93 views
-1

我不能找到什麼錯我的代碼我得到解決不了或不上findViewById的字段。不能得到解決或無法在現場findViewById

的問題行是:

 playSeekBar = (ProgressBar) findViewById(R.id.progressBar1); 
     buttonPlay = (Button) findViewById(R.id.buttonPlay); 
     buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay); 

我的XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content"></ProgressBar> 
<Button android:text="Button" android:id="@+id/buttonPlay" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
<Button android:text="Button" android:id="@+id/buttonStopPlay" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 

</LinearLayout> 

我的代碼是:

public class radioActivite extends Activity implements OnClickListener { 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.radio_xml); 




    initializeUIElements(); 

    initializeMediaPlayer(); 
} 

private void initializeUIElements() { 

     playSeekBar = (ProgressBar) findViewById(R.id.progressBar1); 

    playSeekBar.setMax(100); 
    playSeekBar.setVisibility(View.INVISIBLE); 

    buttonPlay = (Button) findViewById(R.id.buttonPlay); 
    buttonPlay.setOnClickListener(this); 

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

試圖清理代碼,但沒有幫助我。 感謝您的幫助!

+0

你可以使用從LogCat獲取的堆棧跟蹤來更新問題嗎? –

回答

6

嘗試刪除您的R.java以便重新生成。這通常是Eclipse在您的res /目錄中看不到您的更改。 R.java未重新生成的另一個原因是您的資源(佈局,樣式等)可能有問題

+0

我刪除了R.java並重新創建。奇怪我 –

+1

我只是試圖調試應用程序和錯誤消失。 –

+0

ahaha是啊這就是他說的 –

4

檢查您的導入,可能導入了錯誤的R文件。應該有

import <your-project-package-name>.R; 

行,而不是

import android.R; 

,迅速組織在Eclipse中使用進口組合Ctrl+Shift+O。希望這可以幫助。

+0

我的進口沒問題檢查一下 –

+0

@Vitaly Menchikovsky,我在哪裏可以檢查它?你應該檢查你的XML文件是否有錯誤,打開Eclipse的Problems視圖來查看你的項目出了什麼問題。 – Egor

+0

我有這個問題,謝謝葉戈爾! – Marky

0

這種情況在eclipse中有時會發生,清理項目幫助我始終如一。它在Project-> clean下。

+0

做到了這一點,但同樣的事情 –

0

奇怪我只是試圖調試應用程序和錯誤消失。

+1

它可能會觸發一個乾淨,或重新生成R功能,當你去調試它。 – CrackerJack9

5

嘗試刪除關鍵字android當您訪問XML屬性時將在R之前添加的詞。

import android.R; 
... 
setContentView(android.R.id.textView); 
.... 
findViewById(android.R.id.Button2); 

例如,在上述

import android.R.id.Button1; 

刪除Android和把你的包目錄路徑..

1
  1. 撈出進口android.R;

  2. 清潔工程

  3. 然後生成該項目再次

0

撈出進口android.R;

清理項目。

然後再次構建項目。

相關問題