I have a button to add a task .. when I press on it , it takes me to another activity to fill the task details such as (task name, desc, duedate..etc) and when I press on save it must transfer just the task name to the first activity to save it in a list view. I don't have any errors but the (Unfortunately, the application has stopped ) occurs at run time error. please help me !!
public class MainActivity extends AppCompatActivity {
Button add;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = (Button) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(),addtask.class);
startActivityForResult(i,0);
}
});
Intent intent= getIntent();
String task= intent.getStringExtra("task");
ArrayList<String> mylist=new ArrayList<String>();
ListView lv=(ListView) findViewById(R.id.listview);
mylist.add(task);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,R.layout.activity_main,R.id.listview, mylist);
lv.setAdapter(adapter);
}}
and my second class (activity2)
public class addtask extends Activity {
Button save;
EditText tasktxt;
public static String task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dolist);
tasktxt= (EditText) findViewById(R.id.tasktxt);
task= tasktxt.getText().toString();
save= (Button) findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(addtask.this,MainActivity.class);// define the intent
intent.putExtra("task",task); // store the message in "message"
startActivityForResult(intent,0); // calling the other activity
}
});
}
}