Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试调用虚拟方法';void android.os.Bundle.putString(java.lang.String,java.lang.String)和#x27;关于空对象引用_Android_Nullpointerexception - Fatal编程技术网

尝试调用虚拟方法';void android.os.Bundle.putString(java.lang.String,java.lang.String)和#x27;关于空对象引用

尝试调用虚拟方法';void android.os.Bundle.putString(java.lang.String,java.lang.String)和#x27;关于空对象引用,android,nullpointerexception,Android,Nullpointerexception,我正在Android Studio中创建一个基于健康的导航抽屉应用程序,并希望使用google drive来存储一些用户详细信息。我经历了将应用程序设置为连接到驱动器api的繁琐过程,并使用google示例将其写入应用程序文件夹。我希望将信息放在一个包中,然后将其写入驱动器。然而,当我点击按钮调用save方法时,我不断得到相同的空指针异常 主要活动: public class MainActivity extends AppCompatActivity implements Nav

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

主要活动:

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;
}
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());
            }
        };
}
 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");
}
在应用程序文件夹中创建文件活动:

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;
}
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());
            }
        };
}
 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)

作为
userDetails
bundle的shwoing NPE,因为您从未初始化它


作为
userDetails
bundle的shwoing NPE,因为您从未初始化它


在片段xml文件中定义的onClick()方法无效。您必须通过java进行设置code@VivekMishra我已经为按钮在xml中添加了以下内容,但仍然存在相同的错误:android:onClick=“onConnected”我是说从xml中删除它并通过java@VivekMishra我误解了。但是,它在第一个实例中不在xml中。我上面提供的代码不包含任何xml,是我在活动中使用的java代码。希望这能澄清在frgment xml文件中定义的.onClick()方法不起作用。您必须通过java进行设置code@VivekMishra我已经为按钮在xml中添加了以下内容,但仍然存在相同的错误:android:onClick=“onConnected”我是说从xml中删除它并通过java@VivekMishra我误解了。但是,它在第一个实例中不在xml中。我上面提供的代码不包含任何xml,是我在活动中使用的java代码。希望这能澄清。