片段页面内的导航

时间:2013-12-20 06:42:51

标签: android json android-fragments fragmentmanager

在Home.java活动中扩展Fragment,我使用两个按钮自定义Listview。所以我想在单击按钮时移动下一个活动。我使用Fragement和FragmentMangar代码。但它不起作用。它给出了NullpointerException的错误。我也试过Intent并发现了许多想法,但没有解决我的问题。

Home.java

 public class Home extends Fragment
    {
        StrictMode.ThreadPolicy thmod = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        JSONParser jParser = new JSONParser();
         private Handler handler;
        private ListView home_lst;
        private TextView home_txt_dt;

        // JSON Node names
         static final String TAG_SUCCESS = "success";
         static final String TAG_PRODUCTS = "customerdetail";
          static final String TAG_PID = "CId";
         static final String TAG_NAME = "Cname";
         public static final String TAG_BNAME = "Cbusinessname";
         public static final String TAG_Address = "Caddress";
         public static final String TAG_PHONE = "Cphone";
         public static final String TAG_TIME = "Cordertime";

        View rootview;
        private ArrayList<HashMap<String, String>> productsList=new ArrayList<HashMap<String,String>>();
        ArrayList<String> ali = new ArrayList<String>();
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
        {
             rootview = inflater.inflate(R.layout.home, container,false);
             StrictMode.setThreadPolicy(thmod);
            Toast.makeText(getActivity(), "Home", Toast.LENGTH_LONG).show();
            getActivity().setTitle("Home");
            initialize();

            home_btn_setting.setOnClickListener(this);
            home_btn_porder.setOnClickListener(this);
            home_btn_help.setOnClickListener(this);
        //  home_lst.setOnItemClickListener(this);   
                // Loading products in Background Thread
                new LoadAllProducts().execute();

            return rootview;
        }


        private void initialize() {
            // TODO Auto-generated method stub
            home_lst=(ListView)rootview.findViewById(R.id.home_lst);
            home_txt_dt=(TextView)rootview.findViewById(R.id.home_dt);

        }

        public class LoadAllProducts extends AsyncTask <String,String,String>{

            private ProgressDialog pDialog;
            private Ladapter ladapter;

            /*protected Object doInBackground(Object... arg0) {
                // TODO Auto-generated method stub
                return null;
            }*/
            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                 pDialog = new ProgressDialog(getActivity());
                 pDialog.setMessage("Loading products. Please wait...");
                 pDialog.setIndeterminate(false);
                 pDialog.setCancelable(false);
                 pDialog.show();
            }

            @Override
            protected String doInBackground(String... args) {
                // TODO Auto-generated method stub
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                // getting JSON string from URL
                JSONObject json = jParser.makeHttpRequest( "url", "GET", params);
                // Check your log cat for JSON reponse

                try {
                    // Checking for SUCCESS TAG
                    int success = json.getInt(TAG_SUCCESS);
                    System.out.println("Success"+success);
                    if (success == 1) {
                        // products found
                        // Getting Array of Products
                      JSONArray  products = json.getJSONArray(TAG_PRODUCTS);

                        // looping through All Products
                        for (int i = 0; i < products.length(); i++) 
                        {
                            System.out.println(i);
                            JSONObject c = products.getJSONObject(i);

                            String CId = c.getString(TAG_PID); 
                            String Cname = c.getString(TAG_NAME);
                            String Cbusinessname=c.getString(TAG_BNAME);
                            String Caddress = c.getString(TAG_Address);
                            String Cphone = c.getString(TAG_PHONE);
                            String Cordertime=c.getString(TAG_TIME);
                            HashMap<String, String> map = new HashMap<String, String>();
                            ali.add(TAG_PID);
                            map.put(TAG_PID,"OrderId : "+CId);
                            map.put(TAG_TIME, Cordertime);
                            productsList.add(map);
                        }
                        System.out.println(productsList.size());
                    }
                    else {
                        // no products found
                        // Launch Add New product Activity
                        System.out.println("There is no data");
                       }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return json.toString();
            }
            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
                 pDialog.dismiss();
                // updating UI from Background Thread

                     try{
                            // updating listview
                         ladapter = new Ladapter(getActivity(),R.layout.listitem,productsList);
                           home_lst.setAdapter(ladapter);
                     }
                        catch(Exception e)
                        {
                            e.printStackTrace();
                        }
            }
        }
        public void transpactivity(Fragment fragment1) 
        {

               System.out.println("test activity :"+fragment1);
               FragmentManager fragmentmanager = getFragmentManager();
               FragmentTransaction ft = fragmentmanager.beginTransaction();
               ft.replace(R.id.content_frame,fragment1).commit();
               Intent ine = new Intent(Intent.ACTION_VIEW);     
          System.out.println("test activity :"+fragment1);
        }
    }

Ladapter.java

  public class Ladapter extends BaseAdapter {
         Fragment fragment=null;
        Context context;
        int layoutid;
        ArrayList<HashMap<String, String>> al = new ArrayList<HashMap<String,String>>();
        public Ladapter(Context context, int layoutid,ArrayList<HashMap<String, String>> al) 
        {
            this.context=context;
            this.layoutid=layoutid;
            this.al = al;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return al.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return al.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {

            Listholder lh=null;
            if(convertView==null)
            {
                LayoutInflater infltate = ((Activity) context).getLayoutInflater();
                convertView=infltate.inflate(layoutid, parent,false);
                lh = new Listholder();
                lh.txt_oid = (TextView)convertView.findViewById(R.id.lst_txt_orderid);
                lh.txt_time=(TextView)convertView.findViewById(R.id.lst_txt_time);
                lh.img_cle=(ImageView)convertView.findViewById(R.id.lst_img_cle);
                lh.img_ok=(ImageView)convertView.findViewById(R.id.lst_img_ok);
                convertView.setTag(lh);
            }
            else
            {
                lh=(Listholder) convertView.getTag();
            }
            HashMap<String, String> hst = new HashMap<String, String>();
            hst=al.get(position);
            lh.txt_oid.setText(hst.get(Home.TAG_PID));
            lh.txt_time.setText("Received @ "+hst.get(Home.TAG_TIME));

            //Click event of Button cancle and ok

            lh.img_cle.setOnClickListener(new View.OnClickListener() {  //Cancel Button click event

                @SuppressLint("NewApi")
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    System.out.println("Cancel button is click");
                    fragment = new CancelOrder();
                    nextactivity();             
                }
            });
            lh.img_ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    fragment = new OrderTime();
                    nextactivity();
                }
            });
            return convertView;
        }

        class Listholder
        {

            public ImageView img_ok;
            public ImageView img_cle;
            public TextView txt_time;
            public TextView txt_oid;

        }

        protected void nextactivity() {
            // TODO Auto-generated method stub
            if(fragment!=null)
            {
                Home hm = new Home();
                hm.transpactivity(fragment);
            }
            else
            {
                Toast.makeText(context, "There is no file", Toast.LENGTH_LONG).show();
            }

        }
    }

错误

12-20 11:31:40.947: E/AndroidRuntime(1425): FATAL EXCEPTION: main
12-20 11:31:40.947: E/AndroidRuntime(1425): java.lang.NullPointerException
12-20 11:31:40.947: E/AndroidRuntime(1425):     at android.content.ComponentName.<init>(ComponentName.java:75)
12-20 11:31:40.947: E/AndroidRuntime(1425):     at android.content.Intent.<init>(Intent.java:3004)
12-20 11:31:40.947: E/AndroidRuntime(1425):     at com.example.bluetoothapp.Home.transpactivity(Home.java:214)
12-20 11:31:40.947: E/AndroidRuntime(1425):     at com.example.bluetoothapp.Ladapter.nextactivity(Ladapter.java:127)
12-20 11:31:40.947: E/AndroidRuntime(1425):     at com.example.bluetoothapp.Ladapter$1.onClick(Ladapter.java:90)
12-20 11:31:40.947: E/AndroidRuntime(1425):     at android.view.View.performClick(View.java:3480)
12-20 11:31:40.947: E/AndroidRuntime(1425):     at android.view.View$PerformClick.run(View.java:13983)
12-20 11:31:40.947: E/AndroidRuntime(1425):     at android.os.Handler.handleCallback(Handler.java:605)
12-20 11:31:40.947: E/AndroidRuntime(1425):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-20 11:31:40.947: E/AndroidRuntime(1425):     at android.os.Looper.loop(Looper.java:137)
12-20 11:31:40.947: E/AndroidRuntime(1425):     at android.app.ActivityThread.main(ActivityThread.java:4340)
12-20 11:31:40.947: E/AndroidRuntime(1425):     at java.lang.reflect.Method.invokeNative(Native Method)
12-20 11:31:40.947: E/AndroidRuntime(1425):     at java.lang.reflect.Method.invoke(Method.java:511)
12-20 11:31:40.947: E/AndroidRuntime(1425):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-20 11:31:40.947: E/AndroidRuntime(1425):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-20 11:31:40.947: E/AndroidRuntime(1425):     at dalvik.system.NativeStart.main(Native Method)

Ladapter活动中,当单击取消按钮或确定指针转到nextactivity()方法然后Home.java activity.Transpactivity()方法的Home.java活动时,我收到错误NullPointerException在begintranscation()。repalce()。commit;

抱歉,我无法发布网址。

布局Home.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/wallpaper7">

    <TextView
        android:id="@+id/home_nm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:text="@string/home_name"
        android:textColor="#ffffff"
        android:textSize="@dimen/text_size" />

    <TextView
        android:id="@+id/home_dt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/home_nm"
        android:layout_alignParentRight="true"
        android:layout_marginRight="22dp"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:text="@string/home_dt"
        android:textSize="@dimen/text_size" />

    <TextView
        android:id="@+id/home_ins"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/home_nm"
        android:layout_marginTop="10dp"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:text="@string/home_btn_instruction"
        android:textSize="@dimen/text_instruction"
        android:textStyle="italic" />

     <Button
        android:id="@+id/home_btn_setting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/home_ins"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginTop="7dp"
        android:text="@string/home_btn_nm1" 
        android:textSize="@dimen/home_btn_size"/>

      <Button
          android:id="@+id/home_btn_help"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignBaseline="@+id/home_btn_pastorder"
          android:layout_alignBottom="@+id/home_btn_pastorder"
          android:layout_marginLeft="19dp"
          android:layout_toRightOf="@+id/home_btn_pastorder"
          android:text="@string/home_btn_nm3"
          android:textSize="@dimen/home_btn_size" />

    <TextView
        android:id="@+id/home_order"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/home_btn_setting"
        android:layout_marginTop="5dp"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:text="@string/home_new_order"
        android:textSize="@dimen/text_instruction"
        android:textStyle="bold" />

    <View
        android:id="@+id/home_view"
        android:layout_width="fill_parent"
        android:layout_height="3dp"
        android:layout_below="@+id/home_order"
        android:background="#333333" />

        <ListView
            android:id="@+id/home_lst"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/home_view"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:layout_marginTop="20dp"
            android:background="#ffffff" />

     <Button
         android:id="@+id/home_btn_pastorder"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignBaseline="@+id/home_btn_setting"
         android:layout_alignBottom="@+id/home_btn_setting"
         android:layout_centerHorizontal="true"
         android:paddingLeft="5dp"
         android:paddingRight="5dp"
         android:text="@string/home_btn_nm2"
         android:textSize="@dimen/home_btn_size" />

</RelativeLayout>

Ladapter布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_margin="@dimen/activity_horizontal_margin">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginRight="20dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lst_txt_orderid"
        android:text="Order Id"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lst_txt_time"
        android:text="name"/>
</LinearLayout>
<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/cancel1_mini"
        android:layout_marginRight="40dp"
        android:id="@+id/lst_img_cle"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ok1_mini"
        android:layout_gravity="end"
        android:id="@+id/lst_img_ok"/>
    </LinearLayout>

1 个答案:

答案 0 :(得分:0)

请尝试使用getChildFragmentManager()。 例如:

public void transpactivity(Fragment fragment1) 
        {

               System.out.println("test activity :"+fragment1);
               FragmentManager fragmentmanager = getChildFragmentManager();
               FragmentTransaction ft = fragmentmanager.beginTransaction();
               ft.replace(R.id.content_frame,fragment1).commit();
               Intent ine = new Intent(Intent.ACTION_VIEW);     
          System.out.println("test activity :"+fragment1);
        }

替换片段内的片段需要getChildFragmentManager()管理器。