【问题标题】:Fragment ButtonClick NullPointerException片段按钮单击 NullPointerException
【发布时间】:2014-05-17 07:05:36
【问题描述】:

我在片段中有一个按钮。

<Button
        android:id="@+id/set_reminder"
        style="@style/CustomStyleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="169dp"
        android:layout_y="312dp"
        android:onClick="onClick1"
        android:text="Set Reminder" />

并且方法onClick1在MainActivity中如下-

Fragment newfrag = new FlightDetailsFragment();

    public void onClick1(View v) throws ParseException 
    {

        ((FlightDetailsFragment) newfrag).clicked();


    }

并且点击的方法在按钮所在的FlightDetailsFragment中-

public void clicked()
    {
    String s1 = t7.getText().toString();
    }

没有编译错误,但是当我运行它时,我在该行得到一个 NullPointerException -

String s1 = t7.getText().toString();

这里 t7 是在 Fragment 中定义的 textview

t7 = (TextView) rootView.findViewById(R.id.departure_date);

请帮忙。谢谢:D

这是整个片段 -

public class FlightDetailsFragment extends Fragment {

    public FlightDetailsFragment(){}
     String sessionflight;
    TextView t1;
    TextView t2;
    TextView t3;
    TextView t4;
    TextView t5;
    TextView t6;
    TextView t7;
    TextView t8;
    TextView t9;
    TextView t10;
    Button b1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_flightdetails, container, false);

        SharedPreferences pref = getActivity().getSharedPreferences("AndroidHivePref", 0);
        sessionflight = pref.getString(SessionManager.KEY_EMAIL,null);
        t1 = (TextView) rootView.findViewById(R.id.passenger_name);
        t2 = (TextView) rootView.findViewById(R.id.flight_number);
        t3 = (TextView) rootView.findViewById(R.id.aircraft_number);
        t4 = (TextView) rootView.findViewById(R.id.deaparture_airport);
        t5 = (TextView) rootView.findViewById(R.id.departure_terminal);
        t6 = (TextView) rootView.findViewById(R.id.airline_name);
        t7 = (TextView) rootView.findViewById(R.id.departure_date);
        t8 = (TextView) rootView.findViewById(R.id.departure_time);
        t9 = (TextView) rootView.findViewById(R.id.txt_pnr);
        t10 = (TextView) rootView.findViewById(R.id.txt_ticketno);
        b1 = (Button) rootView.findViewById(R.id.set_reminder);


        new getFlightDetails().execute(new ApiConnector());

        return rootView;
    }


    private class getFlightDetails extends AsyncTask<ApiConnector,Long,JSONArray> {

        @Override
        protected JSONArray doInBackground(ApiConnector... params) {

            return params[0].getFlightDetails(sessionflight);

        }

        @Override
        protected void onPostExecute(JSONArray jsonArray) {
            // TODO Auto-generated method stub
            JSONObject customer ;
            try {
                customer = jsonArray.getJSONObject(0);
                String s1 = customer.getString("BKTPAXNAME");
                String s2 = customer.getString("BKTFLIGHT");
                String s3 = customer.getString("BKTAIRCRAFT");
                String s4 = customer.getString("BKTOFFAIRPORT");
                String s5 = customer.getString("BKTBOARDAIRPORT");
                String s6 = customer.getString("BKTAIRLINE");
                String s7 = customer.getString("BKTDEPDATE");
                String s8 = customer.getString("BKTAIRPNRNO");
                String s9 = customer.getString("BKTAIRTICKETNO");

                t1.setText(s1);
                t2.setText(s2);
                t3.setText(s3);
                if(s4 != null)
                {
                t4.setText(s4);
                }
                else
                {
                    t4.setText("NA");
                }
                if(t5 != null)
                {
                t5.setText(s5);
                }
                else
                {
                    t5.setText("NA");
                }
                t6.setText(s6);
                t7.setText(s7.substring(0,11));
                t8.setText(s7.substring(11));
                t9.setText(s8);
                t10.setText(s9);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }


    }




     public void clicked()
    {
    String s1 = t7.getText().toString();
    }



}

【问题讨论】:

  • 你能把版式发一下吗
  • 发布了整个片段。看看

标签: java android android-fragments android-fragmentactivity android-button


【解决方案1】:

尝试扩充布局并使用它找到您的 t7-view。 并在 XML 文件中为您的按钮设置 onClick 属性不是一个好方法,并且会使调试您的应用程序在 clicklistener 上的使用更加复杂......

编辑:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.your_t7_contain_layout, container, false);
        TextView tv = (TextView) view.findViewById(R.id.tv);
        Button btn = (Button) view.findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                              //your btn action
                    }
        });

        return view;
    }//End of onCreateView

【讨论】:

  • 我无法让 onClickListener 工作。
  • 它给了我错误.. button.setOnClickListener(this); -- 不能接受值作为片段
  • this 无法在片段中获取活动,请在每个需要引用 Fragments 中的活动的地方使用 getactivity() 而不是 this
猜你喜欢
  • 1970-01-01
  • 2015-12-20
  • 2018-02-18
  • 2014-07-28
  • 1970-01-01
  • 1970-01-01
  • 2016-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多