2014-01-17 4 views
0

speed = (TextView) findViewById(R.id.speed);이 (가) 입니다. "속도를 해결할 수 없거나 필드가 아닙니다."입니다.안드로이드의 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

속도는 텍스트보기입니다. 단추로 변경하는 방법 – 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" 

그 버튼에 캐스팅 텍스트 뷰

이 또한

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

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

다른 오류가 있습니다. –

+0

@Raghunandan 나는 동의하지 않는다. OP는 컴파일 타임 오류에 대해 불평하고있다. 런타임 오류 – Blackbelt

+0

@blackbelt 동의에 대해 이야기하고 있습니다. – Raghunandan

2

당신은 확인해야 할 수 있어야 R CIA 요원 경우 가져온 ss은 프로젝트 중 하나이며 안드로이드의 것이 아닙니다. 또한 @Raghunandan에서 올바르게 명시된 바와 같이 잘못된 개체로 캐스팅 중입니다.

+2

OP가 직면하고있는 실제 문제에 답하는 +1. –

+1

+1 또한 내게 바로 대답했다. 나는 코드를 보았지만 첫 문장을 놓쳤다. 그리고 @ZouZou가 나에게 내가 포스트를 다시 읽지 않을 것이라고 지적했다. – 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

내가 말했듯이 impoterd 또한 XML에 오류가 있지만 같은 오류를 보여주는 – user3153083

0

은 그것은 그것은 작동합니다

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

다음과 같이 중복 그렇게주고 만들었습니다!