错误/AndroidRuntime(775): java.lang.RuntimeException: 无法启动活动 ComponentInfo{..}: java.lang.NullPointerException

发布于 2024-12-14 23:36:10 字数 5717 浏览 3 评论 0原文

这个问题可能看起来像是一个重复的问题。但这些查询并没有帮助我解决我的问题。

我想在用 SimpleCursorAdapter 填充的列表(婴儿名字)上实现“onClick”事件。当用户单击某个名称时,将会生成一个 toast,显示数据库中记录的“id”。

这是我的代码 -

public class CommonNames extends Activity {

TextView rowview;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.common_names);

    rowview = (TextView)findViewById(R.id.commonName);

    CommonNamesAdapter cnTable = new CommonNamesAdapter(this);
    ListView cnListView = (ListView)findViewById(R.id.common_name_layout);

    cnTable.open(getApplicationContext());
    Cursor c = cnTable.fetch_all_common_names_only();
    startManagingCursor(c);

    if(c!=null){
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.listview,
                c,
                new String[] {c.getColumnName(0), c.getColumnName(1)},

                new int[] {R.id.rowLayout,R.id.commonName});

                cnListView.setAdapter(adapter);

        }


    rowview.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            Context context = getApplicationContext();

            String id = context.getString(R.id.commonName);
            CharSequence text = "This id of this item is... "+id ;
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

        }


    });
    cnTable.close();
}

这是 listview.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowLayout"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
>
<TextView 
    android:id="@+id/commonName"
    android:layout_alignParentLeft="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16dp"
    android:paddingLeft="14dp"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
/>

</LinearLayout>

这是 common_names.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView 
    android:id = "@+id/common_name_layout"
    android:layout_width="fill_parent"
    android:layout_height= "wrap_content" >
</ListView>
</LinearLayout>

当我运行我的应用程序时,我收到运行时 NullPointerException。这是 LogCat。请帮我解决这个问题。

11-13 18:23:27.852: INFO/COMMON_NAMES_TABLE(775): Inserting record...
11-13 18:23:27.902: INFO/COMMON_NAMES_TABLE(775): OPening DataBase Connection....
11-13 18:23:28.002: DEBUG/AndroidRuntime(775): Shutting down VM
11-13 18:23:28.002: WARN/dalvikvm(775): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-13 18:23:28.012: ERROR/AndroidRuntime(775): FATAL EXCEPTION: main
11-13 18:23:28.012: ERROR/AndroidRuntime(775): java.lang.RuntimeException: Unable to start activity ComponentInfo{balu.android/balu.android.CommonNames}: java.lang.NullPointerException
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.os.Looper.loop(Looper.java:123)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at java.lang.reflect.Method.invokeNative(Native Method)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at java.lang.reflect.Method.invoke(Method.java:521)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at dalvik.system.NativeStart.main(Native Method)
11-13 18:23:28.012: ERROR/AndroidRuntime(775): Caused by: java.lang.NullPointerException
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at balu.android.CommonNames.onCreate(CommonNames.java:42)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     ... 11 more
11-13 18:23:28.032: WARN/ActivityManager(59):   Force finishing activity balu.android/.CommonNames
11-13 18:23:28.032: WARN/ActivityManager(59):   Force finishing activity balu.android/.Select
11-13 18:23:28.585: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{45025710 balu.android/.CommonNames}
11-13 18:23:31.192: INFO/Process(775): Sending signal. PID: 775 SIG: 9
11-13 18:23:31.212: INFO/ActivityManager(59): Process balu.android (pid 775) has died.
11-13 18:23:31.212: INFO/WindowManager(59): WIN DEATH: Window{45084738 balu.android/balu.android.BabyNamesAppActivity paused=false}

谢谢。

This question may look like a repeated one. But those queries hadn't helped me to fix my problem.

I would like to implement "onClick" event on the list (of baby names) which is populated with SimpleCursorAdapter. When ever the user clicks on a name, a toast will be generated showing the "id" of the record in the database.

Here is my code -

public class CommonNames extends Activity {

TextView rowview;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.common_names);

    rowview = (TextView)findViewById(R.id.commonName);

    CommonNamesAdapter cnTable = new CommonNamesAdapter(this);
    ListView cnListView = (ListView)findViewById(R.id.common_name_layout);

    cnTable.open(getApplicationContext());
    Cursor c = cnTable.fetch_all_common_names_only();
    startManagingCursor(c);

    if(c!=null){
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.listview,
                c,
                new String[] {c.getColumnName(0), c.getColumnName(1)},

                new int[] {R.id.rowLayout,R.id.commonName});

                cnListView.setAdapter(adapter);

        }


    rowview.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            Context context = getApplicationContext();

            String id = context.getString(R.id.commonName);
            CharSequence text = "This id of this item is... "+id ;
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

        }


    });
    cnTable.close();
}

Here is listview.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowLayout"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
>
<TextView 
    android:id="@+id/commonName"
    android:layout_alignParentLeft="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16dp"
    android:paddingLeft="14dp"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
/>

</LinearLayout>

Here is common_names.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView 
    android:id = "@+id/common_name_layout"
    android:layout_width="fill_parent"
    android:layout_height= "wrap_content" >
</ListView>
</LinearLayout>

When I run my application I'm getting an Runtime NullPointerException. Here is the LogCat. Please help me in fixing this.

11-13 18:23:27.852: INFO/COMMON_NAMES_TABLE(775): Inserting record...
11-13 18:23:27.902: INFO/COMMON_NAMES_TABLE(775): OPening DataBase Connection....
11-13 18:23:28.002: DEBUG/AndroidRuntime(775): Shutting down VM
11-13 18:23:28.002: WARN/dalvikvm(775): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-13 18:23:28.012: ERROR/AndroidRuntime(775): FATAL EXCEPTION: main
11-13 18:23:28.012: ERROR/AndroidRuntime(775): java.lang.RuntimeException: Unable to start activity ComponentInfo{balu.android/balu.android.CommonNames}: java.lang.NullPointerException
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.os.Looper.loop(Looper.java:123)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at java.lang.reflect.Method.invokeNative(Native Method)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at java.lang.reflect.Method.invoke(Method.java:521)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at dalvik.system.NativeStart.main(Native Method)
11-13 18:23:28.012: ERROR/AndroidRuntime(775): Caused by: java.lang.NullPointerException
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at balu.android.CommonNames.onCreate(CommonNames.java:42)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-13 18:23:28.012: ERROR/AndroidRuntime(775):     ... 11 more
11-13 18:23:28.032: WARN/ActivityManager(59):   Force finishing activity balu.android/.CommonNames
11-13 18:23:28.032: WARN/ActivityManager(59):   Force finishing activity balu.android/.Select
11-13 18:23:28.585: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{45025710 balu.android/.CommonNames}
11-13 18:23:31.192: INFO/Process(775): Sending signal. PID: 775 SIG: 9
11-13 18:23:31.212: INFO/ActivityManager(59): Process balu.android (pid 775) has died.
11-13 18:23:31.212: INFO/WindowManager(59): WIN DEATH: Window{45084738 balu.android/balu.android.BabyNamesAppActivity paused=false}

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丿*梦醉红颜 2024-12-21 23:36:10

您不应在行视图上设置 onClickListener。这只将 onClick 事件设置为一行,如果没有内容它会崩溃(这可能是你的问题)

尝试使用

cnListView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id)
    {
    }
});

You shouldn't set the onClickListener on the row view. That only sets the onClick event to one row and if there's no content it will crash (which is probably your problem)

try using

cnListView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id)
    {
    }
});
丢了幸福的猪 2024-12-21 23:36:10

您的 common_names 布局没有 id 为 commonName 的视图,因此 rowview 为 null,roview.setOnClickListener() 将为 NPE。

实际上,通过您粘贴的堆栈跟踪中的错误消息,

...(775): Caused by: java.lang.NullPointerException
...(775):     at balu.android.CommonNames.onCreate(CommonNames.java:42)

应该很容易查明这一点,因为 NPE 位于 CommonNames.java 第 42 行

Your common_names layout has no view with id of commonName so rowviewis null and roview.setOnClickListener() will NPE.

Actually by the error message of

...(775): Caused by: java.lang.NullPointerException
...(775):     at balu.android.CommonNames.onCreate(CommonNames.java:42)

in the stack trace you pasted it should easy to pinpoint this, as the NPE is in CommonNames.java line 42

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文