In Activity A , it has a multiline listView
. The value return from Activity B should populate the Activity A listView
.
Activity A
MyCustomBaseAdapter objMyCustomBaseAdapter;
ArrayList<SearchResults> results=new ArrayList<SearchResults>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.work_details);
listview = (ListView) findViewById(R.id.listView);
name = getIntent().getExtras().getString("Name"); // receive name from Information
weather = getIntent().getExtras().getString("Weather"); //receive weather
date = getIntent().getExtras().getString("date2"); //receive date
status = getIntent().getExtras().getString("Status"); // receive status
objMyCustomBaseAdapter=new MyCustomBaseAdapter(getApplicationContext(),results);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.addDetails:
View menuItemView = findViewById(R.id.addDetails);
PopupMenu po = new PopupMenu(this, menuItemView); //for drop-down menu
po.getMenuInflater().inflate(R.menu.popup_details, po.getMenu());
po.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
if ("Add Work Details".equals(item.getTitle())) {
Intent intent = new Intent(getApplication(), B.class); // go to B class
startActivityForResult(intent, PROJECT_REQUEST_CODE);
}
return true;
}
});
po.show(); //showing popup menu
}
return super.onOptionsItemSelected(item);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B
if (requestCode == PROJECT_REQUEST_CODE) {
ReceiveProject = data.getStringExtra("Project");
ReceiveDescription = data.getStringExtra("Description");
ReceiveProgress = data.getIntExtra("progress", 0);
ReceiveTimeIn = data.getStringExtra("TimeIn");
ReceiveTimeOut = data.getStringExtra("TimeOut");
MyCustomBaseAdapter objMyCustomBaseAdapter= (MyCustomBaseAdapter)listview.getAdapter();
objMyCustomBaseAdapter.addNewItem(ReceiveProject, ReceiveDescription, ReceiveProgress, ReceiveTimeIn, ReceiveTimeOut);
}
}
Activity B
save.setOnClickListener(new View.OnClickListener()
{ // return values to Activity A
@Override
public void onClick(View v)
{
Intent returnIntent=new Intent();
Project=project.getSelectedItem().toString();
Description=description.getText().toString();
progress=seekBar.getProgress();
returnIntent.putExtra("Project",Project);
returnIntent.putExtra("Description", Description);
returnIntent.putExtra("progress", progress);
Toast.makeText(getApplicationContext(), progress+"", Toast.LENGTH_LONG).show();
returnIntent.putExtra("TimeIn", TimeIn);
returnIntent.putExtra("TimeOut",TimeOut);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
});
MyCustomBaseAdapter.java
public class MyCustomBaseAdapter extends BaseAdapter{ // for ListView in Activity A
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public void addNewItem(String P,String D,int Per,String I,String O)
{
SearchResults obj=new SearchResults();
obj.setProject(P);
obj.setDescription(D);
obj.setProgress(Per);
obj.setTimeIn(I);
obj.setTimeOut(O);
searchArrayList.add(obj);
this. notifyDataSetChanged();
}
custom_row_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/ListProject"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/ListDescription"
android:layout_width="wrap_content"
android:textSize="15sp"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/ListProgress"
android:layout_width="wrap_content"
android:textSize="15sp"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:id="@+id/ListTimeIn"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:id="@+id/ListTimeOut"/>
</LinearLayout>
When I click the save button in Activity B to return value to A, app crashed .
Process: com.example.project.myapplication, PID: 23777
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.project.myapplication/com.example.project.myapplication.GUI.A}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.project.myapplication.Adapter.MyCustomBaseAdapter.addNewItem(java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.project.myapplication.Adapter.MyCustomBaseAdapter.addNewItem(java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)' on a null object reference
at com.example.project.myapplication.GUI.A.onActivityResult(A.java:95)
Error where the code refer to
objMyCustomBaseAdapter.addNewItem(ReceiveProject, ReceiveDescription, ReceiveProgress, ReceiveTimeIn, ReceiveTimeOut);
try to set adapter to the listview before you start activity B,, the listView have no adapter so it will return null in getAdapter
edit
Add set adapter after this code to use the adapter when you call listview.getAdapter() ,, without this the adapter will be null
listview = (ListView) findViewById(R.id.listView);
name = getIntent().getExtras().getString("Name"); // receive name from Information
weather = getIntent().getExtras().getString("Weather"); //receive weather
date = getIntent().getExtras().getString("date2"); //receive date
status = getIntent().getExtras().getString("Status"); // receive status
objMyCustomBaseAdapter=new MyCustomBaseAdapter(getApplicationContext(),results);
listview.setAdapter(objMyCustomBaseAdapter);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.