使用片段时出现空指针错误

时间:2014-04-16 04:15:08

标签: android android-layout android-intent android-fragments

在我们尝试添加第二页之前,GPS类和MainActivity都正常工作。我们已经尝试过使用意图和片段,并花费了太多时间/天来试图解决这个问题。

我们在NullPointerException onClickListener()以及MainActivity类中更新任何textview时,不断获得GPS秒。我们知道它与片段的工作方式有关,但我们不确定如何解决这个问题。

这是我们的MainActivity类

public class MainActivity extends ActionBarActivity {

// Get buttons and text fields
public Button showLocation;

// TextViews
public static TextView showLatitude;
public static TextView showLongitude;  
public static TextView showDistance;

// GPS class
GPS gps;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
        }


    showLocation = (Button) findViewById(R.id.btnShowLocation);
    showLatitude = (TextView) findViewById(R.id.txtLatitude);
    showLongitude = (TextView) findViewById(R.id.txtLongitude);
    showDistance = (TextView) findViewById(R.id.txtDisplayDistance);

    // Create gps
    gps = new GPS(MainActivity.this);

    // show location button click event
    showLocation.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {       
            gps.setTotalDistance(0.0);
        }
    });//END LISTENER  


}// END ONCREATE

public void nextPage(View view) {

    // In response to button
    Intent intent = new Intent(this, ResourcePage.class);

    // Set activity intent
    startActivity(intent);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;

    }
  }  
}

这是GPS课程

public class GPS extends Service implements LocationListener {

private final Context mContext;

// Flag for GPS/Network/Location status
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;

// Location Class
Location location; 

// Latitude and longitude save points
private double startLatitude = 0;
private double endLatitude = 1.0; 
private double startLongitude = 0;
private double endLongitude = 1.0;
private double totalDistance = 0;

// Results Array
private float[] results = new float[1];


// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_FOR_UPDATES = 10; // meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BETWEEN_UPDATES = 1000; // 1 second

// Location Manager
protected LocationManager locationManager;


// Interface
public GPS(Context context) {
    this.mContext = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

        // Getting GPS status
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // Getting network status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


        // Checking Flags
        if (!isGPSEnabled && !isNetworkEnabled) {
            // ^no network provider is enabled do nothing/go on^
        } 
        else {
            this.canGetLocation = true;

            // First get location from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BETWEEN_UPDATES, MIN_DISTANCE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);


                    if (location != null) {
                        startLatitude = location.getLatitude();
                        startLongitude = location.getLongitude();
                    } //END OF LAST IF BLOCK
                } //END OF SECOND IF BLOCK
            } //END OF "FIRST" IF BLOCK


            // if GPS Enabled get latitude/longitude using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BETWEEN_UPDATES, MIN_DISTANCE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            startLatitude = location.getLatitude();
                            startLongitude = location.getLongitude();
                        }
                    }
                }
            }
        }//END OF ELSE BLOCK

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

// Call to remove updates from listener
public void stopUsingGPS(){
    if(locationManager != null){
        locationManager.removeUpdates(GPS.this);
    }      
}

public double getLatitude(){
    if(location != null){
        startLatitude = location.getLatitude();
    }

    // Return latitude
    return startLatitude;
}

public double getLongitude(){
    if(location != null){
        startLongitude = location.getLongitude();
    }

    // Return longitude
    return startLongitude;
}

//Function to check GPS/wifi are enabled
public boolean canGetLocation() {
     //return boolean
    return this.canGetLocation;
}


// Function to show settings alert dialog if gps is off
public void showSettingsAlert(){

     // On pressing Settings button will launch Settings Options
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle("GPS settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // Settings button listener/press
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
        }
    });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}


// Called from listener on gps change of X
@Override
public void onLocationChanged(Location location) {

    //Display Latitude and Longitude
    if(location != null){
        startLatitude = location.getLatitude();
        startLongitude = location.getLongitude();

    MainActivity.showLatitude.setText(String.valueOf(startLatitude));
    MainActivity.showLongitude.setText(String.valueOf(startLongitude));

    }

    // Get distance between
    Location.distanceBetween(startLatitude, startLongitude, endLatitude,endLongitude,  results);

    // Convert to miles Add distance to total
    totalDistance = totalDistance + results[0] * 0.000621371192;

    // Display the total
    MainActivity.showDistance.setText(String.format("%.2f", totalDistance));

    // Move old values to End locations
    endLatitude = startLatitude;
    endLongitude = startLongitude;  

}

// Function to clear total distance (Testing)
public void setTotalDistance(Double savedData){

    totalDistance = savedData; 
    MainActivity.showDistance.setText(String.valueOf(totalDistance));
}

public double getTotalDistance(){

    return totalDistance;
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
}

对于layout文件,我们activity_main.xml为空fragment_main.xml,其中包含我们的所有文本字段和button s,然后fragment_resource.xml没有任何内容但是textview所以我们知道事情会出现在它上面。

3 个答案:

答案 0 :(得分:1)

如果您说您的所有视图都属于该片段,则初始化片段中的视图而不是Activity。

活动布局没有视图,findViewById在当前的膨胀布局中查找视图。因此,当您使用视图对象时,初始化将导致NullPointerException失败。

   TextView showLatitude;
   TextView showLongitude;  
   TextView showDistance;
   GPS gps;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
         showLocation = (Button)rootView.findViewById(R.id.btnShowLocation);
         showLatitude = (TextView) rootView.findViewById(R.id.txtLatitude);
         showLongitude = (TextView) rootView.findViewById(R.id.txtLongitude);
         showDistance = (TextView) rootView.findViewById(R.id.txtDisplayDistance);
         gps = new GPS(getActivity()); // wrong bind the service to the activtiy

         showLocation.setOnClickListener(new View.OnClickListener() {

             @Override
             public void onClick(View arg0) {       
              gps.setTotalDistance(0.0); // remove this
              }
        });
        return rootView;

    }

你也有

public class GPS extends Service 

它是一个服务类。您需要将服务绑定到Activity。但你做gps = new GPS(getActivity());

答案 1 :(得分:0)

替换它:

setContentView(R.layout.activity_main);

人:

setContentView(R.layout.fragment_main);

答案 2 :(得分:0)

  

你有这个方法的问题

public void setTotalDistance(Double savedData){
    totalDistance = savedData; 
    MainActivity.showDistance.setText(String.valueOf(totalDistance));
}
  

你不能只调用它会返回NULL的MainActivity   showDistance变量的指针异常。因为它不是静态的   所以你想这样做。

public void setTotalDistance(Double savedData){

    totalDistance = savedData; 
    ((this.mContext)MainActivity).showDistance.setText(String.valueOf(totalDistance));
}

这肯定会有所帮助。