点击搜索到的活动后,我的应用会立即崩溃。这是下面的代码行。我正在片段中查看它;它在片段中加载SQLite数据库。应用程序立即崩溃;我点击列表项中的搜索视图。
public class HymnsFragment extends Fragment implements SearchView.OnQueryTextListener,
SearchView.OnCloseListener {
private ListView mListView;
private SearchView searchView;
private CustomersDbAdapter mDbHelper;
private TextView inspectionDate;
private TextView customerText;
private TextView nameText;
private TextView addressText;
private TextView cityText;
private TextView stateText;
private TextView zipCodeText;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_hymns,container,false);
searchView = (SearchView)rootView.findViewById(R.id.search);
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
searchView.setOnCloseListener(this);
mListView = (ListView)rootView.findViewById(R.id.list);
// inspectionDate = (TextView) findViewById(R.id.inspectionDate);
//displayDate();
mDbHelper = new CustomersDbAdapter(getActivity());
mDbHelper.open();
//Clean all Customers
mDbHelper.deleteAllCustomers();
//Add some Customer data as a sample
mDbHelper.createCustomer("Hymn No", "Hymn Index", "Hymn name","", "Hymn Body","AUthor" , "90007");
mDbHelper.createCustomer("Hymn No", "Hymn Index", "Hymn name", "", "Hymn Body", "AUthor", "90015");
mDbHelper.createCustomer("Hymn No", "Hymn Index", "Hymn name", "", "Hymn Body", "AUthor", "90014");
mDbHelper.createCustomer("Hymn No", "Hymn Index", "Hymn name", "", "Hymn Body", "AUthor", "90026");
mDbHelper.createCustomer("Hymn No", "Hymn Index", "Hymn name", "", "Los Angeles", "AUthor", "90065");
mDbHelper.createCustomer("Hymn No", "Hymn Index", "4351 South Central Avenue", "", "Los Angeles", "CA", "90011");
mDbHelper.createCustomer("Hymn No", "Hymn Index", "975 West Jefferson", "", "Los Angeles", "CA", "90007");
mDbHelper.createCustomer("Hymn No", "Hymn Index", "2805 South Figueroa Street", "", "Los Angeles", "CA", "90007");
mDbHelper.createCustomer("Hymn No", "Hymn Index", "198 South Vermont Avenue", "", "Los Angeles", "CA", "90004");
mDbHelper.createCustomer("Hymn No", "Hymn Index", "504 West Olympic Boulevard", "", "Los Angeles", "CA", "90015");
mDbHelper.createCustomer("Hymn No", "Hymn Index", "975 West Jefferson", "", "Los Angeles", "CA", "90007");
mDbHelper.createCustomer("Hymn No", "Hymn Index", "504 West Olympic Boulevard", "", "Los Angeles", "CA", "90015");
mDbHelper.createCustomer("Hymn No", "Hymn Index", "504 West Olympic Boulevard", "", "Los Angeles", "CA", "90015");
mDbHelper.createCustomer("Hymn 1", "Hymn Index", "504 West Olympic Boulevard", "", "Los Angeles", "CA", "90015");
mDbHelper.createCustomer("Hymn 1", "Hymn Index", "504 West Olympic Boulevard", "", "Los Angeles", "CA", "90015");
return rootView;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mDbHelper != null) {
mDbHelper.close();
}
}
public boolean onQueryTextChange(String newText) {
showResults(newText + "*");
return false;
}
public boolean onQueryTextSubmit(String query) {
showResults(query + "*");
return false;
}
public boolean onClose() {
showResults("");
return false;
}
private void showResults(String query) {
Cursor cursor = mDbHelper.searchCustomer((query != null ? query.toString() : "@@@@"));
if (cursor == null) {
//
} else {
// Specify the columns we want to display in the result
String[] from = new String[]{
CustomersDbAdapter.KEY_CUSTOMER,
CustomersDbAdapter.KEY_NAME,
CustomersDbAdapter.KEY_ADDRESS,
// CustomersDbAdapter.KEY_CITY,
CustomersDbAdapter.KEY_STATE,
CustomersDbAdapter.KEY_ZIP};
// Specify the Corresponding layout elements where we want the columns to go
int[] to = new int[]{
R.id.scustomer,
R.id.sname,
R.id.saddress,
//R.id.scity,
R.id.sstate};
// R.id.szipCode};
// Create a simple cursor adapter for the definitions and apply them to the ListView
SimpleCursorAdapter customers = new SimpleCursorAdapter(mListView.getContext(),R.layout.customerresult,cursor,from,to);
// SimpleCursorAdapter customers = new SimpleCursorAdapter(this, R.layout.customerresult, cursor, from, to);
mListView.setAdapter(customers);
// Define the on-click listener for the list items
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) mListView.getItemAtPosition(position);
// Get the state's capital from this row in the database.
String customer = cursor.getString(cursor.getColumnIndexOrThrow("customer"));
String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
String address = cursor.getString(cursor.getColumnIndexOrThrow("address"));
String city = cursor.getString(cursor.getColumnIndexOrThrow("city"));
String state = cursor.getString(cursor.getColumnIndexOrThrow("state"));
String zipCode = cursor.getString(cursor.getColumnIndexOrThrow("zipCode"));
//Check if the Layout already exists
LinearLayout customerLayout = (LinearLayout)view.findViewById(R.id.customerLayout);
if (customerLayout == null) {
LinearLayout leftLayout = (LinearLayout)view.findViewById(R.id.rightLayout);
View customerInfo = getLayoutInflater().inflate(R.layout.customer_info, leftLayout, false);
leftLayout.addView(customerInfo);
}
//Get References to the TextViews
customerText = (TextView)view.findViewById(R.id.customer);
nameText = (TextView)view. findViewById(R.id.name);
addressText = (TextView)view. findViewById(R.id.address);
cityText = (TextView)view. findViewById(R.id.city);
stateText = (TextView)view. findViewById(R.id.state);
// zipCodeText = (TextView) findViewById(R.id.zipCode);
// Update the parent class's TextView
customerText.setText(customer);
nameText.setText(name);
addressText.setText(address);
cityText.setText(city);
stateText.setText(state);
//zipCodeText.setText(zipCode);
searchView.setQuery("", true);
}
});
}
}
private LayoutInflater getLayoutInflater() {
return null;
}
}
这是log cat
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial D/AndroidRuntime: procName from cmdline: com.example.vickie.mfmofficial
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: in writeCrashedAppName, pkgName :com.example.vickie.mfmofficial
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial D/AndroidRuntime: file written successfully with content: com.example.vickie.mfmofficial StringBuffer : ;com.example.vickie.mfmofficial
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: FATAL EXCEPTION: main
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: Process: com.example.vickie.mfmofficial, PID: 12417
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: java.lang.NullPointerException
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: at com.example.vickie.mfmofficial.HymnsFragment$1.onItemClick(HymnsFragment.java:152)
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: at android.widget.AdapterView.performItemClick(AdapterView.java:299)
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: at android.widget.AbsListView$3.run(AbsListView.java:3638)
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5021)
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
06-27 00:15:16.745 12417-12417/com.example.vickie.mfmofficial E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
我认为问题出在这一行:
View customerInfo = getLayoutInflater().inflate(R.layout.customer_info, leftLayout, false);
正如您定义getLayoutInflater
方法一样:
private LayoutInflater getLayoutInflater() {
return null;
}
这显然会导致NullPointerException
。
相反,尝试使用这样的东西:
View customerInfo = LayoutInflater.from(getActivity()).inflate(R.layout.customer_info, leftLayout, false);
或者,您可以修改getLayoutInflater
方法,以便它不会返回null
:
private LayoutInflater getLayoutInflater() {
return LayoutInflater.from(getActivity());
}