更改片段中的Textview文本

时间:2016-11-30 17:12:40

标签: android android-fragments

我在android中创建我的第一个应用程序,我使用抽屉布局和服务获取当前位置并将数据发送到Broadacast接收器,这个应该在Dashboard Fragment中更新纬度和经度textview,但是当updateDashLocation调用我收到此错误:引起:java.lang.NullPointerException:尝试调用虚方法' void android.widget.TextView.setText(java.lang.CharSequence)'在DashLatitude.setText(纬度)的空对象引用上;和DashLongitude.setText(经度);

片段代码

public class DashboardFragment extends Fragment {


public DashboardFragment() {
    // Required empty public constructor
}

View dashboardInflatedView;
TextView DashLatitude,DashLongitude;
String TAG = "GPSTracker";

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

    Toolbar toolbar = ((MainActivity)getActivity()).toolbar;
    toolbar.setTitle("Dashboard");

    dashboardInflatedView = inflater.inflate(R.layout.fragment_dashboard, container, false);
    DashLatitude = (TextView)dashboardInflatedView.findViewById(R.id.DashLatitude);
    DashLongitude = (TextView)dashboardInflatedView.findViewById(R.id.DashLongitude);

    return dashboardInflatedView;
}

public void updateDashLocation(String latitude, String longitude){

    //Log.d(TAG, "Update Dashboard Location");
    DashLatitude.setText(latitude);
    DashLongitude.setText(longitude);
}

}

如何在调用updateDashLocation时访问Dashboard片段的元素?

2 个答案:

答案 0 :(得分:0)

在调用updateDashLocation方法时尝试声明TextViews?

    public class DashboardFragment extends Fragment {

        public DashboardFragment() {
            // Required empty public constructor
        }

        View dashboardInflatedView;
        TextView DashLatitude,DashLongitude;
        String TAG = "GPSTracker";

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

            Toolbar toolbar = ((MainActivity)getActivity()).toolbar;
            toolbar.setTitle("Dashboard");

            dashboardInflatedView = inflater.inflate(R.layout.fragment_dashboard, container, false);
            return dashboardInflatedView;
        }

        public void updateDashLocation(String latitude, String longitude){

            DashLatitude = (TextView)dashboardInflatedView.findViewById(R.id.DashLatitude);
            DashLongitude = (TextView)dashboardInflatedView.findViewById(R.id.DashLongitude);

            //Log.d(TAG, "Update Dashboard Location");
            DashLatitude.setText(latitude);
            DashLongitude.setText(longitude);
        }

答案 1 :(得分:0)

我找到了解决方案,问题出在主要活动中,现在在广播接收器中,当坐标已经通过时,代码是:

// Define the callback for what to do when data is received
private BroadcastReceiver locationReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d(TAG, "Broadcastreceiver Coordinates");

        int resultCode = intent.getIntExtra("resultCode", RESULT_CANCELED);
        if (resultCode == RESULT_OK) {
            Double latitude= intent.getDoubleExtra("latitude",0);
            Double longitude= intent.getDoubleExtra("longitude",0);

            DashboardFragment dashboardFrag  = (DashboardFragment)
                    manager.findFragmentByTag("Dashboard");
            dashboardFrag .updateLocation(latitude.toString(),longitude.toString());
            Log.d(TAG, latitude.toString()+" --- "+longitude.toString());
        }
    }
};

我通过Tag获取片段并访问方法