我在這裏調用對象。調用對象時出錯 - Android
public class TestDetails extends ListActivity {
protected TextView testNameText;
protected SQLiteDatabase db;
protected TextView testvalueText;
protected List<TestAction> actions;
protected TestItemAdapter adapter;
protected int testId;
protected int categoryId;
@Override
//adds options menu
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.list_search: onSearchRequested();
break;
}
return true;
}
//end of add options menu
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_details);
// Get the intent, verify the action and get the query
db = (new DatabaseHelper(this)).getWritableDatabase();
Intent intent1 = getIntent();
SimpleSearch SSearch = new SimpleSearch();
if (Intent.ACTION_SEARCH.equals(intent1.getAction())) {
String query = intent1.getStringExtra(SearchManager.QUERY);
SSearch.testSearch(query);
}
testId = getIntent().getIntExtra("EMPLOYEE_ID", 0);
SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT emp._id, emp.firstName, emp.lastName, emp.title, emp.officePhone, emp.cellPhone, emp.email, emp.managerId, mgr.firstName managerFirstName, mgr.lastName managerLastName FROM employee emp LEFT OUTER JOIN employee mgr ON emp.managerId = mgr._id WHERE emp._id = ?",
new String[]{""+testId});
if (cursor.getCount() == 1)
{
cursor.moveToFirst();
testNameText = (TextView) findViewById(R.id.employeeName);
testNameText.setText(cursor.getString(cursor.getColumnIndex("firstName")) + " " + cursor.getString(cursor.getColumnIndex("lastName")));
actions = new ArrayList<TestAction>();
String officePhone = cursor.getString(cursor.getColumnIndex("officePhone"));
if (officePhone != null) {
actions.add(new TestAction("Call office", officePhone, TestAction.ACTION_CALL));
}
String cellPhone = cursor.getString(cursor.getColumnIndex("cellPhone"));
if (cellPhone != null) {
actions.add(new TestAction("Call mobile", cellPhone, TestAction.ACTION_CALL));
actions.add(new TestAction("SMS", cellPhone, TestAction.ACTION_SMS));
}
String email = cursor.getString(cursor.getColumnIndex("email"));
if (email != null) {
actions.add(new TestAction("Email", email, TestAction.ACTION_EMAIL));
}
categoryId = cursor.getInt(cursor.getColumnIndex("managerId"));
if (categoryId>0) {
actions.add(new TestAction("View manager", cursor.getString(cursor.getColumnIndex("managerFirstName")) + " " + cursor.getString(cursor.getColumnIndex("managerLastName")), TestAction.ACTION_VIEW));
}
cursor = db.rawQuery("SELECT count(*) FROM employee WHERE managerId = ?",
new String[]{""+testId});
cursor.moveToFirst();
int count = cursor.getInt(0);
if (count>0) {
actions.add(new TestAction("View direct reports", "(" + count + ")", TestAction.ACTION_REPORTS));
}
adapter = new TestItemAdapter();
setListAdapter(adapter);
}
}
class TestItemAdapter extends ArrayAdapter<TestAction> {
TestItemAdapter() {
super(TestDetails.this, R.layout.action_list_item, actions);
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
public boolean isEnabled(int position) {
return false;
}
public View getView(int position, View convertView, ViewGroup parent) {
TestAction action = actions.get(position);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.action_list_item, parent, false);
TextView label = (TextView) view.findViewById(R.id.label);
label.setText(action.getLabel());
TextView data = (TextView) view.findViewById(R.id.data);
data.setText(action.getData());
return view;
}
}
}
這是我從中調用該對象的類(類的一部分)
public class SimpleSearch extends ListActivity {
protected SQLiteDatabase db;
protected Cursor cursor;
protected ListAdapter adapter;
protected String query;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = (new DatabaseHelper(this)).getWritableDatabase();
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
testSearch(query);
}
else TestListAll();
}
但我上運行的應用越來越接近力。堆棧跟蹤顯示錯誤在SSearch.testSearch(query);
聲明中。我在這裏錯過了什麼?
堆棧跟蹤:
app_vercode:1
device_model:umts_jordan
build_version:1.11.18
condition:1
processName:com.simple.search
pid:3529
uid:10063
tag:null
shortMsg:java.lang.NullPointerException
longMsg:java.lang.NullPointerException: Unable to start activity ComponentInfo{com.simple.search/com.simple.search.TestDetails}: java.lang.NullPointerException
stackTrace:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simple.search/com.simple.search.TestDetails}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1664)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3703)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.simple.search.SimpleSearch.testSearch(SimpleSearch.java:68)
at com.simple.search.TestDetails.onCreate(TestDetails.java:59)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1628)
... 11 more
有什麼例外? – kabuko
@kabuko異常:'11-22 19:09:41.094:E/AndroidRuntime(609):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.simple.search/com.simple.search.TestDetails}:java .lang.NullPointerException' –
然後你顯然沒有包含相關的代碼。它可能在testSearch方法中,也許......只是發佈完整的堆棧跟蹤,而當你在它時,也可能是TestDetails活動的代碼。 – kabuko