请看下面的代码
private class OpenFileEvent implements OnClickListener
{
@Override
public void onClick(View arg0) {
LinearLayout openFileDialogView = (LinearLayout)findViewById(R.id.open_file_dialog);
// TODO Auto-generated method stub
final Dialog openFileDialog = new Dialog(VoiceNotes.this);
openFileDialog.setTitle("Open File");
openFileDialog.setContentView(R.layout.open_dialog);
//First, list all the available Files
File folder = new File(Environment.getExternalStorageDirectory()+"/Voice/Notes/");
File file = new File(folder.getAbsolutePath());
File[] fileNameList = file.listFiles();
if(fileNameList != null && fileNameList.length>0)
{
for(int i=0;i<fileNameList.length;i++)
{
//Get the sub views first
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View openThisFileView = inflater.inflate(R.layout.open_dialog_file, null);
Button openThisFileButton = (Button)openThisFileView.findViewById(R.id.open_this_file_button);
Button appendThisFileButton = (Button)openThisFileView.findViewById(R.id.append_note_this_file);
TextView openThisFileNameTxt = (TextView)openThisFileView.findViewById(R.id.open_this_file_name);
//Set the Text
openThisFileNameTxt.setText(fileNameList[i].getName());
//Set the Listeners
//Add the View
openFileDialogView.addView(openThisFileView);
}
}
//Show the Dialog
openFileDialog.show();
}
}
一旦运行此代码,我就会收到以下错误消息,即 NullPointerException
11-18 16:24:20.832: E/AndroidRuntime(1019): FATAL EXCEPTION: main
11-18 16:24:20.832: E/AndroidRuntime(1019): java.lang.NullPointerException
11-18 16:24:20.832: E/AndroidRuntime(1019): at com.x.xxx.VoiceNotes$OpenFileEvent.onClick(VoiceNotes.java:227)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.view.View.performClick(View.java:4204)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.view.View$PerformClick.run(View.java:17355)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.os.Handler.handleCallback(Handler.java:725)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.os.Handler.dispatchMessage(Handler.java:92)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.os.Looper.loop(Looper.java:137)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-18 16:24:20.832: E/AndroidRuntime(1019): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 16:24:20.832: E/AndroidRuntime(1019): at java.lang.reflect.Method.invoke(Method.java:511)
11-18 16:24:20.832: E/AndroidRuntime(1019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-18 16:24:20.832: E/AndroidRuntime(1019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-18 16:24:20.832: E/AndroidRuntime(1019): at dalvik.system.NativeStart.main(Native Method)
错误指向此处
openFileDialogView.addView(openThisFileView);
由于它表示 LinearLayout
为 null,因此这里是该布局所属的 XML 文件。
open_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/open_file_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
</LinearLayout>
这与充当 Dialog
的内容 View 的布局相同。
为什么我认为 LinearLayout
为 null,而不是类内膨胀的布局?因为下面的代码还生成了 NullPointerException
openFileDialogView.addView(new Button(Notes.this));
为什么我会收到此错误?
最佳答案
LinearLayout openFileDialogView = (LinearLayout)findViewById(R.id.open_file_dialog);
此处 findViewById()
尝试在 Activity 的 View 层次结构中查找指定 View (由 setContentView()
设置),但您仅使用该 id 来扩展布局稍后。findViewById()
返回 null
并尝试调用 null
上的方法会导致 NPE。
关于java - NullPointerException:LinearLayout 为 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20053465/