Android 为什么(ListView)findViewById返回null?
救命啊!在过去的几个月里,我真的开始欣赏堆栈溢出和它的贡献者。我有很多问题,我在这里找到了答案。。。但是这个我似乎在任何地方都找不到。。。我是Java和Android的不速之客,几天来我一直在努力解决这个问题。出于某种原因,我有一个名为fileList的ListView对象,它返回null。。。一切编译都很好,但当我尝试使用fileList时,我得到了一个NullPointerException。。。我已经能够将它隔离到它的声明中:Android 为什么(ListView)findViewById返回null?,android,listview,Android,Listview,救命啊!在过去的几个月里,我真的开始欣赏堆栈溢出和它的贡献者。我有很多问题,我在这里找到了答案。。。但是这个我似乎在任何地方都找不到。。。我是Java和Android的不速之客,几天来我一直在努力解决这个问题。出于某种原因,我有一个名为fileList的ListView对象,它返回null。。。一切编译都很好,但当我尝试使用fileList时,我得到了一个NullPointerException。。。我已经能够将它隔离到它的声明中: ListView fileList = (ListView)f
ListView fileList = (ListView)findViewById(R.id.open_ListView);
但我一辈子都不明白这句话到底是怎么回事!我在下面包含了很多代码,理论上它应该有任何和所有可能以任何方式与此错误相关的代码
求求你,如果你能帮上忙,我将不胜感激!谢谢
这是代码中有问题的部分。它只是开关块的OPEN_对话框部分,所有其他开关都可以很好地显示它们的newDialog。我用星星标出了冒犯的界线
@Override
protected Dialog onCreateDialog(int id)
{
Dialog newDialog = new Dialog(Minervalia.this);
switch(id)
{
case DIALOG_FILE_NEW:
newDialog.setContentView(R.layout.new_file);
newDialog.setTitle("New File");
newDialog.setCancelable(true);
Button okBtn = (Button) newDialog.findViewById(R.id.ok_btn);
Button cancelBtn = (Button) newDialog.findViewById(R.id.cancel_btn);
final EditText widthEt = (EditText) newDialog.findViewById(R.id.width_edit);
final EditText heightEt = (EditText) newDialog.findViewById(R.id.height_edit);
okBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
file_width = Integer.parseInt(widthEt.getText().toString());
file_height = Integer.parseInt(heightEt.getText().toString());
onCreate(null);
dismissDialog(DIALOG_FILE_NEW);
}
});
cancelBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
dismissDialog(DIALOG_FILE_NEW);
}
});
return newDialog;
case DIALOG_OPEN:
newDialog.setContentView(R.layout.open_file);
newDialog.setTitle("Open File");
newDialog.setCancelable(true);
// ********** This next line returns null! Why?
ListView fileList = (ListView)findViewById(R.id.open_ListView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, loadFileList());
fileList.setAdapter(adapter);
fileList.setTextFilterEnabled(true);
fileList.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
return newDialog;
case DIALOG_SAVE:
newDialog.setContentView(R.layout.save_file);
newDialog.setTitle("Save File");
newDialog.setCancelable(true);
//--==[[ Define the important TextViews for our Save Dialog ]]==--\\
TextView pathTxt = (TextView) newDialog.findViewById(R.id.save_path_info);
EditText fnTxt = (EditText) newDialog.findViewById(R.id.save_filename_edit);
//--==[[ Define the Radio Buttons for our Save Dialog ]]==--\\
RadioButton JPEGoption = (RadioButton) newDialog.findViewById(R.id.save_JPEGoption);
RadioButton PNGoption = (RadioButton) newDialog.findViewById(R.id.save_PNGoption);
file_type = TYPE_JPEG; // Set this as the default option
JPEGoption.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
file_type = TYPE_JPEG;
}
});
PNGoption.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
file_type = TYPE_PNG;
}
});
Button save_okBtn = (Button) newDialog.findViewById(R.id.save_ok_btn);
Button save_cancelBtn = (Button) newDialog.findViewById(R.id.save_cancel_btn);
path = pathTxt.getText().toString();
fnTxt.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
filename = s.toString();
}
});
Toast.makeText(this, path, Toast.LENGTH_SHORT).show();
Toast.makeText(this, filename, Toast.LENGTH_SHORT).show();
save_okBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
String fullName = path + filename;
Bitmap.CompressFormat compForm = Bitmap.CompressFormat.JPEG; // make sure our format is initialized
if(file_type == TYPE_JPEG)
{
fullName = fullName + ".jpg";
compForm = Bitmap.CompressFormat.JPEG;
}
if(file_type == TYPE_PNG)
{
fullName = fullName + ".png";
compForm = Bitmap.CompressFormat.PNG;
}
File thisImage = new File(fullName);
FileOutputStream out = new FileOutputStream(thisImage);
mBitmap.compress(compForm, 90, out);
new SingleMediaScanner(mContext, thisImage);
out.flush();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
dismissDialog(DIALOG_SAVE);
}
});
save_cancelBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
dismissDialog(DIALOG_SAVE);
}
});
return newDialog;
}
return null;
}
private String[] loadFileList()
{
String[] mFileList = new String[0]; // generate empty array to avoid NullPointerException
try
{
filePath.canWrite();
}
catch(SecurityException e)
{
// Why isn't TAG recognized?...
// Log.e(TAG, "unable to write on the sd card " + e.toString());
}
if(filePath.exists())
{
FilenameFilter filter = new FilenameFilter()
{
public boolean accept(File dir, String filename)
{
File sel = new File(dir, filename);
return filename.contains(".jpg") || filename.contains(".png") || sel.isDirectory();
}
};
mFileList = filePath.list(filter);
}
else
{
mFileList = new String[0];
}
return mFileList;
}
@覆盖
受保护的对话框onCreateDialog(int id)
{
Dialog newDialog=新对话框(Minervalia.this);
开关(id)
{
案例对话框\u文件\u新建:
newDialog.setContentView(R.layout.new_文件);
setTitle(“新文件”);
newDialog.setCancelable(true);
按钮okBtn=(按钮)newDialog.findViewById(R.id.ok\u btn);
按钮cancelBtn=(按钮)newDialog.findViewById(R.id.cancel\u btn);
最终编辑文本宽度集=(编辑文本)newDialog.findviewbyd(R.id.width\u edit);
最终编辑文本高度=(编辑文本)newDialog.findViewById(R.id.height\u edit);
setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
file_width=Integer.parseInt(widthEt.getText().toString());
file_height=Integer.parseInt(heightEt.getText().toString());
onCreate(空);
dismissDialog(对话框\文件\新建);
}
});
cancelBtn.setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
dismissDialog(对话框\文件\新建);
}
});
返回newDialog;
案例对话框\u打开:
newDialog.setContentView(R.layout.open_文件);
setTitle(“打开文件”);
newDialog.setCancelable(true);
//**********下一行返回null!为什么?
ListView文件列表=(ListView)findViewById(R.id.open\u ListView);
ArrayAdapter=新的ArrayAdapter(这个,R.layout.list_项,loadFileList());
setAdapter(适配器);
fileList.setTextFilterEnabled(true);
setOnItemClickListener(新的OnItemClickListener()
{
public void onItemClick(AdapterView父对象、视图、整型位置、长id)
{
//单击后,显示带有文本视图文本的祝酒词
Toast.makeText(getApplicationContext(),((TextView)视图).getText(),Toast.LENGTH_SHORT.show();
}
});
返回newDialog;
案例对话框\u保存:
newDialog.setContentView(R.layout.save_文件);
setTitle(“保存文件”);
newDialog.setCancelable(true);
//--==[[为我们的保存对话框定义重要的文本视图]]==-\\
TextView pathTxt=(TextView)newDialog.findViewById(R.id.save\u path\u info);
EditText fnTxt=(EditText)newDialog.findViewById(R.id.save\u filename\u edit);
//--==[[定义保存对话框的单选按钮]]==--\\
RadioButton JPEGoption=(RadioButton)newDialog.findViewById(R.id.save\u JPEGoption);
RadioButton PNGoption=(RadioButton)newDialog.findViewById(R.id.save\u PNGoption);
file\u type=type\u JPEG;//将此设置为默认选项
JPEGoption.setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
文件类型=类型JPEG;
}
});
PNGoption.setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
文件类型=类型PNG;
}
});
按钮save\u okBtn=(按钮)newDialog.findviewbyd(R.id.save\u ok\u btn);
按钮save\u cancelBtn=(按钮)newDialog.findviewbyd(R.id.save\u cancel\u btn);
path=pathTxt.getText().toString();
addTextChangedListener(新的TextWatcher()
{
公共无效后文本已更改(可编辑)
{
}
更改前文本之前的公共void(字符序列s、int start、int count、int after)
{
}
public void onTextChanged(字符序列、int start、int before、int count)
{
filename=s.toString();
}
});
Toast.makeText(this,path,Toast.LENGTH_SHORT).show();
Toast.makeText(this,filename,Toast.LENGTH_SHORT).show();
保存\u okBtn.setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
尝试
{
字符串fullName=路径+文件名;
Bitmap.CompressFormat compForm=Bitmap.CompressFormat.JPEG;//确保我们的格式已初始化
如果(文件类型==类型JPEG)
{
全名=全名+“.jpg”;
compForm=Bitmap.CompressFormat.JPEG;
}
如果(文件类型==类型PNG)
{
全名=全名+“.png”;
compForm=Bitmap.CompressFormat.PNG;
}
文件thisImage=新文件(全名);
文件输出流输出
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/open_ListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
<LinearLayout
android:id="@+id/open_ButtonLinearLayout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:id="@+id/open_ok_btn"
android:text="Open"
android:layout_width="150dp"
android:layout_height="wrap_content">
</Button>
<Button
android:id="@+id/open_cancel_btn"
android:text="Cancel"
android:layout_width="150dp"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
ListView fileList = (ListView)newDialog.findViewById(R.id.open_ListView);