Reputation: 104
I am creating a health based nav drawer application in Android Studio, and want to use the google drive to store some user details. I have been through the rigmarole of setting the application up to connect to the drive api and have used the google exemplars to write out to the application folder. I wish to place the information in a bundle and then write out to the drive. However I keep getting the same null pointer exception, when I hit the button to call the save method.
Main Activity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
//**************************************************************************//
//API Handling //
//**************************************************************************//
public static GoogleApiClient mGoogleApiClient;
private File fileToSave;
public static final String TAG = "fitness_first";
private static final int REQUEST_CODE_RESOLUTION = 0;
private static final int REQUEST_CODE_DATA_SAVED = 1;
private static final int REQUEST_CODE_CREATOR = 2;
@Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
mGoogleApiClient.connect();
}
@Override
public void onConnectionSuspended(int cause) {
Log.i(TAG, "GoogleApiClient connection suspended");
}
@Override
public void onConnected(Bundle connectionHint) {
Log.i(TAG, "API client connected.");
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
if (!result.hasResolution()) {
GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
return;
}
try {
result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Exception while starting resolution activity", e);
}
}
@Override
protected void onPause() {
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
super.onPause();
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_CODE_DATA_SAVED:
// Called after a photo has been taken.
if (resultCode == Activity.RESULT_OK) {
// Store the image data as a bitmap for writing later.
fileToSave = (File) data.getExtras().get("data");
}
break;
case REQUEST_CODE_CREATOR:
// Called after a file is saved to Drive.
if (resultCode == RESULT_OK) {
Log.i(TAG, "Image successfully saved.");
fileToSave = null;
}
break;
}
}
//**************************************************************************//
//Interface Handling //
//**************************************************************************//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home_screen, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
//Add fragments
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = null;
AccountFragment fragment = null;
Bundle extraData = null;
int id = item.getItemId();
switch (id) {
case R.id.your_account:
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.relativeLayoutFragmentContainer, new AccountFragment());
fragmentTransaction.commit();
break;
case R.id.BMI:
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.relativeLayoutFragmentContainer, new BMIFragment());
fragmentTransaction.commit();
break;
case R.id.Weight_Loss:
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.relativeLayoutFragmentContainer, new WeightLossFragment());
fragmentTransaction.commit();
break;
case R.id.nav_send:
break;
case R.id.nav_share:
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Create File in App Folder Activity:
public class CreateFileInAppFolderActivity extends MainActivity {
@Override
public void onConnected(Bundle connectionHint) {
super.onConnected(connectionHint);
// create new contents resource
Drive.DriveApi.newDriveContents(mGoogleApiClient)
.setResultCallback(driveContentsCallback);
}
// [START drive_contents_callback]
final private ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback =
new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
Log.i(TAG, "Error while trying to create new file contents");
return;
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("appconfig.txt")
.setMimeType("text/plain")
.build();
Drive.DriveApi.getAppFolder(mGoogleApiClient)
.createFile(mGoogleApiClient, changeSet, result.getDriveContents())
.setResultCallback(fileCallback);
}
};
// [END drive_contents_callback]
final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new
ResultCallback<DriveFolder.DriveFileResult>() {
@Override
public void onResult(DriveFolder.DriveFileResult result) {
if (!result.getStatus().isSuccess()) {
Log.i(TAG,"Error while trying to create the file");
return;
}
Log.i(TAG,"Created a file in App Folder: "
+ result.getDriveFile().getDriveId());
}
};
}
Account Fragment:
public class AccountFragment extends Fragment implements View.OnClickListener{
public static String TitleKey = "sec_title";
private String activityName = "Account management";
private Button accountDetails;
private EditText accountName, accountUserName;
private Bundle userDetails;
private static final String NAME = "accountName";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.your_account, container, false);
accountDetails = (Button) rootView.findViewById(R.id.buttonSaveDetails);
accountName = (EditText) rootView.findViewById(R.id.editTextName);
accountUserName = (EditText) rootView.findViewById(R.id.editTextUsername);
accountDetails.setOnClickListener(this);
super.onCreateView(inflater, container, savedInstanceState);
return rootView;
}
public void onClick(View v){
userDetails.putString(NAME, accountName.getText().toString());
CreateFileInAppFolderActivity saveDetails = new CreateFileInAppFolderActivity();
saveDetails.onConnected(userDetails);
}
@Override
public void onPause() {
super.onPause();
Log.i(activityName, "Paused");
}
@Override
public void onResume() {
super.onResume();
Log.i(activityName, "Resumed");
}
@Override
public void onStop() {
super.onStop();
Log.i(activityName, "Stopped");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(activityName, "Destroyed");
}
}
Logcat
04-28 11:40:41.395 7023-7023/com.example.fitness_first.fitnessfirst
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fitness_first.fitnessfirst, PID: 7023 java.lang.NullPointerException:
Attempt to invoke virtual method 'void android.os.Bundle.putString(java.lang.String, java.lang.String)' on a null object reference at com.example.fitness_first.fitnessfirst.AccountFragment.onClick(AccountFragment.java:42)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Upvotes: 1
Views: 7077
Reputation: 9009
As its shwoing NPE for userDetails
bundle because you never initialized it.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
userDetails = new Bundle();
//your other code
}
Upvotes: 4