0

再次出现另一个新手错误,我似乎无法理解,

我想简单地开始一个新课程的意图,我已经在我的应用程序上完成了很多这些操作,没有任何问题。我的manaifest似乎很好(没有拼写错误或不应该出现的小写/大写字母)并且类名拼写正确等。

这是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)

显现:

<?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>

我的班级正在从以下位置加载意图:

        case (R.id.btnSearch):

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

    break;
}

以及旨在从意图打开的“.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);




}
4

2 回答 2

1

你的意图是启动活动就好了。空指针异常出现在这一行

searchButton.setOnClickListener(this);

您尚未为活动设置内容视图,因此findViewById返回 null。

于 2013-01-13T02:38:56.517 回答
1

我认为您setContentView()在搜索课程中失踪了。在访问之前设置布局:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.YourLayOutName);
于 2013-01-13T02:42:41.237 回答