1

ヘルプ!findViewByIdnullの戻り値の問題が発生しています。私はたくさんの調査を行い、すでにプロジェクトをクリアし、setContentViewを先に進めましたが、それでも失敗します!カスタムビューの背景を青に設定すると、アプリの実行時にビューの色が期待どおりに青になるため、xmlレイアウトは正常に機能します。しかし、私はfindViewByIdを使用できません

レイアウトxml:

<?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>

カスタムビュークラス、その親クラスはすでにandroidのビュークラスを拡張しています:

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

}

}

アクティビティクラス

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);

}
}
4

1 に答える 1

2

ここで間違ったスーパーコンストラクターを呼び出しています:

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

する必要があります:

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

また、アクティビティの貼り付けられたコードにRのインポートが表示されないので、誤ってcom.android.Rではなく、プロジェクトのRファイルをインポートしてください。

于 2012-05-30T08:14:50.763 に答える