好吧,所以我創建了一個包含幾個佈局和文字瀏覽的自定義視圖。我做了一個動態創建視圖的對象。繼承人,使視圖並設置TextViews代碼....向滾動視圖添加視圖時出現空指針異常
inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.run_info_layout, null);
TextView infoP, in;
infoP = (TextView) layout.findViewById(R.id.tvInfoParameter);
in = (TextView) layout.findViewById(R.id.tvInfo);
infoP.setText(ip);
in.setText(i);
在活動中,我有我有它創造了一些使用此代碼,這些對象....
runInfo.add(new RunInfoLayout("Final Time", run.getTime(), RunEnding.this));
runInfo.add(new RunInfoLayout("Distance", Float.toString(run.getDistance()), RunEnding.this));
runInfo.add(new RunInfoLayout("Started", run.getBTime(), RunEnding.this));
runInfo.add(new RunInfoLayout("Ended", run.getETime(), RunEnding.this));
runInfo.add(new RunInfoLayout("Number of laps", Integer.toString(numberOfLaps), RunEnding.this));
我有一個循環,通過runInfo去這是一個ArrayList,並添加意見,滾動型.....
for(int x = 0; x < runInfo.size(); x++){
llRunInfo.addView(runInfo.get(x).getView());
}
llRunInfo是的LinearLayout在滾動型。該getView()方法只是返回它在前面的代碼中所做的視圖。問題在於addView()行。那裏我得到一個nullPointerException。這裏的logcat的....
07-25 12:42:25.223: E/AndroidRuntime(31056): Caused by: java.lang.NullPointerException
07-25 12:42:25.223: E/AndroidRuntime(31056): at android.view.ViewGroup.addView(ViewGroup.java:3148)
07-25 12:42:25.223: E/AndroidRuntime(31056): at android.view.ViewGroup.addView(ViewGroup.java:3131)
07-25 12:42:25.223: E/AndroidRuntime(31056): at com.TBJsoft.runprogress.RunEnding.build(RunEnding.java:55)
你在哪裏初始化'llRunInfo'? – codeMagic
什麼是'RunInfoLayout',它的'getView()'方法做了什麼?它看起來像是返回null。 – Geobits
我在將對象添加到runInfo的代碼之前初始化它。它是我創建的一個類,它返回使用此代碼創建的視圖 – ThatBoiJo