jeudi 25 février 2016

Android Emulator: Unfortunately application has stopped [duplicate]

This question already has an answer here:

I'm new to Android Programming.and every time try to run the project emulator stop. "Android Emulator: Unfortunately application has stopped" project name :Sqllight an this is all code

main activity:

 package com.example.sqllight;
 import android.app.Activity;
 import android.content.ContentValues;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
 import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
  import android.widget.TextView;
   import android.widget.Toast;


 public class MainActivity extends Activity implements OnClickListener {
EditText name;
Button save;
Button view;
TableLayout table;


SQLiteDatabase db;
DBhelper helper;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    helper=new DBhelper(this);
    db=helper.getWritableDatabase();

    name=(EditText) findViewById(R.id.editText1);
    save=(Button)findViewById(R.id.button1);
    view=(Button)findViewById(R.id.button2);


    save.setOnClickListener(this);
    view.setOnClickListener(this);


}
@Override
public void onClick(View v) {

int Id= v.getId()   ;
switch (Id) {
case R.id.button1:
String fr_name= name.getText().toString();
addfriendname(fr_name);

break;

case R.id.button2:
getfriendname();
break;
}


}
private void getfriendname() {
    String columns[]={DB_Data.key_name};
Cursor c=   db.query(true, DB_Data.table_name, columns, null, null, null,     null, null, null);



if(c.moveToFirst())
 {
 do
 {
    int colindex=c.getColumnIndex(DB_Data.key_name);
    String frinds=c.getString(colindex);

    TableRow tr=new TableRow(MainActivity.this);
    TextView tv=new TextView(MainActivity.this);

    tv.setText(frinds);
    tr.addView(tv);
    table.addView(tr);


 }while(c.moveToNext());
 }

}
 private void addfriendname(String fr_name) {
    ContentValues value=new ContentValues();
    value.put(DB_Data.key_name, fr_name);
    long row=   db.insert(DB_Data.table_name, null, value);

if(row != -1)
{
    Toast.makeText(MainActivity.this, row+"was inserted", Toast.LENGTH_SHORT).show();
}
else
{
    Toast.makeText(MainActivity.this, "insertion failed",     Toast.LENGTH_SHORT).show();

}
name.setText("");
}

}

second class:

   package com.example.sqllight;
   public class DB_Data {

     //  database name and version
     public static final String db_name="friends";
     public static final int dv_version=1;
  //table name
  public static final String table_name="frindsnames";
 //table columns
  public static final String key_id="_id";
  public static final String key_name="fr_name";


}

third class:

 package com.example.sqllight;

 import android.content.Context;
 import android.database.DatabaseErrorHandler;
  import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

 public class DBhelper extends SQLiteOpenHelper{

public DBhelper(Context context)
         {
    super(context, DB_Data.db_name, null, DB_Data.dv_version);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
//"creat table"+ tablename  +""(coul1 int primary key autoincrement ,col2 "text")
    String sql="creat table "+DB_Data.table_name +" "+(DB_Data.key_id +"integer primary key autoincrement,"+ DB_Data.key_name +"text");

  db.execSQL(sql);  
  }




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

    //drop table table name
    String sql ="drop table"+DB_Data.table_name;
    db.execSQL(sql);
     onCreate(db);      
        }

        }

activity_main:

  <LinearLayout xmlns:android="http://ift.tt/nIICcg"
   xmlns:tools="http://ift.tt/LrGmb4"
  android:id="@+id/LinearLayout1"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
  android:orientation="vertical"
  android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.example.sqllight.MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="your friend" 
    android:inputType="text"/>

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Save" />

 <Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/View" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:id="@+id/table">


        </TableLayout>

    </ScrollView>

  </LinearLayout>

 </LinearLayout>

Aucun commentaire:

Enregistrer un commentaire