0

I have a simple app here which runs perfectly on emulator. but shows application has stopped when run on a mobile or tab.

My mainactivity is like this.

public class MainActivity extends Activity {

    EditText animalName;
    DBController controller = new DBController(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        animalName = (EditText) findViewById(R.id.editText1);

        final ListView lv=(ListView)findViewById(R.id.listView1);
        final TextView tv=(TextView)findViewById(R.id.textView1);

        final ArrayList<String> your_array_list = new ArrayList<String>();
        final ArrayAdapter<String> arrayAdapter;

        Button b2=(Button)findViewById(R.id.button2);
        b2.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                tv.setText(controller.getInfo());   

            }

        });

        Button b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
                String name=animalName.getText().toString();
                controller.createEntry(name);
                this.callHomeActivity(view);

            }

            private void callHomeActivity(View view) {
                // TODO Auto-generated method stub
                Intent objIntent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(objIntent);
            }

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

}

and DBController.java

package com.example.dbapp;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class DBController  extends SQLiteOpenHelper {
      private static final String LOGCAT = null;

      private static String DB_TABLE = "Animals";

      private static String KEY_ROW = "animalId";

      private static String KEY_NAME = "animalName";



      public DBController(Context applicationcontext) {
        super(applicationcontext, "androidsqlite.db", null, 1);
        Log.d(LOGCAT,"Created");
      }

      @Override
      public void onCreate(SQLiteDatabase database) {
        String query;
        //query = "CREATE TABLE" +DB_TABLE+"(" +KEY_ROW+" INTEGER PRIMARY KEY AUTOINCREMENT, " +KEY_NAME+ " TEXT NOT NULL);"
        database.execSQL("CREATE TABLE" +DB_TABLE+"(" +KEY_ROW+" INTEGER PRIMARY KEY AUTOINCREMENT, " +KEY_NAME+ " TEXT NOT NULL);");
        Log.d(LOGCAT,"animals Created");
      }
      @Override
      public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
        String query;
        query = "DROP TABLE IF EXISTS animals";
        database.execSQL(query);
        onCreate(database);
      }

      public String getInfo()
      {
          SQLiteDatabase myDataBase = this.getReadableDatabase();
        String s = null;
        Cursor c=myDataBase.rawQuery("SELECT * FROM animals",null);
        if(c.moveToFirst())
        {
            s=c.getString(c.getColumnIndex("animalName"));
        }
        return s;
      } 

      public void createEntry(String name)
      {
          String s= "Insert success";
          SQLiteDatabase database = this.getWritableDatabase();
          ContentValues cv = new ContentValues();
          cv.put(KEY_NAME, name);
          database.insert("DB_TABLE",null,cv);
          database.close();
      }   
}

The app has to take value from the editText and should be inserted into the table. And on clicking display it should show the first row element.

6
  • It shows no errors in the logcat Jan 28, 2014 at 14:48
  • If it shows that it stops, there must be a logcat
    – Emmanuel
    Jan 28, 2014 at 14:49
  • If it shows "has stopped" that it must show you some error in Logcat. If you can't find one, paste the complete logcat to pastebin.com and link it here. Jan 28, 2014 at 14:49
  • Without the logcat I can't be sure it's the only error, but you need a space after CREATE TABLE in your SQL statement.
    – NigelK
    Jan 28, 2014 at 14:54
  • How can I get the logcat for the application running on another device? Jan 28, 2014 at 16:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.