gpt4 book ai didi

java - Android:无法使用 .getStringExtra 获取值

转载 作者:行者123 更新时间:2023-11-30 01:46:13 26 4
gpt4 key购买 nike

我直接进入问题。在 UploadNotesActivity.java....

首先,我使用 intent 选择一个 .pdf 文件

chooseNotesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
// Start the Intent
startActivityForResult(intent, RESULT_LOAD_FILE);
}
});

然后,在 `onActivityResult 中,我保存所选文件的文件路径。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_FILE && resultCode == RESULT_OK && data != null) {
data.putExtra("filePath", data.getData().getPath());
choosenFile.setText(data.getStringExtra("filePath"));
} else {
Toast.makeText(this, "Error in choosing file",
Toast.LENGTH_LONG).show();
}
}

点击上传按钮开始上传文件

 uploadNotesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onUploadButtonClick();
}
});

onUploadButtonClick()

private void onUploadButtonClick() {
final String serverUrlString = "http://XXXX/uploadNotes.php";
if (getIntent().getStringExtra("filePath").isEmpty()) {
Log.d(TAG, "filePath is null");
} else {
Log.d(TAG, getIntent().getStringExtra("filePath"));
}
final String fileToUploadPath = getIntent().getStringExtra("filePath");
final String paramNameString = "uploaded_file";

String fileName[] = fileToUploadPath.split("/");

final MultipartUploadRequest request =
new MultipartUploadRequest(this, UUID.randomUUID().toString(), serverUrlString);

request.addFileToUpload(fileToUploadPath, paramNameString,
fileName[fileName.length-1]+".pdf", ContentType.APPLICATION_OCTET_STREAM);

request.setNotificationConfig(R.drawable.ic_launcher,
getString(R.string.app_name),
getString(R.string.uploading),
getString(R.string.upload_success),
getString(R.string.upload_error),
false);

// if you comment the following line, the system default user-agent will be used
request.setCustomUserAgent("UploadServiceDemo/1.0");

// set the intent to perform when the user taps on the upload notification.
// currently tested only with intents that launches an activity
// if you comment this line, no action will be performed when the user taps
// on the notification
// request.setNotificationClickIntent(new Intent(this, MainActivity.class));

// set the maximum number of automatic upload retries on error
request.setMaxRetries(2);

try {
request.startUpload();
} catch (Exception exc) {
Toast toast = Toast.makeText(getApplicationContext(), "Malformed upload request. " + exc.getLocalizedMessage(), Toast.LENGTH_LONG);
toast.show();
}
}

但问题是,它会抛出空指针异常,我不太明白原因。

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference
at com.fyp.mycyberlaw.Lecturer.UploadNotesActivity.onUploadButtonClick(UploadNotesActivity.java:73)
at com.fyp.mycyberlaw.Lecturer.UploadNotesActivity.access$100(UploadNotesActivity.java:19)
at com.fyp.mycyberlaw.Lecturer.UploadNotesActivity$2.onClick(UploadNotesActivity.java:53)

第 73 行:if (getIntent().getStringExtra("filePath").isEmpty())

第 19 行:public class UploadNotesActivity extends Activity

第 53 行:onUploadButtonClick();

第 73 行的 filePath 似乎是空的,我将 filePath 保存到 bundle (?) 的方式不正确。如何从 onActivityResult 获取文件路径?这是 .java类,以防万一。先感谢您。真的需要你的帮助。

最佳答案

Intent对象用于在 Activity 之间传递参数。您收到的文件路径必须保留在您的 Activity 中。

在您的 Activity 中创建一个 filePath 变量,在 onActivityResult 上设置它并在 onUploadButtonClick 上读取它。

请注意,必须在 onSaveInstanceState 回调期间保存变量值并在 onCreate 中恢复它,因为每次您打开手机时, Activity 都会被销毁并重新创建。检查此以获取更多信息:Recreating an Activity

关于java - Android:无法使用 .getStringExtra 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33687327/

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