title: how to insert data using a button from fragment in android studio
body: <p>am having a problem while trying to insert into sqlite database in android.
  am having my app with tabs(tab one, tab two,tab three), they are linked to fragments. i put two edit text for amount and reason to the fragment with the button of add in the database. but when i try to fill the form and click to the database it get this error. i need really your help.</p>
  
  <p><img src="https://i.stack.imgur.com/qELAu.png" alt=""></p>
  
  <p><strong>MainActivity Code</strong></p>
  
  <pre><code>package package com.example;
  
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.widget.TextView;
  import android.support.v4.view.ViewPager;
  import android.support.v7.app.ActionBarActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.View;
  import android.support.design.widget.TabLayout;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.support.v4.app.FragmentManager;
  import android.widget.EditText;
  import java.util.ArrayList;
  import java.util.List;
  
  
  public class MainActivity extends AppCompatActivity{
  
  private Toolbar toolbar;
  private TabLayout tabLayout;
  private ViewPager viewPager;
  TextView data;
  private EditText modesteinputAmount;
  private EditText modesteinputReason;
  MyDBHandler dbHandler;
  
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
       data = (TextView) findViewById(R.id.textView2);
       modesteinputAmount = (EditText) findViewById(R.id.inputAmount);
       modesteinputReason = (EditText)findViewById(R.id.inputReason);
       dbHandler = new MyDBHandler(this, null, null, 1);
      printDatabase();
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      getSupportActionBar().setHomeButtonEnabled(true);
  
      viewPager = (ViewPager) findViewById(R.id.viewpager);
  
      setupViewPager(viewPager);
  
      tabLayout = (TabLayout) findViewById(R.id.tabs);
      tabLayout.setupWithViewPager(viewPager);
  
  }
  
  public void addbuttonClicked(View view) {
      modesteinputAmount = (EditText) findViewById(R.id.inputAmount);
      modesteinputReason = (EditText)findViewById(R.id.inputReason);
      dbHandler = new MyDBHandler(this, null, null, 1);
  
      Incomes income = new Incomes(modesteinputAmount.getText().toString());
      Incomes incomes = new Incomes(modesteinputReason.getText().toString());
      dbHandler.addIncomes(income);
      dbHandler.addIncomes(incomes);
      printDatabase();
  
  }
  public void printDatabase(){
      String dbString  = dbHandler.databseToString();
      data.setText(dbString);
  
  }
  
   public void deletebutton(View view) {
       String inputtext = modesteinputReason.getText().toString();
      dbHandler.deleteIncomes(inputtext);
  }
  
  
  
  private void setupViewPager(ViewPager viewPager) {
      ViewPageAdapter adapter = new ViewPageAdapter(getSupportFragmentManager());
      // ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
      adapter.addFragment(new OneFragment(), "ONE");
      adapter.addFragment(new TwoFragment(), "TWO");
      adapter.addFragment(new ThreeFragmant(), "THREE");
      adapter.addFragment(new FourFragment(), "FOUR");
      viewPager.setAdapter(adapter);
  }
  
  
  public class ViewPageAdapter extends FragmentPagerAdapter {
  
      private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
      private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
  
      public ViewPageAdapter(FragmentManager manager) {
          super(manager);
      }
  
  
      @Override
      public Fragment getItem(int position) {
          return mFragmentList.get(position);
      }
  
      @Override
      public int getCount() {
          return mFragmentList.size();
      }
  
      public void addFragment(Fragment fragment, String title) {
          mFragmentList.add(fragment);
          mFragmentTitleList.add(title);
      }
  
     @Override
      public CharSequence getPageTitle(int position) {
         return mFragmentTitleList.get(position);
      }
  
  }
  
  
  }
  </code></pre>
  
  <p><strong>MyDBHandler class</strong></p>
  
  <pre><code>package com.example;
  
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  import android.content.Context;
  import android.content.ContentValues;
  
  public class MyDBHandler extends SQLiteOpenHelper {
  
  private static final int DATABASE_VERSION = 1;
  private static final String DATABASE_NAME = "personalManagementDB.db";
  public static final String TABLE_INCOMES = "incomes";
  public static final String COLUMN_ID ="_id";
  public static final String COLUMN_INCOME_AMOUNT ="incomeAmount";
  public static final String COLUMN_INCOME_REASON ="incomeReason";
  
  public MyDBHandler(Context context, String name,      SQLiteDatabase.CursorFactory factory, int version) {
      super(context, DATABASE_NAME, factory, DATABASE_VERSION);
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {
      String query = "CREATE TABLE " + TABLE_INCOMES + " ( " +
              COLUMN_ID  + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
              COLUMN_INCOME_AMOUNT + " INT " +
              COLUMN_INCOME_REASON + " TEXT " +
              ");";
      db.execSQL(query);
  
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_INCOMES);
      onCreate(db);
  }
  
  public void addIncomes(Incomes income){
      ContentValues values = new ContentValues();
      values.put(COLUMN_INCOME_AMOUNT,income.get_incomeAmout());
      values.put(COLUMN_INCOME_REASON,income.get_incomeDescription());
      SQLiteDatabase db = getWritableDatabase();
      db.insert(TABLE_INCOMES,null,values);
      db.close();
  }
  
  public void deleteIncomes(String incomeReason){
      SQLiteDatabase db = getWritableDatabase();
      db.execSQL("DELETE FROM " + TABLE_INCOMES + " WHERE " +     COLUMN_INCOME_AMOUNT +"=\"" + incomeReason + "\";");
  
  
  }
  public String databseToString(){
      String dbString ="";
      SQLiteDatabase db= getWritableDatabase();
      String query ="SELECT * FROM " + TABLE_INCOMES + "WHERE 1";
  
      Cursor c = db.rawQuery(query, null);
      c.moveToFirst();
  
      while(!c.isAfterLast()){
          if(c.getString(c.getColumnIndex("incomeAmount"))!= null);
              dbString += c.getString(c.getColumnIndex("incomeAmount"));
              dbString +="\n";
          c.moveToNext();
      }
      db.close();
      return dbString;
  }
  }
  
  
  **fragment one**
  package com.example
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  
  
  public class OneFragment extends Fragment {
  
  
  public OneFragment() {
      // Required empty public constructor
  }
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
  
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState)  {
  
      return inflater.inflate(R.layout.fragment_one, container, false);
  }
  
  
    }
  
  Income Class
  
  package com.example;
  
  
  public class Incomes{
  
  private int _id;
  private String _incomeAmout;
  private String _incomeDescription;
  
  public Incomes(){}
  public Incomes(String incomeAmout) {
      this._incomeAmout = incomeAmout;
  }
  
  public void set_id(int _id) {
      this._id = _id;
  }
  
  public void set_incomeAmout(String _incomeAmout) {
      this._incomeAmout = _incomeAmout;
  }
  
  public void set_incomeDescription(String _incomeDescription) {
      this._incomeDescription = _incomeDescription;
  }
  
  public int get_id() {
      return _id;
  }
  
  public String get_incomeAmout() {
      return _incomeAmout;
  }
  
  public String get_incomeDescription() {
      return _incomeDescription;
  }
  
  }
  </code></pre>
  
  <p><strong>fragment one xml file</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent"&gt;
  
  &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:text="Enter Amount"
      android:id="@+id/textView"
      android:layout_marginLeft="48dp"
      android:layout_marginStart="48dp"
      android:layout_alignParentTop="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_marginTop="83dp" /&gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/buttonadd"
      android:id="@+id/addbutton"
      android:layout_centerVertical="true"
      android:layout_alignLeft="@+id/inputReason"
      android:layout_alignStart="@+id/inputReason"
      android:onClick="addbuttonClicked" /&gt;
  
  &lt;EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/inputReason"
      android:width="250dp"
      android:layout_below="@+id/textView3"
      android:layout_alignLeft="@+id/inputAmount"
      android:layout_alignStart="@+id/inputAmount"
      android:inputType="text" /&gt;
  
  &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:text="Enter Reason"
      android:id="@+id/textView3"
      android:layout_below="@+id/inputAmount"
      android:layout_alignLeft="@+id/inputAmount"
      android:layout_alignStart="@+id/inputAmount" /&gt;
  
  &lt;EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/inputAmount"
      android:width="250dp"
      android:layout_below="@+id/textView"
      android:layout_alignLeft="@+id/textView"
      android:layout_alignStart="@+id/textView"
      android:inputType="text" /&gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/buttondelete"
      android:id="@+id/buttondelete"
      android:layout_alignTop="@+id/addbutton"
      android:layout_alignRight="@+id/inputReason"
      android:layout_alignEnd="@+id/inputReason"
      android:onClick="deletebuttonClicked" /&gt;
  
  &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:text="Medium Text"
      android:id="@+id/textView2"
      android:layout_below="@+id/buttondelete"
      android:layout_toRightOf="@+id/addbutton"
      android:layout_toEndOf="@+id/addbutton"
      android:layout_marginTop="59dp" /&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>manifest file (text view)</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example"&gt;
  
  &lt;application
  
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
      &lt;activity
          android:name=".MainActivity"
          android:label="@string/app_name"
          android:theme="@style/AppTheme.NoActionBar"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
   &lt;/application&gt;
  
  &lt;/manifest&gt;
  
  fragment one (design view)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/J1ToC.png" alt=""></p>
  
  <p>i want to click on the button add(which belong to fragment one) and be able to insert into sql lite database. thank you very much </p>