嗨,我有一个布局,其中有 4 个Button
,Button
分别放在一个单独的LinearLayout
. 有LinearLayout
id 分配给他们。现在在我的包含这个布局的活动中,我正在创建LinearLayout
变量并为它们分配 id。
现在,当我尝试运行代码时,会显示以下错误。![在此处输入图像描述][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
还
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)
此外,显示错误的行如下:
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);
这是xml代码
<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>
请告诉我我能做些什么来消除错误。