-1

我有一個片段中的按鈕。片段ButtonClick空指針異常

<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" /> 

而且methid 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是分段定義爲

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

請幫助TextView的。謝謝: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(); 
    } 



} 
+0

你能張貼布局 – Sripathi

+0

貼整個片段。看看 –

回答

2

嘗試膨脹佈局,找到你的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 
+0

我不能讓onClickListener工作。 –

+0

它給我錯誤.. button.setOnClickListener(this); - 不能接受價值片段 –

+0

我加了一些代碼。 – Pedram