【发布时间】: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