2013-09-23 82 views
0

我突然注意到,當我試圖訪問一個按鈕的資源時,我的項目開始拋出錯誤。它強調了R.id.button。我不明白爲什麼。我甚至刪除了我創建的最後一個xml,但問題仍然存在。R.id.button上的Android項目顯示錯誤

這是我的xml文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/layoutborder" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/chat" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:text="@string/stepone" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="@color/wine" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:src="@drawable/ai" /> 

    <Button 
     android:id="@+id/drugdetails" 
     style="@style/smallButtonStyleBlackpearl" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="10dp" 
     android:text="@string/nextbut" /> 

</LinearLayout> 

我的Java代碼的例子

package com.example.rhemahealthcare; 

import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

import com.actionbarsherlock.app.SherlockActivity; 
import com.example.rhemahealthcare.R; 

public class SteponeActivity extends SherlockActivity{ 


    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.steponeactivity); 

     final Button button = (Button)findViewById(R.id.button1); 
     button.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v){ 
       // TODO Auto-generated method stub 
       Intent intent = new Intent(SteponeActivity.this,SteptwoActivity.class); 
       startActivity(intent); 
      } 

     }); 
    } 

} 
+1

請發佈您的代碼 – Wannabe

+2

您是否在任何佈局xml文件中都有'android:id =「@ + id/button」'? –

+1

可以請你發佈xml活動和你的Java類嗎? –

回答

0

由於@Aleks摹gussed是正確的評論,你不必與ID的任何按鈕button1在您的xml文件中。您提到過:

final Button button = (Button)findViewById(R.id.button1); 

使用適當的ID或將其放入佈局文件中。

+0

是的,這不是我不知道我的班級文件是如何改變他們的按鈕ID的。自動。這讓我很驚訝。非常感謝。 – user2754532

0

我已經找到了問題所在。我的按鈕id自動變爲button1,所以他們沒有參考我給他們的先前的id。非常感謝

1

我想你改變任何button1 id購買單擊右鍵單擊並選擇編輯ID。此選項會在所有佈局中更改所有具有該名稱的ID。