在活动A 中,它有一条多行listView
。从活动B 返回的值应填充活动A listView
。
活动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);
}
}
活动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>
当我单击活动B中的保存按钮将值返回到A时,应用程序崩溃了。
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)
代码引用的错误
objMyCustomBaseAdapter.addNewItem(ReceiveProject, ReceiveDescription, ReceiveProgress, ReceiveTimeIn, ReceiveTimeOut);
答案 0 :(得分:0)
尝试在启动活动B之前将适配器设置为listview listView没有适配器,因此它将在getAdapter中返回null
编辑
在此代码后添加set adapter以在调用listview.getAdapter()时使用适配器 没有这个,适配器将为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);