gpt4 book ai didi

java - 开始 Intent 问题

转载 作者:行者123 更新时间:2023-12-02 07:20:32 27 4
gpt4 key购买 nike

又回来了,又出现了一个新手错误,我似乎无法理解,

我想简单地开始一个新类(class)的 Intent ,我已经在我的应用程序上完成了其中的一些操作,没有任何问题。我的 list 看起来不错(没有拼写错误或不应该出现的小写/大写字母)并且类名拼写正确等。

这是 logcat 错误:

01-13 02:23:21.816: E/AndroidRuntime(283): FATAL EXCEPTION: main
01-13 02:23:21.816: E/AndroidRuntime(283): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flybase2/com.example.flybase2.search}: java.lang.NullPointerException
01-13 02:23:21.816: E/AndroidRuntime(283): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-13 02:23:21.816: E/AndroidRuntime(283): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-13 02:23:21.816: E/AndroidRuntime(283): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-13 02:23:21.816: E/AndroidRuntime(283): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-13 02:23:21.816: E/AndroidRuntime(283): at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 02:23:21.816: E/AndroidRuntime(283): at android.os.Looper.loop(Looper.java:123)
01-13 02:23:21.816: E/AndroidRuntime(283): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-13 02:23:21.816: E/AndroidRuntime(283): at java.lang.reflect.Method.invokeNative(Native Method)
01-13 02:23:21.816: E/AndroidRuntime(283): at java.lang.reflect.Method.invoke(Method.java:521)
01-13 02:23:21.816: E/AndroidRuntime(283): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-13 02:23:21.816: E/AndroidRuntime(283): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-13 02:23:21.816: E/AndroidRuntime(283): at dalvik.system.NativeStart.main(Native Method)
01-13 02:23:21.816: E/AndroidRuntime(283): Caused by: java.lang.NullPointerException
01-13 02:23:21.816: E/AndroidRuntime(283): at com.example.flybase2.search.onCreate(search.java:27)
01-13 02:23:21.816: E/AndroidRuntime(283): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-13 02:23:21.816: E/AndroidRuntime(283): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flybase2"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />

            <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".search"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.flybase2.search" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<activity
android:name=".add"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.flybase2.add" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<activity
android:name=".view"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.flybase2.view" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>



</application>

</manifest>

我的类(class)正在加载 Intent :

        case (R.id.btnSearch):

Intent Search = new Intent("com.example.flybase2.search");
startActivity(Search);

break;
}

以及旨在从 Intent 打开的“.search”类文件。

package com.example.flybase2;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class search extends ListActivity implements OnClickListener {


Button searchButton;
EditText searchName;
ListView searchedListResults;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

searchButton = (Button) findViewById(R.id.btnSearch);
searchName = (EditText) findViewById(R.id.inputName);

searchButton.setOnClickListener(this);

}
@Override
public void onClick(View clickedSearchButton) {

String searchedName = searchName.getText().toString();

searchedListResults = (ListView)findViewById(android.R.id.list);
DBHandler DBsearchRef = new DBHandler(this, null, null);

DBHandler search = new DBHandler(this, null, null);

search.open();
Cursor cursor = search.searchOnName(searchedName);

startManagingCursor(cursor);

@SuppressWarnings("static-access")
String [] from = new String [] {DBsearchRef.KEY_NAME, DBsearchRef.KEY_TEL, DBsearchRef.KEY_EMAIL, DBsearchRef.KEY_COMMENTS};
int [] to = new int [] {R.id.txtNameList, R.id.txtTelList, R.id.txtEmailList, R.id.txtCommentsList};

@SuppressWarnings("deprecation")
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.search, cursor, from, to);
searchedListResults.setAdapter(cursorAdapter);




}

最佳答案

您的 Intent 是很好地启动该 Activity 。空指针异常出现在这一行

searchButton.setOnClickListener(this);

您尚未为 Activity 设置内容 View ,因此 findViewById 返回 null。

关于java - 开始 Intent 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14300111/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com