2012-05-30 4 views
1

Help! findViewById null 반환 값 문제가 나타납니다. 나는 수 많은 연구를했고 이미 프로젝트를 정리했고, setContentView를 앞에 두었다. 그러나 여전히 실패한다! 사용자 정의보기의 배경을 파란색으로 설정하면보기가 응용 프로그램을 실행할 때 예상대로 파란색으로 표시되기 때문에 xml 레이아웃이 올바르게 작동합니다. 하지만 난 그냥 캔트 사용Android findViewById가 null로 설정됩니다. 이미 프로젝트 청소를 시도했습니다. setcontentview first

레이아웃 XML findViewById를 :

package shaotian.android.blackboard; 
import java.util.Observable; 
import java.util.Observer; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.Toast; 
public class BBView extends shaotian.android.blackboard.View { 


public BBView(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 
    TextView txt=new TextView(context); 
    this.setBackgroundColor(Color.BLUE); 

} 
public BBView(Context context, AttributeSet attr) 
{super(context); 
this.setBackgroundColor(Color.BLUE); 
} 

public BBView(Context context,AttributeSet attr,int sty) 
{ 
    super(context,attr,sty); 
    this.setBackgroundColor(Color.BLUE); 
} 


@Override 
protected void onDraw(Canvas canvas) { 
    // TODO Auto-generated method stub 
    super.onDraw(canvas); 
} 

public void update(Observable arg0, Object arg1) { 
    // TODO Auto-generated method stub 

} 

}

:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<shaotian.android.blackboard.BBView 
    android:id="@+id/bBView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

</FrameLayout> 

사용자 정의 뷰 클래스를 부모 클래스는 이미 안드로이드의 뷰 클래스 확장

활동 클래스 다음에 붙여 넣은 코드에서, 또한

public BBView(Context context, AttributeSet attr) 
{ 
    super(context, attr); 
    this.setBackgroundColor(Color.BLUE); 
} 

:

public BBView(Context context, AttributeSet attr) 
{ 
    super(context); 
    this.setBackgroundColor(Color.BLUE); 
} 

될해야 : 당신은 여기에 잘못된 슈퍼 생성자를 호출하는

package shaotian.android.blackboard; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 
public class BlackBoardActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    BBView view=(BBView)findViewById(R.id.bBView1); 

} 
} 
+0

게시하기이 클래스 shaotian.android.blackboard.View ... –

+0

레이아웃 파일이 실제로'layout.xml'이라면, 당신은 * setContentView 대신'setContentView (R.layout.layout);을 호출해야합니다. (R.layout.main);'. – Jens

+0

setContentView (R.layout.layout); – Khan

답변

2

활동 나는 R에 대한 가져 오기를 보지 못했지만, 프로젝트의 R 파일을 가져 왔는지 확인하십시오. 실수로 com.android.R이 아닙니다.

+0

아! 고마워! 그것은 작동! –

+0

awesome :) 기꺼이 도와 드리겠습니다 ... – marmor

+0

저는이 답변을 오랫동안 찾고 있었어요 .... 고맙습니다. –

관련 문제