在StackOverflow上多次询问与java.lang.RuntimeException: Unable to instantiate application
相关的问题,但我的问题有点不同。
我正在尝试在我的应用程序中实现ActionBarSherlock。如果我创建一个像这样的空白SherlockActivity,一切正常:
public class MainActionBarActivity extends SherlockActivity implements SearchView.OnQueryTextListener,
SearchView.OnSuggestionListener {
private Object mSuggestionsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_action_bar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
boolean isLight = true;
//Create the search view
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search in MyWord¦");
searchView.setOnQueryTextListener(this);
searchView.setOnSuggestionListener(this);
// if (mSuggestionsAdapter == null) {
// MatrixCursor cursor = new MatrixCursor(COLUMNS);
// cursor.addRow(new String[]{"1", "'Murica"});
// cursor.addRow(new String[]{"2", "Canada"});
// cursor.addRow(new String[]{"3", "Denmark"});
// mSuggestionsAdapter = new SuggestionsAdapter(getSupportActionBar().getThemedContext(), cursor);
// }
// searchView.setSuggestionsAdapter(mSuggestionsAdapter);
menu.add("Search")
.setIcon(isLight ? R.drawable.abs__ic_search_api_holo_light : R.drawable.abs__ic_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
// MenuInflater inflater = getSupportMenuInflater();
// inflater.inflate(R.menu.main_action_bar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
createSearchView();
// Handle item selection
switch (item.getItemId()) {
case R.id.action_search:
// do s.th.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void createSearchView() {
//Create the search view
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search for countries…");
searchView.setOnQueryTextListener(this);
searchView.setOnSuggestionListener(this);
String[] COLUMNS = {"A","B","C"};
// if (mSuggestionsAdapter == null) {
// MatrixCursor cursor = new MatrixCursor(COLUMNS);
// cursor.addRow(new String[]{"1", "'Murica"});
// cursor.addRow(new String[]{"2", "Canada"});
// cursor.addRow(new String[]{"3", "Denmark"});
// //mSuggestionsAdapter = new SuggestionsAdapter(getSupportActionBar().getThemedContext(), cursor);
// }
}
@Override
public boolean onSuggestionSelect(int position) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onSuggestionClick(int position) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
}
和Manifest这样的文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jakhar.actionbar"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light.DarkActionBar">
<activity
android:name="com.jakhar.actionbar.MainActionBarActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
但是当我从onCreate方法调用其他类时会出现问题。 首先,它不会让我的一些类在MainActionBarActivity类中导入。如果我手动导入它们,那么调用它们会产生问题。
我有时会收到java.lang.RuntimeException: Unable to instantiate application
这个错误,有时我得ClassDefNotFoundError
。有任何想法可以帮助我。
使用ActionBarSherlock :我在Project-&gt; Properties-&gt; Android下添加ActionBarSherlock
作为依赖项目。