簡體   English   中英

如何調試Android運行時錯誤?

[英]How to debug an Android runtime error?

這些又是問題所在:

01-19 10:46:42.630: E/AndroidRuntime(819): FATAL EXCEPTION: main
01-19 10:46:42.630: E/AndroidRuntime(819): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.as400samplecode/com.as400samplecode.AndroidListViewCursorAdaptorActivity}: java.lang.ClassNotFoundException: Didn't find class "com.as400samplecode.AndroidListViewCursorAdaptorActivity" on path: DexPathList[[zip file "/data/app/com.as400samplecode-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.as400samplecode-2, /system/lib]]
01-19 10:46:42.630: E/AndroidRuntime(819):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
01-19 10:46:42.630: E/AndroidRuntime(819):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-19 10:46:42.630: E/AndroidRuntime(819):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-19 10:46:42.630: E/AndroidRuntime(819):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-19 10:46:42.630: E/AndroidRuntime(819):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 10:46:42.630: E/AndroidRuntime(819):  at android.os.Looper.loop(Looper.java:137)
01-19 10:46:42.630: E/AndroidRuntime(819):  at android.app.ActivityThread.main(ActivityThread.java:5103)
01-19 10:46:42.630: E/AndroidRuntime(819):  at java.lang.reflect.Method.invokeNative(Native Method)
01-19 10:46:42.630: E/AndroidRuntime(819):  at java.lang.reflect.Method.invoke(Method.java:525)
01-19 10:46:42.630: E/AndroidRuntime(819):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-19 10:46:42.630: E/AndroidRuntime(819):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-19 10:46:42.630: E/AndroidRuntime(819):  at dalvik.system.NativeStart.main(Native Method)
01-19 10:46:42.630: E/AndroidRuntime(819): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.as400samplecode.AndroidListViewCursorAdaptorActivity" on path: DexPathList[[zip file "/data/app/com.as400samplecode-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.as400samplecode-2, /system/lib]]
01-19 10:46:42.630: E/AndroidRuntime(819):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
01-19 10:46:42.630: E/AndroidRuntime(819):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-19 10:46:42.630: E/AndroidRuntime(819):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-19 10:46:42.630: E/AndroidRuntime(819):  at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
01-19 10:46:42.630: E/AndroidRuntime(819):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
01-19 10:46:42.630: E/AndroidRuntime(819):  ... 11 more

這是我的MainActivity.java。

package com.example.bago;

import com.as400samplecode.R;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {

    private CountriesDbAdapter dbHelper;
     private SimpleCursorAdapter dataAdapter;

     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      dbHelper = new CountriesDbAdapter(this);
      dbHelper.open();

      //Clean all data
      dbHelper.deleteAllCountries();
      //Add some data
      dbHelper.insertSomeCountries();

      //Generate ListView from SQLite Database
      displayListView();

     }

     private void displayListView() {


      Cursor cursor = dbHelper.fetchAllCountries();

      // The desired columns to be bound
      String[] columns = new String[] {
        CountriesDbAdapter.KEY_CODE,
        CountriesDbAdapter.KEY_NAME,
        CountriesDbAdapter.KEY_CONTINENT,
        CountriesDbAdapter.KEY_REGION
      };

      // the XML defined views which the data will be bound to
      int[] to = new int[] { 
        R.id.code,
        R.id.name,
        R.id.continent,
        R.id.region,
      };

      // create the adapter using the cursor pointing to the desired data 
      //as well as the layout information
      dataAdapter = new SimpleCursorAdapter(
        this, R.layout.country_info, 
        cursor, 
        columns, 
        to,
        0);

      ListView listView = (ListView) findViewById(R.id.listView1);
      // Assign adapter to ListView
      listView.setAdapter(dataAdapter);


      listView.setOnItemClickListener(new OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> listView, View view, 
         int position, long id) {
       // Get the cursor, positioned to the corresponding row in the result set
       Cursor cursor = (Cursor) listView.getItemAtPosition(position);

       // Get the state's capital from this row in the database.
       String countryCode = 
        cursor.getString(cursor.getColumnIndexOrThrow("code"));
       Toast.makeText(getApplicationContext(),
         countryCode, Toast.LENGTH_SHORT).show();

       }
      });

      EditText myFilter = (EditText) findViewById(R.id.myFilter);
      myFilter.addTextChangedListener(new TextWatcher() {

       public void afterTextChanged(Editable s) {
       }

       public void beforeTextChanged(CharSequence s, int start, 
         int count, int after) {
       }

       public void onTextChanged(CharSequence s, int start, 
         int before, int count) {
        dataAdapter.getFilter().filter(s.toString());
       }
      });

      dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
             public Cursor runQuery(CharSequence constraint) {
                 return dbHelper.fetchCountriesByName(constraint.toString());
             }
         });

     }
    }

Country.java

package com.example.bago;

public class Country {
    String code = null;
     String name = null;
     String continent = null;
     String region = null;

     public String getCode() {
      return code;
     }
     public void setCode(String code) {
      this.code = code;
     }
     public String getName() {
      return name;
     }
     public void setName(String name) {
      this.name = name;
     }
     public String getContinent() {
      return continent;
     }
     public void setContinent(String continent) {
      this.continent = continent;
     }
     public String getRegion() {
      return region;
     }
     public void setRegion(String region) {
      this.region = region;
     }


    }

國家DBAdapter.java

package com.example.bago;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class CountriesDbAdapter {
    public static final String KEY_ROWID = "_id";
     public static final String KEY_CODE = "code";
     public static final String KEY_NAME = "name";
     public static final String KEY_CONTINENT = "continent";
     public static final String KEY_REGION = "region";

     private static final String TAG = "CountriesDbAdapter";
     private DatabaseHelper mDbHelper;
     private SQLiteDatabase mDb;

     private static final String DATABASE_NAME = "World";
     private static final String SQLITE_TABLE = "Country";
     private static final int DATABASE_VERSION = 1;

     private final Context mCtx;

     private static final String DATABASE_CREATE =
      "CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
      KEY_ROWID + " integer PRIMARY KEY autoincrement," +
      KEY_CODE + "," +
      KEY_NAME + "," +
      KEY_CONTINENT + "," +
      KEY_REGION + "," +
      " UNIQUE (" + KEY_CODE +"));";

     private static class DatabaseHelper extends SQLiteOpenHelper {

      DatabaseHelper(Context context) {
       super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }


      @Override
      public void onCreate(SQLiteDatabase db) {
       Log.w(TAG, DATABASE_CREATE);
       db.execSQL(DATABASE_CREATE);
      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
         + newVersion + ", which will destroy all old data");
       db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
       onCreate(db);
      }
     }

     public CountriesDbAdapter(Context ctx) {
      this.mCtx = ctx;
     }

     public CountriesDbAdapter open() throws SQLException {
      mDbHelper = new DatabaseHelper(mCtx);
      mDb = mDbHelper.getWritableDatabase();
      return this;
     }

     public void close() {
      if (mDbHelper != null) {
       mDbHelper.close();
      }
     }

     public long createCountry(String code, String name, 
       String continent, String region) {

      ContentValues initialValues = new ContentValues();
      initialValues.put(KEY_CODE, code);
      initialValues.put(KEY_NAME, name);
      initialValues.put(KEY_CONTINENT, continent);
      initialValues.put(KEY_REGION, region);

      return mDb.insert(SQLITE_TABLE, null, initialValues);
     }

     public boolean deleteAllCountries() {

      int doneDelete = 0;
      doneDelete = mDb.delete(SQLITE_TABLE, null , null);
      Log.w(TAG, Integer.toString(doneDelete));
      return doneDelete > 0;

     }

     public Cursor fetchCountriesByName(String inputText) throws SQLException {
      Log.w(TAG, inputText);
      Cursor mCursor = null;
      if (inputText == null  ||  inputText.length () == 0)  {
       mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
         KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION}, 
         null, null, null, null, null);

      }
      else {
       mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
         KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION}, 
         KEY_NAME + " like '%" + inputText + "%'", null,
         null, null, null, null);
      }
      if (mCursor != null) {
       mCursor.moveToFirst();
      }
      return mCursor;

     }

     public Cursor fetchAllCountries() {

      Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
        KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION}, 
        null, null, null, null, null);

      if (mCursor != null) {
       mCursor.moveToFirst();
      }
      return mCursor;
     }

     public void insertSomeCountries() {

      createCountry("AFG","Afghanistan","Asia","Southern and Central Asia");
      createCountry("ALB","Albania","Europe","Southern Europe");
      createCountry("DZA","Algeria","Africa","Northern Africa");
      createCountry("ASM","American Samoa","Oceania","Polynesia");
      createCountry("AND","Andorra","Europe","Southern Europe");
      createCountry("AGO","Angola","Africa","Central Africa");
      createCountry("AIA","Anguilla","North America","Caribbean");

     }

    }

很難說,但看起來android試圖加載以下活動

java.lang.ClassNotFoundException:找不到類“ com.as400samplecode.AndroidListViewCursorAdaptorActivity”

也許您的清單文件中有AndroidListViewCursorAdaptorActivity ,但您的AndroidListViewCursorAdaptorActivity中沒有AndroidListViewCursorAdaptorActivity

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM