lundi 1 juin 2015

android deal with sqlite

I want to make application where data from B Activity through OnActivityResult method put into A activity.A activity save this data to sqlite and create new textview with this data.In OnCreate Activity A method I want to show all created textview.But when I run the application I have the error.

06-01 19:16:25.133      643-670/? E/power﹕ Can not write CPU_PERF_LEVEL_LOCK. (Invalid argument)
06-01 19:16:25.233  14012-14012/com.example.user.campus E/Trace﹕ error opening trace file: No such file or directory (2)
06-01 19:16:25.243     643-1443/? E/Surface﹕ Invalid SurfaceControl
06-01 19:16:25.263  14012-14012/com.example.user.campus E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.campus/com.example.user.campus.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5751)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.user.campus.MainActivity.onCreate(MainActivity.java:41)
            at android.app.Activity.performCreate(Activity.java:5165)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5751)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
            at dalvik.system.NativeStart.main(Native Method)
06-01 19:16:25.273      643-669/? E/EmbeddedLogger﹕ App crashed! Process: com.example.user.campus
06-01 19:16:25.273      643-669/? E/EmbeddedLogger﹕ App crashed! Package: com.example.user.campus v1 (1.0)
06-01 19:16:25.273      643-669/? E/EmbeddedLogger﹕ Application Label: Campus
06-01 19:16:27.145      643-670/? E/power﹕ request perf lock level(0) is invalid. (Invalid argument)
06-01 19:16:29.908  14044-14044/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-01 19:19:02.661  14151-14151/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-01 19:23:42.489   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.509   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.519   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.529   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.539   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.549   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.750   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.770   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.780   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.790   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.800   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.910   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.950   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:42.990   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:43.010   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:43.020   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:43.080   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:43.120   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:43.170   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:23:43.190   1327-14243/? E/NetworkReportService﹕ ERROR: No reports from previous days.
06-01 19:24:40.631  14259-14259/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-01 19:24:41.002  14285-14285/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-01 19:24:54.837  14322-14322/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-01 19:24:54.997  14338-14338/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-01 19:24:55.417  14350-14350/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-01 19:28:05.230  14412-14412/? E/Trace﹕ error opening trace file: No such file or directory (2)

MainActivity: package com.example.user.campus;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.provider.CalendarContract;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity implements View.OnClickListener {
    Button btnadd;
    LinearLayout llMain;

    DBHelper dbHelper;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        db = dbHelper.getWritableDatabase();

        Cursor c;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        btnadd = (Button) findViewById(R.id.button);
        btnadd.setOnClickListener(this);
        dbHelper = new DBHelper(this);
        c = db.query("mytable", null, null, null, null, null, null);
        if (c.moveToFirst()) {


            int idColIndex = c.getColumnIndex("id");
            int nameColIndex = c.getColumnIndex("name");
            int dateColIndex = c.getColumnIndex("date");

            do {


                ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                TextView textView = new TextView(this);
                textView.setText(c.getString(nameColIndex)+" "+c.getString(dateColIndex));
llMain.addView(textView,layoutParams);

            } while (c.moveToNext());
        } else

        c.close();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.button:
                Intent intent = new Intent(this, result.class);
                startActivityForResult(intent, 1);
                break;

            }
        }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        if (data == null) {
            return;
        }

        String name = data.getStringExtra("name");
        String date = data.getStringExtra("date");

        super.onActivityResult(requestCode, resultCode, data);
        cv.put("name", name);
        cv.put("date", date);

        long rowID = db.insert("mytable", null, cv);





            }

    class DBHelper extends SQLiteOpenHelper {

        public DBHelper(Context context) {

            super(context, "myDB", null, 1);
        }


        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("create table mytable ("
                    + "id integer primary key autoincrement,"
                    + "name text,"
                    + "date text" + ");");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }
    }
}

edit:

public class edit extends ActionBarActivity implements View.OnClickListener{
EditText etName;
    EditText etData;
    Button btnEdit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        etName = (EditText) findViewById(R.id.editText3);
        etData = (EditText) findViewById(R.id.editText4);
        btnEdit = (Button) findViewById(R.id.button3);
        btnEdit.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_edit, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.putExtra("name", etName.getText().toString());
        intent.putExtra("date",etData.getText().toString());
        setResult(RESULT_OK, intent);
        finish();
    }
}

Aucun commentaire:

Enregistrer un commentaire