おはようの人々にカスタムウィジェットは、私がAndroidアプリに取り組んでいると私は、問題のビットに実行した、setRequestedViewSize
をNullPointerExceptionがスローされます。私はViewを拡張する新しいクラスを作成しました。私は適切なメソッド(コンストラクタ、onDraw、onMeasure)をオーバーライドし、アプリケーションレイアウトXML(main.xmlと呼ばれます)を通してViewをインスタンス化しています。
public class CustomViewTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* I *think* I need to setContentView before I actually can use any of the widgets on the form. */
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// my new widget
newWidget s = (newWidget)findViewById(R.id.testWidget);
s.setRequestedViewSize(300);
}
});
}
}
ここでの問題、s.setRequestedViewSize(300)は、NullPointerExceptionがスローということです。私のアプリのソースコード内
は、私は次のコードを持っています。前に誰かがこれにぶつかったり、助けてくれる人がいますか?[EDIT] main.XMLは次のようになります。それはnullを返す
が、XMLは次のようになります。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.testing.CustomViewTest02.newWidget
android:id="@+id/testWidget"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></Button>
</LinearLayout>
感謝します! サム
はい、それは*表示されます*この問題が解決されるまで、私はちょうどその色を別の色に塗りつぶしています。私がボタンを押したときにのみ、強制的に閉じることができます。 – Sam
'android:id ='の代わりに 'id ='だけ試しましたか?それは私があなたが持っているものとは違って見える次のリンクの例が何であるかを示しています:http://developer.android.com/guide/topics/ui/custom-components.html – Zach
"id = "私はXMLエラーを取得します。 – Sam