gpt4 book ai didi

android - 尝试在空对象引用上调用虚拟方法 'void android.os.Bundle.putString(java.lang.String, java.lang.String)'

转载 作者:行者123 更新时间:2023-11-29 19:53:22 24 4
gpt4 key购买 nike

我正在 Android Studio 中创建一个基于健康的抽屉导航应用程序,并希望使用谷歌驱动器来存储一些用户详细信息。我经历了设置应用程序以连接到驱动器 api 的繁琐过程,并使用了谷歌示例来写出到应用程序文件夹。我希望将信息打包,然后写入驱动器。但是,当我点击按钮调用保存方法时,我不断收到相同的空指针异常。

主要 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;
}

在应用程序文件夹 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());
}
};
}

帐户 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");
}

日志

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)

最佳答案

作为 userDetails 包的显示 NPE,因为您从未初始化它

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
userDetails = new Bundle();
//your other code
}

关于android - 尝试在空对象引用上调用虚拟方法 'void android.os.Bundle.putString(java.lang.String, java.lang.String)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36914113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com