私はコンパスAndroidアプリをプログラミングしていますが、ユーザーがメニューに戻ることができるように設計された戻るボタンを使用して、メインXMLファイルにキャンバスを追加したいという問題に直面しています。
キャンバスコンパスをaddview()
main.xmlに追加しようとしましたが、それでもエラーは「mainLayout.addView(compassView);」のNULLPOINTEREXCEPTIONでした。MAIN.JAVAコードで
これが私のコードです
MAIN.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
compassView = new MyCompassView(this);
setContentView(compassView);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.compasslayout);
LayoutParams imageViewLayoutParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
compassView.setLayoutParams(imageViewLayoutParams);
mainLayout.addView(compassView);
MyCompassView.java
public class MyCompassView extends View {
private Paint paint;
private float position = 0;
public MyCompassView(Context context) {
super(context);
init();
}
private void init() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(25);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLACK);
paint.setStrokeMiter(position);
}
XMLファイル
<LinearLayout 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:orientation="vertical"
tools:context=".Compass" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:id="@+id/compasslayout">
<Button
android:id="@+id/buttona"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="@drawable/b_select" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingTop="10dp">
</LinearLayout>
</LinearLayout>
私を助けてください、私はすでにこれに一日立ち往生していて、このタスクを完了せずに続けることはできません