Rudi
Rudi

Reputation: 21

What is the error in my code

Hi I have a layout in which there are 4 Button, the Button are placed in a separate LinearLayout. The LinearLayout have id's assigned to them. Now in my activity which holds this layout, I am creating LinearLayout variables and assigning id's to them.

Now when I try to run the code, the following error is displayed .![enter image description here][1]

10-19 12:36:43.899: E/AndroidRuntime(21774): FATAL EXCEPTION: main
10-19 12:36:43.899: E/AndroidRuntime(21774): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.code.accountmanager/com.code.accountmanager.AccountManagerActivity}: java.lang.NullPointerException

also

10-19 12:36:43.899: E/AndroidRuntime(21774): Caused by: java.lang.NullPointerException
10-19 12:36:43.899: E/AndroidRuntime(21774):    at android.app.Activity.findViewById(Activity.java:1825)
10-19 12:36:43.899: E/AndroidRuntime(21774):    at com.code.accountmanager.AccountManagerActivity.<init>(AccountManagerActivity.java:22)

Also the line in which error is displayed is as:

LinearLayout l4 = (LinearLayout) findViewById(R.id.anim_recent_transaction);
LinearLayout l3 = (LinearLayout) findViewById(R.id.anim_view_account);
LinearLayout l2 = (LinearLayout) findViewById(R.id.anim_add_transaction);
LinearLayout l1 = (LinearLayout) findViewById(R.id.anim_add_account);

here is the xml code

    <LinearLayout
        android:id="@+id/buttonHolder"
        android:layout_width="120dp"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/anim_add_account"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" >

            <ImageButton
                android:id="@+id/addAccount"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:background="@drawable/addaccount_new"
                android:gravity="center" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />

        <LinearLayout
            android:id="@+id/anim_add_transaction"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" >

            <ImageButton
                android:id="@+id/addTransaction"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:background="@drawable/addtransaction_new"
                android:gravity="center" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />

        <LinearLayout
            android:id="@+id/anim_view_account"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" >

            <ImageButton
                android:id="@+id/viewAccount"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:background="@drawable/viewaccounts_new"
                android:gravity="center" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />

        <LinearLayout
            android:id="@+id/anim_recent_transaction"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" >

            <ImageButton
                android:id="@+id/recentTransaction"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:background="@drawable/viewtransaction_new"
                android:gravity="center" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Please tell me what could I do to remove the error.

Upvotes: 0

Views: 103

Answers (2)

Dany Y
Dany Y

Reputation: 7031

You should add setContentView(R.layout.yourLayoutXML) before using findViewById. I think that might be your problem

Upvotes: 2

dharmendra
dharmendra

Reputation: 7881

It would be better to identify your error if u had give a full code of your layout xml files .

in my perspective best way to identify this kind of error is DEBUG your code .

here is the link from where you can get to know about best way to debug .

http://developer.android.com/tools/debugging/ddms.html

Good luck !!

Upvotes: 0

Related Questions