0
所以基本上,我做了一個測驗應用程序,並在應用程序的結尾我有一個方法,應該顯示用戶的進度。基本上我想爲用戶放入的每個不正確的答案添加textViews,但它告訴我他們都是空的。這裏是代碼:動態添加的textView裏面的片段給出空引用
public void progFinish() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_progress, null);
TextView title = (TextView) mView.findViewById(R.id.title_dialog);
int total = wrongChars.size();
LinearLayout linearLayout = (LinearLayout) mView.findViewById(R.id.prog_dialog);
for(Map.Entry<String, Integer> entry : wrongChars.entrySet()) {
Log.d("test", entry.getKey());
TextView wrongChar = new TextView(getActivity());
wrongChar.setText(entry.getKey() + " \t" + entry.getValue() + " ");
wrongChar.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(wrongChar);
}
builder.setView(view);
AlertDialog dialog = builder.create();
dialog.show();
}
我不確定這裏的問題。值得注意的是,有一個成員變量
protected View mView
它在onViewCreated中初始化。有沒有特別的原因,他們是空的?我會認爲這是因爲mView - 但我不確定。
這是返回錯誤的行:
linearLayout.addView(wrongChar);
Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
編輯:需要注意的是這是靜態佈局內TextView的充氣就好了(當用戶沒有得到任何錯誤都會顯示。 )所以我認爲,假設動態添加的textViews是手頭的問題是安全的。不知道爲什麼。