Java 使用setText方法时获取错误
这是我的密码:Java 使用setText方法时获取错误,java,android,macos,macros,android-logcat,Java,Android,Macos,Macros,Android Logcat,这是我的密码: public class NoteView extends FragmentActivity implements CustomDialogFragment.CustomDialogListener { private Uri mUri; private String[] projection = { NotepadContract.Notes.COLUMN_NAME_NOTE, NotepadContract.Notes.COLUMN_NAME_TITLE };
public class NoteView extends FragmentActivity implements CustomDialogFragment.CustomDialogListener {
private Uri mUri;
private String[] projection = { NotepadContract.Notes.COLUMN_NAME_NOTE,
NotepadContract.Notes.COLUMN_NAME_TITLE };
private TextView mTextView;
private String content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note_view);
mTextView = (TextView) findViewById(R.id.text_body);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
mUri = intent.getData();
// String action=intent.getAction();
// if ("com.yuyizhao.NoteView".equals(action)) System.out.println(mUri);
Cursor mCursor = getContentResolver().query(mUri, projection, null,
null, null);
int index_note = mCursor
.getColumnIndexOrThrow(NotepadContract.Notes.COLUMN_NAME_NOTE);
System.out.println(index_note);
int index_title = mCursor
.getColumnIndexOrThrow(NotepadContract.Notes.COLUMN_NAME_TITLE);
System.out.println(index_title);
if (mCursor != null) {
while (mCursor.moveToNext()) {
content = mCursor.getString(index_note);
setTitle(mCursor.getString(index_title));
System.out.println(mCursor.getString(index_title));
System.out.println(mCursor.getString(index_note));
}
}
mTextView.setText(content);
}
布局xml文件:(注意\u查看xml)
有人知道为什么我在使用
setText()
方法时出现这个错误吗?没有足够的stacktrace或错误报告,所以只需要假设(完成stacktrace后)
许多程序失败的可能性,或者mTextView
为NULL
或者可能是您的字符串内容为NULL
所以在执行mTextView.setText(content)之前代码>只需检查
if(content != null)
{
mTextView.setText(content);
}
更新:您的TextView mTextView
为NULL
,因为在xml中id名称为view\u body而不是text\u body
从
mTextView = (TextView) findViewById(R.id.text_body);
到
可能是TextView
为null
。你的文本视图是否在R.layout中。注意\u view
?请显示完整的堆栈跟踪。你可以发布你的xml并完成你收到的logcat错误…兄弟请检查更新。谢谢兄弟。我检查了内容是否正确,因为我得到了打印件。还不明白为什么会发生这种事,他妈的对!非常感谢兄弟!这是我愚蠢的错误!再次感谢!我问你文本视图是否在那个布局中。
if(content != null)
{
mTextView.setText(content);
}
mTextView = (TextView) findViewById(R.id.text_body);
mTextView = (TextView) findViewById(R.id.view_body);