0

我想问..我有 2 个表格,说它是表格 1 和表格 2,我在表格 1 上有列表视图,我想从表格 2 设置数据..所以我只是创建一些虚拟项目来尝试它,我想知道为什么不能工作..

所以这是我在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 

任何帮助将不胜感激谢谢...

4

1 回答 1

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上的 Activity 生命周期。

使用自定义应用程序类在活动之间传递数据,并使用 Activity.onResume() 方法在被调用活动的列表视图上使用新数据。

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

已编辑(下面的代码)

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

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

Of course, change the type of mData in MyApplication class (Object in the example) for whatever you need.

于 2012-10-21T22:14:17.870 回答