这是我的代码:
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文件:(note_view xml)
<?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"
android:orientation="vertical" >
<TextView
android:id="@+id/view_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="vertical"
android:textIsSelectable="true"
android:textSize="@dimen/font_medium" />
</LinearLayout>
这些是我在logcat中收到的错误消息。我已经尝试了很多次但仍然得到相同的错误。首先,我打印了笔记的索引和内容。这些是正确的。但是当我使用setText()将内容放入TextView小部件时,就会出现错误。
02-25 06:25:48.132: I/ActivityManager(289): START u0 {act=com.yuyizhao.NoteView dat=content://com.yuyizhao.notepad_yuyizhao.provider/notes/1 cmp=com.yuyizhao.notepad_yuyizhao/.NoteView} from pid 2243
02-25 06:25:48.482: D/dalvikvm(289): GC_FOR_ALLOC freed 1176K, 31% free 12125K/17420K, paused 92ms, total 104ms
02-25 06:25:48.522: I/Choreographer(2243): Skipped 30 frames! The application may be doing too much work on its main thread.
02-25 06:25:48.803: I/System.out(2243): 0
02-25 06:25:48.803: I/System.out(2243): 1
02-25 06:25:48.803: I/System.out(2243): wqdwqdqwdqwd
02-25 06:25:48.813: I/System.out(2243): wdqwdqwdqwdqwdwqdwddwq
02-25 06:25:48.823: D/AndroidRuntime(2243): Shutting down VM
02-25 06:25:48.823: W/dalvikvm(2243): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
02-25 06:25:48.863: E/AndroidRuntime(2243): FATAL EXCEPTION: main
02-25 06:25:48.863: E/AndroidRuntime(2243): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yuyizhao.notepad_yuyizhao/com.yuyizhao.notepad_yuyizhao.NoteView}: java.lang.NullPointerException
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.os.Looper.loop(Looper.java:137)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-25 06:25:48.863: E/AndroidRuntime(2243): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 06:25:48.863: E/AndroidRuntime(2243): at java.lang.reflect.Method.invoke(Method.java:511)
02-25 06:25:48.863: E/AndroidRuntime(2243): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-25 06:25:48.863: E/AndroidRuntime(2243): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-25 06:25:48.863: E/AndroidRuntime(2243): at dalvik.system.NativeStart.main(Native Method)
02-25 06:25:48.863: E/AndroidRuntime(2243): Caused by: java.lang.NullPointerException
02-25 06:25:48.863: E/AndroidRuntime(2243): at com.yuyizhao.notepad_yuyizhao.NoteView.onCreate(NoteView.java:53)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.app.Activity.performCreate(Activity.java:5104)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-25 06:25:48.863: E/AndroidRuntime(2243): ... 11 more
02-25 06:25:48.913: W/ActivityManager(289): Force finishing activity com.yuyizhao.notepad_yuyizhao/.NoteView
02-25 06:25:48.932: W/ActivityManager(289): Force finishing activity com.yuyizhao.notepad_yuyizhao/.Notepad
02-25 06:25:49.252: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
02-25 06:25:49.442: W/ActivityManager(289): Activity pause timeout for ActivityRecord{40eed718 u0 com.yuyizhao.notepad_yuyizhao/.NoteView}
02-25 06:25:49.823: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
02-25 06:25:49.833: W/EGL_emulation(403): eglSurfaceAttrib not implemented
-25 06:25:48.132: I/ActivityManager(289): START u0 {act=com.yuyizhao.NoteView dat=content://com.yuyizhao.notepad_yuyizhao.provider/notes/1 cmp=com.yuyizhao.notepad_yuyizhao/.NoteView} from pid 2243
02-25 06:25:48.482: D/dalvikvm(289): GC_FOR_ALLOC freed 1176K, 31% free 12125K/17420K, paused 92ms, total 104ms
02-25 06:25:48.522: I/Choreographer(2243): Skipped 30 frames! The application may be doing too much work on its main thread.
02-25 06:25:48.803: I/System.out(2243): 0
02-25 06:25:48.803: I/System.out(2243): 1
02-25 06:25:48.803: I/System.out(2243): wqdwqdqwdqwd
02-25 06:25:48.813: I/System.out(2243): wdqwdqwdqwdqwdwqdwddwq
02-25 06:25:48.823: D/AndroidRuntime(2243): Shutting down VM
02-25 06:25:48.823: W/dalvikvm(2243): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
02-25 06:25:48.863: E/AndroidRuntime(2243): FATAL EXCEPTION: main
02-25 06:25:48.863: E/AndroidRuntime(2243): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yuyizhao.notepad_yuyizhao/com.yuyizhao.notepad_yuyizhao.NoteView}: java.lang.NullPointerException
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.os.Looper.loop(Looper.java:137)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-25 06:25:48.863: E/AndroidRuntime(2243): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 06:25:48.863: E/AndroidRuntime(2243): at java.lang.reflect.Method.invoke(Method.java:511)
02-25 06:25:48.863: E/AndroidRuntime(2243): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-25 06:25:48.863: E/AndroidRuntime(2243): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-25 06:25:48.863: E/AndroidRuntime(2243): at dalvik.system.NativeStart.main(Native Method)
02-25 06:25:48.863: E/AndroidRuntime(2243): Caused by: java.lang.NullPointerException
02-25 06:25:48.863: E/AndroidRuntime(2243): at com.yuyizhao.notepad_yuyizhao.NoteView.onCreate(NoteView.java:53)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.app.Activity.performCreate(Activity.java:5104)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-25 06:25:48.863: E/AndroidRuntime(2243): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-25 06:25:48.863: E/AndroidRuntime(2243): ... 11 more
02-25 06:25:48.913: W/ActivityManager(289): Force finishing activity com.yuyizhao.notepad_yuyizhao/.NoteView
02-25 06:25:48.932: W/ActivityManager(289): Force finishing activity com.yuyizhao.notepad_yuyizhao/.Notepad
02-25 06:25:49.252: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
02-25 06:25:49.442: W/ActivityManager(289): Activity pause timeout for ActivityRecord{40eed718 u0 com.yuyizhao.notepad_yuyizhao/.NoteView}
02-25 06:25:49.823: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
02-25 06:25:49.833: W/EGL_emulation(403): eglSurfaceAttrib not implemented
使用setText()
方法时,有没有人知道我为什么会出现此错误?
答案 0 :(得分:0)
没有足够的堆栈跟踪或错误报告所以只是假设(Post full statcktrace)
许多程序失败的可能性,mTextView
NULL 。
或者可能是String content
NULL 。
所以在做mTextView.setText(content);
之前,请检查
if(content != null)
{
mTextView.setText(content);
}
更新:您的TextView mTextView
为NULL
,因为在xml中,ID名称为 view_body 而不是 text_body < / p>
从
改变mTextView = (TextView) findViewById(R.id.text_body);
到
mTextView = (TextView) findViewById(R.id.view_body);
答案 1 :(得分:0)
TextView
可能是null
。您的文字视图是否在R.layout.note_view
。?