1

他の質問を調べましたが、このシナリオに完全に一致するものは見つかりませんでした。XMLでRelativeLayoutを定義しました。このRelativeLayout全体を(個別に作成したクラスの)GraphViewオブジェクトの下に配置したいと思います。私は、RelativeLayoutをGraphViewの下に配置する最良の方法は、プログラムで定義しようとしたLinearLayout内にこれらの両方を貼り付けることであると考えました。

これは私がこれまでに持っているものです-それに何か問題がありますが、私はよくわかりません。

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class Grapher extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        LinearLayout ll = new LinearLayout(this); 
        GraphView gv = new GraphView(this); 
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainRelativeLayout);
        ll.addView(gv); 
        ll.addView(rl); 
        setContentView(ll); 

そして、xml...。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/mainRelativeLayout"
    android:orientation="vertical">
    <Button
        android:id="@+id/second"
        android:clickable="true"
        android:background="@drawable/button_chooser"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:text="@string/second" 
        android:textColor="@color/white"
        android:textSize="15sp"/>
    <Button
        android:id="@+id/alpha"
        android:clickable="true"
        android:background="@drawable/button_chooser"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:layout_below="@id/second"
        android:layout_alignParentLeft="true"
        android:text="@string/alpha"
        android:textColor="@color/white"
        android:textSize="15sp" />
    <Button
        android:id="@+id/mode"
        android:clickable="true"
        android:background="@drawable/button_chooser"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@id/second"
        android:text="@string/mode"
        android:textColor="@color/white"
        android:textSize="15sp" />
</RelativeLayout>

ご協力いただきありがとうございます。私はこれが私が見ている非常に単純なものであるという予感があります。

4

1 に答える 1

3

Activity.findViewByIdアクティビティにバインドされたビューを検索します (通常は を呼び出した結果としてsetLayout(R.layout.yourlayout);)。これを呼び出していないため、現在アクティブなビューがないため、呼び出すfindViewByIdと null が返されます。

あなたがやろうとしているように見えるのは、xmlからレイアウトを膨らませてから、プログラムでビューに追加することです。あなたがすべきことは次のようなものです:

Inflater inflater = LayoutInflater.from(context);
//this will inflate the mainRelativeLayout into the linear layout you called "ll"
inflater.inflate(R.id.mainRelativeLayout, ll);
于 2012-06-29T00:39:01.613 に答える