简体   繁体   中英

My App crashes when I click on the button which perform DBoperation(Retrieving and displaying data in ListView)

Here I have mechanic.java file which takes a particular textview value and passed to the FetchData.java file where in DB operation has been done to retrieve the data from database and should be displayed in a 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; } } 

And here is the FetchData.java file which perform db operation to retrieve data and return the object.

 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(); } } 

Here is the LogCat

 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 

I am taking a wild guess here as there's no Logcat.

If you are setting up your own layout (and not extending from ListActivity ), is likely that you are not using android.R.id.list as a View id.

Therefore, the fact that you are retrieving the ListView reference using this ID, makes me think that it's blowing up with a NullPointerException on list.setAdapter(adopt); .

Edit:

You have to define your own ID for the ListView in your xml, and instead of calling findViewById(android.R.id.list); you should use findViewById(R.id.your_list_id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM