0

当我膨胀布局时,我的代码中出现 NullPointerException。

这是我的java代码-

LayoutInflater layoutInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = layoutInflater.inflate(R.layout.grouped_text_view, this);
itemsContainer = (LinearLayout) convertView.findViewById(R.id.items_container);
itemsContainer.setVisibility(View.GONE);           //line 1

grouped_text_view.xml-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/GroupedTextViewContainer">

<LinearLayout
        android:id="@+id/items_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/item_background"
        android:orientation="vertical">
</LinearLayout>

</LinearLayout>

这些是日志-

08-02 13:44:05.870: W/System.err(500): java.lang.NullPointerException
08-02 13:44:05.880: W/System.err(500):  at  com.drindia.groupedtextviewcustom.GroupedTextView.initialize(GroupedTextView.java:67)
08-02 13:44:05.880: W/System.err(500):  at  com.drindia.groupedtextviewcustom.GroupedTextView.<init>(GroupedTextView.java:45)
08-02 13:44:05.880: W/System.err(500):  at  java.lang.reflect.Constructor.constructNative(Native Method)
08-02 13:44:05.880: W/System.err(500):  at  java.lang.reflect.Constructor.newInstance(Constructor.java:415)
08-02 13:44:05.880: W/System.err(500):  at  android.view.LayoutInflater.createView(LayoutInflater.java:505)
08-02 13:44:05.880: W/System.err(500):  at  android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
08-02 13:44:05.880: W/System.err(500):  at  android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
08-02 13:44:05.880: W/System.err(500):  at  android.view.LayoutInflater.inflate(LayoutInflater.java:408)
08-02 13:44:05.880: W/System.err(500):  at  android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-02 13:44:05.880: W/System.err(500):  at  android.view.LayoutInflater.inflate(LayoutInflater.java:276)
08-02 13:44:05.880: W/System.err(500):  at  com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
08-02 13:44:05.880: W/System.err(500):  at  android.app.Activity.setContentView(Activity.java:1657)
08-02 13:44:05.890: W/System.err(500):  at  com.devrepublic.kommaaropmetjeaanbieding.ui.InformationActivity.onCreate(InformationActivity.j ava:23)
4

1 回答 1

0

尝试这个 ..

context而不是直接使用useLayoutInflator class如下所示:

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View convertView = (LinearLayout) inflater.inflate(R.layout.grouped_text_view, null);
itemsContainer = (LinearLayout) convertView.findViewById(R.id.items_container);
itemsContainer.setVisibility(View.GONE);       
于 2013-08-01T10:55:49.777 回答