2011-11-30 54 views
2

我在这里调用对象。调用对象时出错 - 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 
+1

有什么例外? – kabuko

+0

@kabuko异常:'11-22 19:09:41.094:E/AndroidRuntime(609):java.lang.RuntimeException:无法启动活动ComponentInfo {com.simple.search/com.simple.search.TestDetails}:java .lang.NullPointerException' –

+1

然后你显然没有包含相关的代码。它可能在testSearch方法中,也许......只是发布完整的堆栈跟踪,而当你在它时,也可能是TestDetails活动的代码。 – kabuko

回答

1

最有可能的线

String query = intent1.getStringExtra(SearchManager.QUERY); 

是指派一个空值查询 - 你确定正确的额外有没有?我通常会得到额外以这种方式:

public static final String TAG = "MyActivity"; 
public static final String DATA_KEY = "DataKey"; 
public static final String MY_CUSTOM_ACTION = "MyCustomSearchAction"; 

public void onCreate(Bundle savedInstanceState) { 
    Intent intent1 = getIntent(); 
    SimpleSearch SSearch = new SimpleSearch(); 
    if (intent1.getAction().equals(MY_CUSTOM_ACTION) 
      && intent1.hasExtra(SearchManager.QUERY) 
      && intent.hasExtra(DATA_KEY)) { 
     String query = intent1.getStringExtra(SearchManager.QUERY); 
     DataObject data = intent1.getParcelableExtra(DATA_KEY); 
     if (query != null && data != null) 
      SSearch.testSearch(query, data); 
     else { 
      //invalid query 
      Log.d(TAG,"Activity started with invalid query data - closing"); 
      this.finish(); 
      return; 
     } 
    } else { 
     //Invalid Intent 
     Log.d(TAG,"Activity started with invalid intent - closing"); 
     this.finish(); 
     return; 
    } 
} 

的数据对象可以是这样的:

public class DataObject implements Parcelable { 

    public String someData; 
    public String someMoreData; 

    @Override 
    public int describeContents() { 
     return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeString(someData); 
    dest.writeString(someMoreDate); 
} 

//Constructor for parceler 
public DataObject(Parcel src) { 
    someData = src.readString(); 
    someMoreData = src.readString(); 
} 

public static final Parcelable.Creator<DataObject> CREATOR = 
       new Parcelable.Creator<DataObject>() { 

    public DataObject createFromParcel(Parcel in) { 
     return new DataObject(in); 
    } 

    public DataObject[] newArray(int size) { 
     return new DataObject[size]; 
    } 
}; 

}

要开始你的活动只是去:

DataObject data = new DataObject(); 
data.someData = "test"; 
data.someMoreData = "test2"; 
Intent intent = new Intnent(this, MyActivity.class); 
intent.setAction(MyActivity.MY_CUSTOM_ACTION); 
intent.putExtra(MyActivity.DATA_KEY,data); 
intent.putExtra(SearchManager.QUERY, "Query"); 
startActivity(intent); 
+0

我添加了代码,但Listdetails活动现在不打开。 –

+1

TAG需要是一个字符串 - 对不起,我通常在我的所有活动中定义一个TAG变量。 –

+0

我修正了这个问题,但我认为问题出在使用像alexander这样的“新”关键字的Activity上。没有数据的事情将作为一个包传入onCreate方法。仍在尝试解决它。 –