当我点击执行DBoperation的按钮(在ListView中检索和显示数据)时,我的应用程序崩溃了

时间:2017-05-02 10:40:25

标签: android listview

这里我有一个mechanic.java文件,它接受一个特定的textview值并传递给FetchData.java文件,在该文件中已完成数据库操作以从数据库中检索数据,并应显示在ListView中。



public class MechanicPage extends Activity {
  public ListView list;
  EditText cityName;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mechanic_page);

    cityName = (EditText) findViewById(R.id.place);

    Resources res = getResources();
  }

  public void onclick(View view) {
    FetchData fetchData = new FetchData(this);
    String city = cityName.getText().toString();
    Profile p = fetchData.getData(city);

    list = (ListView) findViewById(android.R.id.list);
    ListAdapter adopt = new ListAdapter(this, p);
    list.setAdapter(adopt);

  }
}

class ListAdapter extends ArrayAdapter < Profile > {
  Context context;
  Profile profile;

  public EditText etName,
  etContact;


  public ListAdapter(Context context, Profile objects) {
    super(context, R.layout.data_list);
    try {
      this.context = context;
      this.profile = objects;

    } catch (SQLException e) {
      Message.message(context, "" + e);
    }


  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;

    if (row == null) {
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

      //row object has refrence to the relative layout of single_row layout file
      row = inflater.inflate(R.layout.data_list, parent, false);
    }
    etName = (EditText) row.findViewById(R.id.name);
    etContact = (EditText) row.findViewById(R.id.contact);

    String name = profile.getName();
    int contact = profile.getContact();

    etName.setText(name);
    etContact.setText(contact);

    return row;
  }
}
&#13;
&#13;
&#13;

这是FetchData.java文件,它执行数据库操作以检索数据并返回对象。

&#13;
&#13;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;


/**
 * Created by lathass on 24/04/17.
 */

public class FetchData {
  DatabaseOpenHelper openHelper;
  SQLiteDatabase database;
  Context context;
  String name;
  int contact;
  ListView list;

  Profile profile = new Profile(name, contact);

  public FetchData(Context context) {
    openHelper = new DatabaseOpenHelper(context);
  }
  public Profile getData(String city) {
    database = openHelper.getWritableDatabase();
    String[] col = {
      DatabaseOpenHelper.NAME,
      DatabaseOpenHelper.PHONE
    };
    Cursor c = null;

    try {
      c = database.query(DatabaseOpenHelper.TABLE1, col, DatabaseOpenHelper.CITY + " = ?", new String[] {
        city
      }, null, null, null);
      c.moveToFirst();
      while (!c.isAfterLast()) {

        name = c.getString(c.getColumnIndex(DatabaseOpenHelper.NAME));
        profile.setName(name);

        contact = c.getInt(c.getColumnIndex(DatabaseOpenHelper.PHONE));
        profile.setContact(contact);

        c.moveToNext();
      }
      c.close();
    } catch (SQLException e) {
      Message.message(context, "" + e);
    }
    return profile;
  }


  public class DatabaseOpenHelper extends SQLiteAssetHelper {

    private static final String DB_NAME = "Data.db";
    private static final int DB_VERSION = 1;
    private static final String TABLE1 = "mechanic";
    private static final String TABLE2 = "lodge";
    private static final String TABLE3 = "travels";

    private static final String NAME = "Name";
    private static final String PHONE = "Phone";
    private static final String CITY = "City";


    Context context;
    public DatabaseOpenHelper(Context context) {
      super(context, DB_NAME, null, DB_VERSION);
      this.context = context;
    }
  }
  public void close() {
    if (database != null)
      this.database.close();
  }

}
&#13;
&#13;
&#13;

这是LogCat

&#13;
&#13;
05 - 04 00: 45: 53.402 2777 - 6542 / com.google.android.gms.unstable W / DG.WV: Widevine DRM not supported on this device
android.media.UnsupportedSchemeException: Failed to instantiate drm object.
at android.media.MediaDrm.native_setup(Native Method)
at android.media.MediaDrm. < init > (MediaDrm.java: 180)
at ono.a(: com.google.android.gms: 122)
at okh.run(: com.google.android.gms: 1095)
at lmq.run(: com.google.android.gms: 450)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java: 1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java: 587)
at lra.run(: com.google.android.gms: 17)
at java.lang.Thread.run(Thread.java: 818)
05 - 04 00: 45: 53.409 1948 - 6539 / com.google.android.gms.persistent I / GoogleURLConnFactory: Using platform SSLCertificateSocketFactory
05 - 04 00: 45: 53.496 1730 - 1846 / system_process W / AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
05 - 04 00: 45: 53.499 6028 - 6028 / com.project.mechfinder I / SQLiteAssetHelper: successfully opened database Data.db
05 - 04 00: 45: 53.535 1730 - 2010 / system_process I / art: Explicit concurrent mark sweep GC freed 41029(2 MB) AllocSpace objects, 20(377 KB) LOS objects, 30 % free, 8 MB / 12 MB, paused 1.254 ms total 33.640 ms
05 - 04 00: 45: 54.149 1948 - 6539 / com.google.android.gms.persistent I / GoogleURLConnFactory: Using platform SSLCertificateSocketFactory

[05 - 04 00: 45: 54.279 2777: 2792 D / ]
HostConnection::get() New Host Connection established 0xb429f7c0, tid 2792
05 - 04 00: 45: 54.678 1948 - 6539 / com.google.android.gms.persistent W / Uploader: no longer exists, so no auth token.
05 - 04 00: 45: 54.819 1948 - 2471 / com.google.android.gms.persistent W / Conscrypt: Could not set socket write timeout: null
05 - 04 00: 45: 54.988 1948 - 2471 / com.google.android.gms.persistent W / Conscrypt: Could not set socket write timeout: null
05 - 04 00: 45: 55.648 1948 - 2471 / com.google.android.gms.persistent W / GLSUser: [AppCertManager] IOException
while requesting key:
  java.io.IOException: Invalid device key response.
at ewg.a(: com.google.android.gms: 274)
at ewg.a(: com.google.android.gms: 4238)
at ewf.a(: com.google.android.gms: 45)
at evz.a(: com.google.android.gms: 50)
at evy.a(: com.google.android.gms: 104)
at com.google.android.gms.auth.account.be.legacy.AuthCronChimeraService.b(: com.google.android.gms: 4049)
at edi.call(: com.google.android.gms: 2041)
at java.util.concurrent.FutureTask.run(FutureTask.java: 237)
at lmq.run(: com.google.android.gms: 450)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java: 1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java: 587)
at lra.run(: com.google.android.gms: 17)
at java.lang.Thread.run(Thread.java: 818)
05 - 04 00: 46: 43.520 6028 - 6028 / com.project.mechfinder I / SQLiteAssetHelper: successfully opened database Data.db
05 - 04 00: 46: 43.521 1730 - 1846 / system_process W / AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
05 - 04 00: 46: 53.720 1730 - 1846 / system_process W / AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
05 - 04 00: 46: 53.724 6028 - 6028 / com.project.mechfinder I / SQLiteAssetHelper: successfully opened database Data.db
05 - 04 00: 47: 00.822 1730 - 1846 / system_process W / AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
05 - 04 00: 47: 00.825 6028 - 6028 / com.project.mechfinder I / SQLiteAssetHelper: successfully opened database Data.db
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我在这里疯狂猜测,因为没有Logcat。

如果您要设置自己的布局(而不是从ListActivity扩展),则很可能您没有使用android.R.id.list作为View ID。

因此,您使用此ID检索ListView引用这一事实让我觉得它在NullPointerException list.setAdapter(adopt);上爆炸了。

编辑:

您必须在xml中为ListView定义自己的ID,而不是调用findViewById(android.R.id.list);,而应使用findViewById(R.id.your_list_id);