Unfortunately, "App" has stopped



i'm a student learning for this android programming. in this program i'm trying to create entry database. When i run the program in go smooth for the main activity, but the second activity gave me the "app stop working" message.




  1. Main Activity



    public class MainActivity extends Activity {
    private ListView list;
    private TextView txt;
    private List<Notes> notes;
    private ArrayAdapter<String> adapter;
    private ArrayList<String> arrlist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    notes = data();
    arrlist = data_Notes();

    setContentView(R.layout.activity_main);
    list = (ListView)findViewById(R.id.note_data);
    txt = (TextView)findViewById(R.id.total_data);

    adapter = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_list_item_1,arrlist);
    list.setAdapter(adapter);

    if (notes.size()>0){
    txt.setText(notes.size()+" note saved");
    }else {
    txt.setText("Note not yet available");
    }
    }
    public List<Notes> data(){
    return new NotesRepo(MainActivity.this).listall();
    }
    public ArrayList<String> data_Notes(){
    ArrayList<String> dt = new ArrayList<String>();
    for (int i = 0; i < data().size(); i++){
    dt.add(data().get(i).getJudul());
    }
    return dt;
    }
    public void tambah_data(View view){
    startActivity(new Intent(MainActivity.this,AddActivity.class));
    }



    @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);
    ///}
    }



  2. Second Activity



    public class AddActivity extends Activity{
    private EditText title;
    private EditText body;
    @Override
    protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_data);
    title = (EditText)findViewById(R.id.title_data);
    body = (EditText)findViewById(R.id.body_data);
    }
    public void save_data (View view){
    Notes notes = new Notes();
    notes.setJudul(title.getText().toString());
    notes.setIsi(body.getText().toString());
    try{
    new NotesRepo(AddActivity.this).create(notes);
    Toast.makeText(AddActivity.this, "data saved", Toast.LENGTH_LONG).show();
    }catch (SQLException e){
    Toast.makeText(AddActivity.this, "failed to save data", Toast.LENGTH_LONG).show();
    e.printStackTrace();
    }
    startActivity(new Intent(AddActivity.this,MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    finish();
    }
    }



  3. activity_main.xml



    <RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <TextView
    android:id="@+id/total_data"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_weight="0.3"
    android:text="Data Tersimpan"
    android:textAppearance="?android:attr/textAppearanceMedium"
    />

    <Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:onClick="tambah_data"
    android:text="+" />
    </LinearLayout>
    <ListView
    android:id="@+id/note_data"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" >
    </ListView>

    </RelativeLayout>



  4. Add_data.xml (second activity xml)



    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://ift.tt/nIICcg"
    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" >

    <TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Title"
    android:textAppearance="?android:attr/textAppearanceMedium" />
    <EditText
    android:id="@+id/title_data"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:ems="10" >
    <requestFocus />
    </EditText>
    <TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="Body"
    android:textAppearance="?android:attr/textAppearanceMedium" />
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.2"
    android:orientation="vertical" >
    <EditText
    android:id="@+id/body_data"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="#fff"
    android:ems="10"
    android:hint="isi notes"
    android:inputType="textMultiLine" />
    </LinearLayout>
    <Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button" />

    </LinearLayout>



No comments:

Post a Comment