设备上的Looper.prepare错误不在Android模拟器上

时间:2018-03-15 11:26:22

标签: android

我尝试创建一个应用程序,我将使用CSV还原到Sqlite数据库。 此代码在Android模拟器上工作正常但在设备上不工作。 请帮助我在过去3天内陷入困境我无法理解并尝试了许多来自谷歌的解决方案但没有一个能够正常工作。

public class MainActivity extends ListActivity {


TextView lbl;
DBController controller;
Button btnimport;
ListView lv;
final Context context = this;
ListAdapter adapter;

ArrayList<HashMap<String, String>> myList;
public static final int requestcode = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    controller = new DBController(this);
    lbl = (TextView) findViewById(R.id.txtresulttext);
    btnimport = (Button) findViewById(R.id.btnupload);
    lv = getListView();
    btnimport.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {



            Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT);
            fileintent.addCategory(Intent.CATEGORY_OPENABLE);
            fileintent.setType("text/csv");
            try {

                startActivityForResult(Intent.createChooser(fileintent,"Open CSv"),requestcode);
            } catch (ActivityNotFoundException e) {
                lbl.setText("No activity can handle picking a file. Showing alternatives.");
            }

        }
    });

    myList = controller.getAllProducts();
    if (myList.size() != 0) {
        ListView lv = getListView();
        ListAdapter adapter = new SimpleAdapter(MainActivity.this, myList,
                R.layout.v, new String[]{"a", "b", "c"}, new int[]{
                R.id.txtproductcompany, R.id.txtproductname, R.id.txtproductprice});
        setListAdapter(adapter);
        lbl.setText("");

    }
        }

/** you were wrong here
 * R.id.txtjournalname, R.id.txtjournalissn, R.id.txtjournalif});
 in v.xml its
 R.id.txtproductcompany, R.id.txtproductname, R.id.txtproductprice});
 */

protected void onActivityResult(int requestCode, int resultCode, Intent data) {



    if (data == null)

    return;
           switch (requestCode) {

        case requestcode:

            String filepath = data.getData().getPath();
            controller = new DBController(getApplicationContext());
            SQLiteDatabase db = controller.getWritableDatabase();
            String tableName = "tbljournal";
            db.execSQL("delete from " + tableName);

            try {

                if (resultCode == RESULT_OK) {

                    try {
                        FileReader file = new FileReader(filepath);
                        BufferedReader buffer = new BufferedReader(file);
                        ContentValues contentValues = new ContentValues();
                        String line = "";
                        db.beginTransaction();

                        buffer.readLine();

                        while ((line = buffer.readLine()) != null) {


                            String[] str = line.split(",", 4);  // defining 3 columns with null or blank field //values acceptance

                            //Id, Company,Name,Price

                            String spinnerdata = str[0].toString();
                            String uniqueid = str[1].toString();
                            String melting = str[2].toString();
                            String weight = str[3].toString();

                            Log.e("data", spinnerdata);

                            contentValues.put("spinnerdata", spinnerdata);
                            contentValues.put("uniqueid", uniqueid);
                            contentValues.put("melting", melting);
                            contentValues.put("weight", weight);
                            db.insert(tableName, null, contentValues);

                            lbl.setText("Successfully Updated Database.");

                        }
                        db.setTransactionSuccessful();

                        db.endTransaction();

                    }catch (SQLException e)
                    {
                        Log.e("Error",e.getMessage().toString());
                    }
                    catch (IOException e) {

                        if (db.inTransaction())
                        db.endTransaction();
                        Dialog d = new Dialog(this);
                        d.setTitle(e.getMessage().toString() + "first");
                        d.show();
                        // db.endTransaction();
                    }
                } else {

                    if (db.inTransaction())

                    db.endTransaction();

                    Dialog d = new Dialog(this);

                    d.setTitle("Only CSV files allowed");

                    d.show();
                                        }
                              } catch (Exception ex) {

                if (db.inTransaction())

                db.endTransaction();


                Dialog d = new Dialog(this);

                d.setTitle(ex.getMessage().toString() + "second");

                d.show();

                // db.endTransaction();

            }

    }


    myList = controller.getAllProducts();

    if (myList.size() != 0) {

        ListView lv = getListView();

        ListAdapter adapter = new SimpleAdapter(MainActivity.this, myList,

                R.layout.v, new String[]{"a", "b", "c"}, new int[]{
                R.id.txtproductcompany, R.id.txtproductname, R.id.txtproductprice});


        setListAdapter(adapter);

        lbl.setText("Data Imported");

    }
}

}

以下是LogCat

  

03-15 16:54:27.577 2559-2611 /? E / AbstractTracker:无法在未调用Looper.prepare()的线程内创建处理程序   03-15 16:54:36.185 2559-2620 / com.example.arnav.androidcsv.demo E / AbstractTracker:无法在未调用Looper.prepare()的线程内创建处理程序   03-15 16:54:46.556 2559-2763 / com.example.arnav.androidcsv.demo E / AbstractTracker:无法在未调用Looper.prepare()的线程内创建处理程序

一旦我从文件Manager Nothing Happening中选择了CSV文件,它就会被困住。

0 个答案:

没有答案