Reputation: 3182
Back again with yet another newbie error i cant seem to get my head around,
I want to simplay start an intent to a new class, I have done a number of these on my app with no issue. My manaifest seems fine (no spelling errors or lower/upper case letters where there shouldnt be) and the class name is spelt correctly etc.
Heres the logcat error:
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)
manifest:
<?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>
My class im loading the intent from:
case (R.id.btnSearch):
Intent Search = new Intent("com.example.flybase2.search");
startActivity(Search);
break;
}
And the '.search' class file which is meant to be opened from the intent.
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);
}
Upvotes: 0
Views: 61
Reputation: 27659
I think you are missing setContentView()
in your search class. Set the layout before accessing it:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YourLayOutName);
Upvotes: 1
Reputation: 32026
You intent is launching the activity just fine. The null pointer exception comes on this line
searchButton.setOnClickListener(this);
You have not set a content view for the activity, so the findViewById
returns null.
Upvotes: 1