Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 片段按钮单击NullPointerException_Java_Android_Android Fragments_Android Fragmentactivity_Android Button - Fatal编程技术网

Java 片段按钮单击NullPointerException

Java 片段按钮单击NullPointerException,java,android,android-fragments,android-fragmentactivity,android-button,Java,Android,Android Fragments,Android Fragmentactivity,Android Button,我有一个按钮碎片 <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="31

我有一个按钮碎片

<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" />
单击的方法位于FlightDetails片段中,按钮位于该片段中-

public void clicked()
    {
    String s1 = t7.getText().toString();
    }
没有编译错误,但是当我运行它时,我在第行得到一个NullPointerException-

String s1 = t7.getText().toString();
这里t7是片段中定义的文本视图

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



}
公共类FlightDetailsFragment扩展片段{
公共FlightDetailsFragment(){}
字符串会话飞行;
文本视图t1;
文本视图t2;
文本视图t3;
TextView t4;
TextView t5;
文本视图t6;
TextView t7;
TextView t8;
文本视图t9;
文本视图t10;
按钮b1;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment\u flightdetails,container,false);
SharedReferences pref=getActivity().GetSharedReferences(“AndroidHivePref”,0);
sessionflight=pref.getString(SessionManager.KEY_电子邮件,null);
t1=(TextView)rootView.findViewById(R.id.passenger\u name);
t2=(TextView)rootView.findViewById(R.id.航班号);
t3=(TextView)rootView.findViewById(R.id.Aircrafter\U编号);
t4=(TextView)rootView.findViewById(R.id.departure_airport);
t5=(TextView)rootView.findViewById(R.id.Exhibit\U终端);
t6=(TextView)rootView.findViewById(R.id.airline\u name);
t7=(TextView)rootView.findViewById(R.id.execution\u date);
t8=(TextView)rootView.findViewById(R.id.execution\u time);
t9=(TextView)rootView.findViewById(R.id.txt\u pnr);
t10=(TextView)rootView.findViewById(R.id.txt\u ticketno);
b1=(按钮)rootView.findViewById(R.id.set\u提醒);
新建getFlightDetails().execute(新建ApiConnector());
返回rootView;
}
私有类getFlightDetails扩展了AsyncTask{
@凌驾
受保护的JSONArray doInBackground(ApiConnector…参数){
返回参数[0]。getFlightDetails(sessionflight);
}
@凌驾
受保护的void onPostExecute(JSONArray JSONArray){
//TODO自动生成的方法存根
JSONObject客户;
试一试{
customer=jsonArray.getJSONObject(0);
字符串s1=customer.getString(“BKTPAXNAME”);
字符串s2=customer.getString(“BKTFLIGHT”);
字符串s3=customer.getString(“BKTAIRCRAFT”);
字符串s4=customer.getString(“BKTOFFAIRPORT”);
String s5=customer.getString(“BKTBOARDAIRPORT”);
字符串s6=customer.getString(“bktailline”);
字符串s7=customer.getString(“BKTDEPDATE”);
String s8=customer.getString(“BKTAIRPNRNO”);
字符串s9=customer.getString(“BKTAIRTICKETNO”);
t1.setText(s1);
t2.setText(s2);
t3.setText(s3);
如果(s4!=null)
{
t4.塞特xt(s4);
}
其他的
{
t4.setText(“NA”);
}
如果(t5!=null)
{
t5.setText(s5);
}
其他的
{
t5.setText(“NA”);
}
t6.setText(s6);
t7.setText(s7.子串(0,11));
t8.setText(s7.substring(11));
t9.setText(s8);
t10.setText(s9);
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
已单击的公共无效()
{
字符串s1=t7.getText().toString();
}
}

尝试对布局进行充气,然后用它找到t7视图。 在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工作。这给了我一个错误。。按钮。setOnClickListener(此);--无法接受作为片段的值
无法获取片段中的活动,请在需要引用
片段中活动的每个位置使用
getactivity()
,而不是
@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