从另一个活动错误中添加listview中的数据

时间:2012-10-21 21:39:36

标签: android listview

我想问..我有2个表单,说它是form1和form2,我在form1上有listview,我想从form2中设置数据。所以我只是创建一个虚拟项目来试试它,我想知道为什么不会工作..

所以这是我在 form2 上的代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.form2);             


    ArrayList<MyClass> searchResults = GetSearchResults();                
      form1.listview.setAdapter(new MyAdapter(this, searchResults));
}

private ArrayList<MyClass> GetSearchResults(){
         ArrayList<MyClass> results = new ArrayList<MyClass>();

         MyClass sr = new MyClass();
         sr.setJumlah_menu("Justin Schultz");
         sr.setNama_menu("San Francisco, CA");

         results.add(sr);          

         return results;
  }

这是我的适配器代码:

public class AdapterListPemesananMenu extends BaseAdapter {
    private static ArrayList<MenuInputClass> searchArrayList;

    private LayoutInflater mInflater;

    public AdapterListPemesananMenu(Context context, ArrayList<MenuInputClass> results) {
        searchArrayList = results;
        mInflater = LayoutInflater.from(context);
    }



    public AdapterListPemesananMenu(OnClickListener onClickListener,
            ArrayList<MenuInputClass> searchResults) {
        // TODO Auto-generated constructor stub
    }



    public int getCount() {
        return searchArrayList.size();
    }

    public Object getItem(int position) {
        return searchArrayList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_pemesananmenu_row, null);
            holder = new ViewHolder();
            holder.txtName = (TextView) convertView.findViewById(R.id.txtNamaMasuk);
            holder.txtCityState = (TextView) convertView
                    .findViewById(R.id.txtJumlahMasuk);
            //holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtName.setText(searchArrayList.get(position).getNama_menu());
        holder.txtCityState.setText(searchArrayList.get(position)
                .getJumlah_menu());


        return convertView;
    }

    static class ViewHolder {
        TextView txtName;
        TextView txtCityState;

    } 
}

当我从form1转到form2时,它总是强制关闭..

我的代码有什么问题?

当我在form1上尝试代码(来自form2)时,它的工作很好......

这是来自logcat:

10-22 04:33:30.647: E/AndroidRuntime(15247): FATAL EXCEPTION: main
10-22 04:33:30.647: E/AndroidRuntime(15247): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aplikasipemesananmenu/list_pemesananmenu_view.SingleListView_Activity}: java.lang.NullPointerException
10-22 04:33:30.647: E/AndroidRuntime(15247):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
10-22 04:33:30.647: E/AndroidRuntime(15247):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-22 04:33:30.647: E/AndroidRuntime(15247):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-22 04:33:30.647: E/AndroidRuntime(15247):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-22 04:33:30.647: E/AndroidRuntime(15247):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 04:33:30.647: E/AndroidRuntime(15247):    at android.os.Looper.loop(Looper.java:130)
10-22 04:33:30.647: E/AndroidRuntime(15247):    at android.app.ActivityThread.main(ActivityThread.java:3693)
10-22 04:33:30.647: E/AndroidRuntime(15247):    at java.lang.reflect.Method.invokeNative(Native Method)
10-22 04:33:30.647: E/AndroidRuntime(15247):    at java.lang.reflect.Method.invoke(Method.java:507)
10-22 04:33:30.647: E/AndroidRuntime(15247):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
10-22 04:33:30.647: E/AndroidRuntime(15247):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
10-22 04:33:30.647: E/AndroidRuntime(15247):    at dalvik.system.NativeStart.main(Native Method)
10-22 04:33:30.647: E/AndroidRuntime(15247): Caused by: java.lang.NullPointerException
10-22 04:33:30.647: E/AndroidRuntime(15247):    at list_pemesananmenu_view.SingleListView_Activity.onCreate(SingleListView_Activity.java:67)
10-22 04:33:30.647: E/AndroidRuntime(15247):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-22 04:33:30.647: E/AndroidRuntime(15247):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-22 04:33:30.647: E/AndroidRuntime(15247):    ... 11 more
10-22 04:33:31.958: E/DataRouter(125): smd_file_fd = 16 and external_file_fd = 24
10-22 04:33:31.958: E/DataRouter(125): ==KDW(3)== smd_new_status = (358)
10-22 04:33:31.958: E/DataRouter(125): ==KDW(4)== smd_new_status = (320)
10-22 04:33:31.958: E/DataRouter(125): DCD pinstatus is ON 

任何帮助都会感谢谢谢...

1 个答案:

答案 0 :(得分:0)

如何在form2.onCreate()方法上设置form1? 好像NPE在form1.listview.setAdapter上(new MyAdapter(this,searchResults));等。

我认为你不应该在另一个中使用一个活动对象,因为系统可以随时杀死使用过的对象。在http://developer.android.com/reference/android/app/Activity.htmlhttp://developer.android.com/training/basics/activity-lifecycle/index.html处查看活动生命周期。

使用自定义Application类在活动和Activity.onResume()方法之间传递数据,以使用名为Activity的listview上的新数据。

如果您不知道如何制作它,请询问相同的示例代码。

Eddited(下面的代码)

创建自定义应用程序对象:

public class MyApplication extends Application {
    Object mData;

    public Object getData() {
        return mData;
    }
    public void setData(Object data) {
        mData = data;
    }

}

在清单中使用它:

 <application
        android:name="my.apppkg.MyApplication"

   ...
 </application>

并创建两个活动。 Form1作为“消费者”:

public class Form1 extends Activity {
    MyApplication mApp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mApp = (MyApplication)getApplication();

    }

    @Override
    protected void onResume() {
        super.onResume();

        Object data = mApp.getData();
        if (data != null) {

            // Do whatever you want with the data

        } else {
            // No data
        }

    }
    ...
}

和Form2作为“生产者”:

public class Form2 extends Activity {
    MyApplication mApp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mApp = (MyApplication)getApplication();

    }

    ...
    /**
     * Data generation method. Calls to Form1 once the data is set.
     * Call it here or from wherever you need it to (Buttons onClick events, ListViews onItemListClick, etc)
     */
    private void processAndSendData() {
        Object newData = ...; // Get the data from your sources

        mApp.setData(newData);

        Intent intent = new Intent(this, Form1.class);
        this.startActivity(intent);
    }
}

当然,根据需要更改MyApplication类(示例中的Object)中的mData类型。