2015-06-21 67 views
0

我嘗試瞭解其他人的問題的解決方案,但沒有奏效。 我收到以下錯誤BaseAdapter類LayoutInflater:BaseAdapter類中的Layoutinflater空指針異常getView()

06-21 17:24:38.808: E/AndroidRuntime(23959): FATAL EXCEPTION: main 
06-21 17:24:38.808: E/AndroidRuntime(23959): Process: org.nick.yarntrading, PID: 23959 
06-21 17:24:38.808: E/AndroidRuntime(23959): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nick.yarntrading/org.nick.yarntrading.MainActivity}: java.lang.NullPointerException 
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.app.ActivityThread.access$800(ActivityThread.java:151) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.os.Handler.dispatchMessage(Handler.java:110) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.os.Looper.loop(Looper.java:193) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.app.ActivityThread.main(ActivityThread.java:5292) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at java.lang.reflect.Method.invokeNative(Native Method) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at java.lang.reflect.Method.invoke(Method.java:515) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at dalvik.system.NativeStart.main(Native Method) 
06-21 17:24:38.808: E/AndroidRuntime(23959): Caused by: java.lang.NullPointerException 
06-21 17:24:38.808: E/AndroidRuntime(23959): at org.nick.card_view.DataListAdapter.<init>(DataListAdapter.java:34) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at org.nick.yarntrading.Home.onCreateView(Home.java:36) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.app.Activity.performStart(Activity.java:5274) 
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311) 
06-21 17:24:38.808: E/AndroidRuntime(23959): ... 11 more 

這裏是延伸BaseAdapter的DataAdapter類:

public class DataListAdapter extends BaseAdapter { 

App app; 
ArrayList<HashMap<String,String>> listdata = null; 
FragmentManager fm; 
LayoutInflater inflater = null; 
LinearLayout lnlyt; 


//ProductList prodctlst; 
public DataListAdapter(App a, FragmentManager f) 
{ 
    this.app =a; 
    this.fm = f; 
    **inflater = (LayoutInflater) app.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);** 
    listdata = new ProductList(this.app).returnList(); 
} 
@Override 
public int getCount() { 

    return listdata.size(); 
} 

@Override 
public Object getItem(int position) { 
    return listdata.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return 0; 
} 

public class ViewHolder{ 
    TextView prod_name,prod_price,prod_descrp; 

} 
@Override 
public View getView(int position, View v, ViewGroup parent) { 
    final ViewHolder h; 
    if(v==null) 
    { 

     v = inflater.inflate(R.layout.card_view,null); 
     h = new ViewHolder(); 
     h.prod_name =(TextView) lnlyt.findViewById(R.id.prod_name); 
     h.prod_price = (TextView)lnlyt.findViewById(R.id.prod_price); 
     h.prod_descrp = (TextView) lnlyt.findViewById(R.id.prod_description); 
     v.setTag(h); 
    }else 
     h = (ViewHolder) v.getTag(); 
     h.prod_name.setText(listdata.get(position).get("prodctName")); 
     h.prod_price.setText(listdata.get(position).get("prodctPrice")); 
     h.prod_descrp.setText(listdata.get(position).get("prodctDescription")); 

    return v; 
} 

} 

下面是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"> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:layout_marginLeft="@dimen/feed_item_margin" 
    android:layout_marginRight="@dimen/feed_item_margin" 
    android:layout_marginTop="@dimen/feed_item_margin" 
    android:background="@drawable/bg_parent_rounded_corner" 
    android:orientation="vertical" 
    android:paddingTop="@dimen/feed_item_padding_top_bottom" > 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:paddingLeft="@dimen/feed_item_padding_left_right" 
      android:paddingRight="@dimen/feed_item_padding_left_right" > 
       <RelativeLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:paddingLeft="@dimen/feed_item_profile_info_padd" > 
        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/prod_name" 
         android:textStyle="bold" 
         android:text="Mobile" 
         android:textSize="20sp"/> 

        <TextView 
         android:id="@+id/prod_price" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerVertical="true" 
         android:layout_marginStart="140dp" 
         android:layout_toEndOf="@+id/prod_name" 
         android:text="15.2641" 
         android:textSize="20sp" /> 

       </RelativeLayout> 

      </LinearLayout> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="15dp" 
     android:layout_marginTop="8dp" 
     > 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/prod_description" 
      android:textColor="@android:color/black" 
      android:textSize="18sp" 
      android:text="Description" 
      /> 

    </LinearLayout> 
    </LinearLayout> 

這裏是家庭類的代碼:

package org.nick.yarntrading; 
import android.annotation.SuppressLint; 
import android.os.Bundle; 
import android.support.v4.app.ListFragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ListView; 
import android.widget.RelativeLayout; 

import org.nick.card_view.DataListAdapter; 


public class Home extends ListFragment { 
App a; 
ListView card_list; 
DataListAdapter adapter; 
RelativeLayout prodctView; 
public void home(App a) 
{ 
this.a = a; 
} 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

} 
@SuppressLint("InflateParams") @Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    prodctView = (RelativeLayout)inflater.inflate(R.layout.home_listview,null); 
     card_list = (ListView)prodctView.findViewById(android.R.id.list); 
    adapter = new DataListAdapter(a,getActivity().getSupportFragmentManager(),getActivity()); 
    card_list.setAdapter(adapter); 
    return prodctView; 
} 

}

而App類擴展了應用程序。 我嘗試了在stackoverflow提供的其他解決方案,但它沒有奏效。 這裏我想要做的是登錄到應用程序後顯示列表\視圖。

任何想法表示讚賞。 在此先感謝。

+0

那裏你叫你'Adapter'背景下的解決方案? –

+0

我曾經從擴展listFragment的home類調用適配器。 –

+1

那麼爲什麼不使用'getActivity()'作爲'Context'並在'Adapter'上傳遞' –

回答

2

惠那裏,

v = inflater.inflate(R.layout.card_view,null); 
    h = new ViewHolder(); 
    h.prod_name =(TextView) *lnlyt*.findViewById(R.id.prod_name); 
    v.setTag(h); 
}else 
    h = (ViewHolder) v.getTag(); 
    h.prod_name.setText(listdata.get(position).get("prodctName")); 

    return v; 
} 

您通過引用您的意見LynLyt,你甚至還沒有初始化它,試試這個:

@Override 
public View getView(int position, View v, ViewGroup parent) { 
//final ViewHolder h;// you have just declared it , initialize it first 
final ViewHolder h = new ViewHolder(); 
if(v==null) 
{ 

    v = inflater.inflate(R.layout.card_view,null); 
    h = new ViewHolder(); 
    h.prod_name =(TextView) v.findViewById(R.id.prod_name); 
    h.prod_price = (TextView)v.findViewById(R.id.prod_price); 
    h.prod_descrp = (TextView) v.findViewById(R.id.prod_description); 
    v.setTag(h); 
}else 
    h = (ViewHolder) v.getTag(); 
    h.prod_name.setText(listdata.get(position).get("prodctName")); 
    h.prod_price.setText(listdata.get(position).get("prodctPrice")); 
    h.prod_descrp.setText(listdata.get(position).get("prodctDescription")); 

return v; 
} 

邏輯是這樣的,一旦你的充氣這樣的佈局,

v = inflater.inflate(R.layout.card_view,null);現在,您已經將視圖中的元素包含在您的佈局中,現在可以通過參考'v'查找視圖。

希望我幫助你。

+0

我按照你所說的做了更改,但沒有發生同樣的錯誤。 ... –

+0

第34行被標記爲**,並且主頁類也被張貼,請檢查一個讓我知道的...我的錯誤 –

+0

我已經做出了很小的回答改變,嘗試一下,並且點給我一行:DataAdapter 34併發布主頁活動代碼以及 –

0

這裏),其工作對我來說 我通過getActivity(到DataListAdapter,並用它作爲像

Context cont; 
//ProductList prodctlst; 
public DataListAdapter(App a, FragmentManager f,Context context) 

{ 
    this.app =a; 
    this.fm = f; 
    this.cont = context; 
    inflater = ((Activity) cont).getLayoutInflater(); 
    listdata = new ProductList(this.app).returnList(); 
}