- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 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/
在几个 SO 的问题中,有这些行可以访问代码的父目录,例如os.path.join(os.path.dirname(__file__)) returns nothing和 os.path.join(o
我想用 Python 更改文件模式。 os 模块具有三个功能上看似相同的功能: os.chmod os.fchmod os.lchmod 这三个版本有什么区别? 最佳答案 chmod 用于更改路径指定
考虑: pipe_read, pipe_write = os.pipe() 现在,我想知道两件事: (1) 我有两个线程。如果我保证只有一个正在读取 os.read(pipe_read,n) 而另一个
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
让我们以硬盘驱动器/网络接口(interface)为例。它由多个进程共享。现在多个进程可能会向硬盘驱动器发出并发命令来读取数据。当数据可用时,内核如何知道哪个进程的数据已准备好?操作系统和硬盘驱动器之
嗨,我正在尝试编写像这样的原子写入函数...... with tempfile.NamedTemporaryFile(mode= "w", dir= target_directory) as f:
net.Conn接口(interface)提供了 SetTimeout 方法,我应该用 os.Timeout 检查返回的错误.但是我看不到在返回的 os.Error 上调用 os.Timeout 的方
我正在使用 os 模块在我的 Django 项目 settings.py 文件中具有相对路径。变量 SITE_ROOT 设置为 settings.py 文件的当前工作目录,然后用于引用同样位于同一目录
正如我们所知,Windows 接受 "\" 和 "/" 作为分隔符。但是在python中,使用的是"\"。例如,调用 os.path.join("foo","bar"),将返回 'foo\\bar'。
我有以下工作目录:/Users/jordan/Coding/Employer/code_base ,我想要获取绝对路径的文件位于 /Users/jordan/Coding/Employer/code_
在 Python 中,如果路径中包含“~”,我能否确定扩展的用户调用将是绝对路径? 例如,这个表达式是否总是为真? path = '~/.my_app' os.path.expanduser(path
我是 Django 项目的初学者。Django 项目的 settings.py 文件包含这两行: BASE_DIR = os.path.dirname(os.path.dirname(os.path.
我有一个旧 MAC OS 文件存储中的文件集合。我知道集合存在文件名/路径名问题。问题源于我认为在原始操作系统中呈现为破折号的路径中包含一个代码点,但 Windows 与代码点斗争,并且其中一个包含
Ubuntu怎么安装mac os x主题呢?下文小编将为大家分享ubuntu14.04安装mac os x主题教程,安装MAC OS X&
我有一个 Firefox OS 应用程序,我希望在该应用程序之外打开一个链接(该链接指向不同的站点,在应用程序中打开它会使应用程序在没有强制的情况下无法使用)。我怎么做? Related bug re
我想为 Firefox OS 编写我的应用程序.使用什么样的语言(如 Android 的 Java 和 iOS 的 Objective C++)和工具(如 Eclipse、Xcode)? 最佳答案 适
我正在尝试创建一个 Palm OS 应用程序,以每 X 分钟或几小时检查一次网站,并在有数据可用时提供通知。我知道这种事情可以在新的 Palm 上完成——例如,当应用程序不在顶部时,我的 Centro
我需要在 Firefox OS 中显示全屏图像。我有一个具有 qHD 分辨率(960x540 像素)的“峰值”开发预览手机。 如何确保我的应用程序在其他具有不同屏幕分辨率的 firefox-os 设备
我正在尝试在 Firefox OS 中安装一个新的语言环境,但我不确定我是否正确地按照这些步骤操作。 首先,我尝试使用 Mercurial 下载所需的语言环境:它对我不起作用,Mercurial 说访
我有这个shell脚本Test.sh: #! /bin/bash FILE_TO_CHECK="/Users/test/start.txt" EXIT=0 while [ $EXIT -eq 0 ];
我是一名优秀的程序员,十分优秀!