2016-04-28 61 views
1

我在Android Studio中創建基於健康的導航抽屜應用程序,並且想要使用谷歌驅動器來存儲一些用戶的詳細信息。我已經完成了將應用程序設置爲連接到驅動器API的艱難工作,並使用谷歌示例寫入應用程序文件夾。我希望將這些信息放在一個包中,然後寫入驅動器。但是,當我點擊按鈕來調用save方法時,我總是收到相同的空指針異常。嘗試在空對象引用上調用虛擬方法'void android.os.Bundle.putString(java.lang.String,java.lang.String)'

主要活動:

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

創建app文件夾活動文件:

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) 
+0

在frgment xml文件中定義的onClick()方法不起作用。你必須通過java代碼 –

+0

@VivekMishra @VivekMishra我已經添加了以下到xml的按鈕,但仍然是相同的錯誤: android:onClick =「onConnected」 –

+0

我說的是從XML中刪除它,並通過調用它通過java –

回答

2

作爲其shwoing NPE對userDetails束因爲你永遠不初始化它

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

happyCoding;

相關問題