Ошибка нулевого исключения при получении LinearLayout - PullRequest
0 голосов
/ 26 февраля 2012

Вот мой код xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <LinearLayout
        android:id="@+id/HUD_Display"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/hud"
        android:contentDescription="HUDContent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/HUD_Text_Element1"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_marginTop="175dp"
            android:textSize="20dp"
            android:text="Hello World"
            android:tag="hud"
            android:textColor="@color/Black"
            android:typeface="serif" />

        <TextView
            android:id="@+id/HUD_Text_Element2"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_marginTop="2dp" 
            android:textSize="20dp"
            android:tag="hud"
            android:text="Hello World HOW"
            android:textColor="@color/Black"
            android:typeface="serif" />

         <TextView
            android:id="@+id/HUD_Text_Element3"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_marginTop="1dp" 
            android:textSize="20dp"
            android:tag="hud"
            android:text="Hello World HOW"
            android:textColor="@color/Black"
            android:typeface="serif" />

          <TextView
            android:id="@+id/HUD_Text_Element4"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_marginTop="1dp" 
            android:textSize="20dp"
            android:text="Hello World"
            android:textColor="@color/Black"
            android:typeface="serif" />
</LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/Btn01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="100dp"
                android:onClick="handleClick"
                android:textColor="@color/White"
                android:textSize="25dp"
                android:textStyle="bold"
                android:typeface="normal" />

            <Button
                android:id="@+id/Btn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="100dp"
                android:onClick="handleClick"
                android:textColor="@color/White"
                android:textSize="25dp"
                android:textStyle="bold"
                android:typeface="sans" /> 
        </LinearLayout>

Это SecondActivity Main2.layout, который раздувает main3, на который будут добавляться различные уровни.

setContentView(R.layout.main2);

    // ViewGroup where n number of view is going to added
    ViewGroup layout= (ViewGroup) findViewById(R.id.main3);

    // inflating the layout depending on the level 
    View view = View.inflate(this,FirstActivity.levels, null);

    Sample sam= new Sample(view.getContext(), level, layout);

Теперь я создал образец класса Object .. Вот образец класса

образец публичного класса {

Sample(Context context, View vs, ViewGroup layout) {

LinearLayout HUDDisplay=(LinearLayout) layout.findViewById(R.id.HUD_Display);

Log.d("Check", "msg" + HUDDisplay.getContentDescription().toString());


    int count =HUDDisplay.getChildCount();

    Log.d("TextView","Count" + count);      
    for(int i=0;i<count;++i)
    {
        Log.d("TextView","textview" + HUDDisplay.getChildAt(i).toString());
    }


}

Я получаю ОШИБКУ ИСКЛЮЧЕНИЯ NULL в точке HUDdisplay? Что в этом плохого? Я хочу установить текстовое значение как

Ответы [ 2 ]

0 голосов
/ 26 февраля 2012

проверьте, есть ли у вас объект линейного макета в этой строке.

ViewGroup layout= (ViewGroup) findViewById(R.id.main3);

это может быть NULL.

0 голосов
/ 26 февраля 2012

Попробуйте изменить Linear Layout на View, посмотрите, что получится

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...