在我的代码中获取nullpointer异常

时间:2013-03-26 09:44:10

标签: android

我正在制作联系人列表申请表。我有一个名为搜索按钮的按钮,当它被点击时,我正在读取文本框中写的名字并在这里搜索数据库是我的代码:

public class SearchResultsActivity extends Activity {

public DatabaseAdapter db;
public ArrayList<String> myList;
public ListView listview;
private TextView view;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);
    view = (TextView) findViewById(R.id.textView1);
    db = new DatabaseAdapter(this);
    myList = new ArrayList<String>();
    getSearchResults();
    //getAllContacts();
    printList();
}

public void getSearchResults(){
    db.open();
    db.getContactbyName(view.getText().toString());
    Cursor c = db.getContactbyName(view.getText().toString());
            if (c.moveToFirst()) {
        do {
            DisplayContact(c);

        } while (c.moveToNext());
    }

    db.close();

}

public void printList(){

    listview = (ListView)findViewById(R.id.contactcListView);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, myList);

    listview.setAdapter(adapter);
}

public void getAllContacts() {
    db.open();
    Cursor c = db.getAllContacts();
    if (c.moveToFirst()) {
        do {
            DisplayContact(c);

        } while (c.moveToNext());
    }
    db.close();
}

public void DisplayContact(Cursor c) {

    String entry = "";
    // if you add another attribute to your table, you need to change 3 into x
    for (int i=1; i<5;i++){
        entry += c.getString(i) + "\n";
    }
    myList.add(entry);
}
}

在我的代码中,getAllContacts()方法工作正常并打印所有联系人。但是,当我尝试搜索联系人时,我得到一个nullpointer异常,应用程序关闭。有谁能看到这个问题?

这是日志猫:

enter image description here

由于

如果你不能阅读它,那么这是另一个版本的logcat:)

03-26 11:52:37.983: D/dalvikvm(12524): GC_CONCURRENT freed 78K, 7% free 12818K/13703K,     
paused 10ms+3ms, total 45ms
03-26 11:52:37.988: D/AbsListView(12524): [unregisterDoubleTapMotionListener]
03-26 11:52:37.993: I/MotionRecognitionManager(12524):   .unregisterListener : /     
listener count = 0->0, listener=android.widget.AbsListView$4@42464368
03-26 11:55:53.778: I/Process(12524): Sending signal. PID: 12524 SIG: 9
03-26 11:56:42.238: E/SensorManager(13254): thread start
03-26 11:56:42.238: D/SensorManager(13254): registerListener :: handle = 0  name=     
LSM330DLC 3-axis Accelerometer delay= 200000 Listener=    
android.view.OrientationEventListener$SensorEventListenerImpl@4243e698
03-26 11:56:42.308: D/SensorManager(13254): unregisterListener::  Listener=     
android.view.OrientationEventListener$SensorEventListenerImpl@4243e698
03-26 11:56:42.308: D/Sensors(13254): Remain listener = Sending .. normal delay 200ms
03-26 11:56:42.308: I/Sensors(13254): sendDelay --- 200000000
03-26 11:56:42.308: D/SensorManager(13254): JNI - sendDelay
03-26 11:56:42.308: I/SensorManager(13254): Set normal delay = true
03-26 11:56:42.333: D/SensorManager(13254): registerListener :: handle = 0  name=    
LSM330DLC 3-axis Accelerometer delay= 200000 Listener=   
android.view.OrientationEventListener$SensorEventListenerImpl@4243e698
03-26 11:56:43.808: D/SensorManager(13254): registerListener :: handle = 0  name=   
LSM330DLC 3-axis Accelerometer delay= 200000 Listener= 
android.view.OrientationEventListener$SensorEventListenerImpl@42474b30
03-26 11:56:43.823: D/dalvikvm(13254): GC_CONCURRENT freed 119K, 7% free 12450K/13319K, 
paused 2ms+2ms, total 21ms
03-26 11:56:43.823: D/dalvikvm(13254): WAIT_FOR_CONCURRENT_GC blocked 6ms
03-26 11:56:43.843: D/SensorManager(13254): unregisterListener::  Listener=   
android.view.OrientationEventListener$SensorEventListenerImpl@42474b30
03-26 11:56:43.843: I/SensorManager(13254): Set min delay = 200000
03-26 11:56:43.843: I/SensorManager(13254): Before sending... delay = 200000
03-26 11:56:43.843: D/Sensors(13254): Remain listener = 
android.view.OrientationEventListener$SensorEventListenerImpl@4243e698
03-26 11:56:43.843: I/Sensors(13254): sendDelay --- 200000000
03-26 11:56:43.843: D/SensorManager(13254): JNI - sendDelay
03-26 11:56:43.848: D/SensorManager(13254): unregisterListener::  Listener= 
android.view.OrientationEventListener$SensorEventListenerImpl@4243e698
03-26 11:56:43.848: D/Sensors(13254): Remain listener = Sending .. normal delay 200ms
03-26 11:56:43.848: I/Sensors(13254): sendDelay --- 200000000
03-26 11:56:43.848: D/SensorManager(13254): JNI - sendDelay
03-26 11:56:43.848: I/SensorManager(13254): Set normal delay = true
03-26 11:56:43.873: D/SensorManager(13254): registerListener :: handle = 0  name= 
LSM330DLC 3-axis Accelerometer delay= 200000 Listener= 
android.view.OrientationEventListener$SensorEventListenerImpl@42474b30
03-26 11:56:44.003: W/IInputConnectionWrapper(13254): getSelectedText on inactive 
InputConnection
03-26 11:56:44.003: W/IInputConnectionWrapper(13254): setComposingText on inactive 
InputConnection
03-26 11:56:44.003: W/IInputConnectionWrapper(13254): getExtractedText on inactive 
InputConnection
03-26 11:56:45.658: D/AbsListView(13254): Get MotionRecognitionManager
03-26 11:56:45.673: D/AndroidRuntime(13254): Shutting down VM
03-26 11:56:45.673: W/dalvikvm(13254): threadid=1: thread exiting with uncaught   
exception (group=0x417922a0)
03-26 11:56:45.678: E/AndroidRuntime(13254): FATAL EXCEPTION: main
03-26 11:56:45.678: E/AndroidRuntime(13254): java.lang.RuntimeException: Unable to 
start activity 
ComponentInfo{cs306.hw2.database/cs306.hw2.database.SearchResultsActivity}:    
java.lang.NullPointerException 03-26 11:56:45.678: E/AndroidRuntime(13254):     at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
android.app.ActivityThread.access$600(ActivityThread.java:140)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
android.os.Handler.dispatchMessage(Handler.java:99)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
android.os.Looper.loop(Looper.java:137)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
android.app.ActivityThread.main(ActivityThread.java:4898)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
java.lang.reflect.Method.invokeNative(Native Method)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
java.lang.reflect.Method.invoke(Method.java:511)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
dalvik.system.NativeStart.main(Native   Method)
03-26 11:56:45.678: E/AndroidRuntime(13254): Caused by: java.lang.NullPointerException
03-26 11:56:45.678: E/AndroidRuntime(13254):    at     
s306.hw2.database.SearchResultsActivity.getSearchResults(SearchResultsActivity.java:34)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
cs306.hw2.database.SearchResultsActivity.onCreate(SearchResultsActivity.java:27)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at  
android.app.Activity.performCreate(Activity.java:5206)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
03-26 11:56:45.678: E/AndroidRuntime(13254):    ... 11 more

这是layour文件          

    <ListView
        android:id="@+id/contactcListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

3 个答案:

答案 0 :(得分:0)

这条线对我来说似乎是一个问题:

 listview = (ListView)findViewById(R.id.contactcListView);

什么是“contactcListView”?我没有在任何地方看到这样的事情。

NPE是最容易解决的问题之一。堆栈跟踪会告诉您发生的文件和行号。您所要做的就是找到哪个引用为null以及为什么。

答案 1 :(得分:0)

没有

view = (TextView) findViewById(R.id.textView1);

在您的布局xml中定义。 view必须为null。

你认为NPE在这里得到了:

Cursor c = db.getContactbyName(view.getText().toString());

请尝试:

<ListView
    android:id="@+id/contactcListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
<TextView android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
 android:id="@+id/textView1"/>
</ListView>
</LinearLayout>

答案 2 :(得分:0)

打开ddms预期并运行应用程序

复制日志