我尝试在android提供的教程的帮助下实现上面提到的适配器,但是当我启动活动并且错误为NoClassDefFound
时应用程序崩溃这是错误的日志。
01-07 13:56:32.322: W/dalvikvm(9379): threadid=1: thread exiting with uncaught exception (group=0x419be700)
01-07 13:56:32.342: E/AndroidRuntime(9379): FATAL EXCEPTION: main
01-07 13:56:32.342: E/AndroidRuntime(9379): java.lang.NoClassDefFoundError: android.support.v4.widget.SimpleCursorAdapter
01-07 13:56:32.342: E/AndroidRuntime(9379): at com.assassin.expensemanager.HistoryActivity.onCreate(HistoryActivity.java:43)
01-07 13:56:32.342: E/AndroidRuntime(9379): at android.app.Activity.performCreate(Activity.java:5204)
01-07 13:56:32.342: E/AndroidRuntime(9379): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-07 13:56:32.342: E/AndroidRuntime(9379): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2304)
01-07 13:56:32.342: E/AndroidRuntime(9379): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2400)
01-07 13:56:32.342: E/AndroidRuntime(9379): at android.app.ActivityThread.access$600(ActivityThread.java:153)
01-07 13:56:32.342: E/AndroidRuntime(9379): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
01-07 13:56:32.342: E/AndroidRuntime(9379): at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 13:56:32.342: E/AndroidRuntime(9379): at android.os.Looper.loop(Looper.java:137)
01-07 13:56:32.342: E/AndroidRuntime(9379): at android.app.ActivityThread.main(ActivityThread.java:5328)
01-07 13:56:32.342: E/AndroidRuntime(9379): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 13:56:32.342: E/AndroidRuntime(9379): at java.lang.reflect.Method.invoke(Method.java:525)
01-07 13:56:32.342: E/AndroidRuntime(9379): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
01-07 13:56:32.342: E/AndroidRuntime(9379): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
01-07 13:56:32.342: E/AndroidRuntime(9379): at dalvik.system.NativeStart.main(Native Method)
01-07 13:56:33.764: I/Process(9379): Sending signal. PID: 9379 SIG: 9
此处还有我使用的代码
package com.assassin.expensemanager;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;
public class HistoryActivity extends Activity {
private TextView txtHistory;
private ListView lvHistoryDetails;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
// Initializing Views
txtHistory = (TextView) findViewById(R.id.txtHistory);
lvHistoryDetails = (ListView) findViewById(R.id.lvHistoryDetails);
// Initializing DBs and cursors
SQLiteDatabase expenses = MainActivity.expensesDb.getReadableDatabase();
String sql = "Select * from " + ExpenseDBHelper.tableName;
Cursor cursor = expenses.rawQuery(sql, null);
// Setting Views
try {
String [] fromColumns = new String[4];
fromColumns = ExpenseDBHelper.columns;
int [] toViews = {R.id.txtHistoryDate, R.id.txtHistoryCategory, R.id.txtHistoryDescription, R.id.txtHistoryAmount};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.history_row, cursor, fromColumns, toViews, 0) ;
lvHistoryDetails.setAdapter(adapter);
/* StringBuilder txt = new StringBuilder("");
cursor.moveToFirst();
do {
int id = cursor.getInt(0);
String date = cursor.getString(1);
String category = cursor.getString(2);
String desc = cursor.getString(3);
double amount = cursor.getDouble(4);
txt.append( id + " " + date + " " + category + " " + desc + " "
+ amount + "\n");
txtHistory.setText(txt);
}while (cursor.moveToNext());
*/
} catch (Exception e) {
//Log.d("HistoryActivity", e.getStackTrace().toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
}
return super.onContextItemSelected(item);
}
}
答案 0 :(得分:2)
尝试转到项目 - > 属性 - > Java构建路径 - > 订单&导出并确保为您的项目以及您正在使用的所有其他库项目检查Android Private Libraries
。
答案 1 :(得分:1)
尝试替换import android.support.v4.widget.SimpleCursorAdapter;
与import android.widget.SimpleCursorAdapter;
应该有效
答案 2 :(得分:0)
只需转到您的项目属性然后转到java构建路径并添加您的android.jar文件。检查它是否带有检查标记?
解决您的问题并享受。