1

这是我的代码:

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()方法时会出现这个错误?

4

2 回答 2

0

没有足够的堆栈跟踪或错误报告,所以只是假设(发布完整的 statcktrace)

程序失败的许多可能性,无论mTextViewNULL

或者可能是您的String contentis NULL

所以在做之前mTextView.setText(content);检查

if(content != null)
{
 mTextView.setText(content);
}

更新:TextView mTextViewNULL因为,在 xml 中,id 名称是view_body而不是text_body

从改变

mTextView = (TextView) findViewById(R.id.text_body);

mTextView = (TextView) findViewById(R.id.view_body);
于 2013-02-25T06:21:42.147 回答
0

可能TextViewnull。您的文本视图是否在R.layout.note_view.?

于 2013-02-25T06:36:23.917 回答