我只想问为什么StartActivity(intent)失败? 然后显示logcat:
D/AddPhotosFragment: categoryDesignID: ClothingDesign
D/EGL_emulation: eglMakeCurrent: 0x7ea0c3349a00: ver 2 0 (tinfo 0x7ea19df7cea0)
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@15c2c02
D/CategoryDetailRecyclerViewActivity: Intent:Intent { }
D/CategoryDetailRecyclerViewActivity: categoryDesignID: null
AddPhotosFragment.kt
//Upload images button
upload_imgs_button.setOnClickListener {
Log.d(TAG, "uploadImgsPath: $uploadImgsPath");
//Upload images to firebase storage
if (uploadImgsPath != null) {
var count = imagesList.size
for (uriForUpload in imagesList) {
val uri = Uri.parse(uriForUpload)
val imgNameUUID = UUID.randomUUID().toString()
val withAppendedPath =
Uri.withAppendedPath(uri, imgNameUUID).toString()
var imgFileRef = uploadImgsPath!!.child(withAppendedPath)
imgFileRef.putFile(uri)
.continueWithTask {
if (!it.isSuccessful) {
it.exception?.let {
throw it
}
}
imgFileRef.downloadUrl
}.addOnCompleteListener {
if (it.isSuccessful) {
var uriForDownload = it.result.toString()
Log.d(TAG, "uriForDownload:$uriForDownload ");
downloadImagesUriList.add(uriForDownload)
count--
if (count == 0) {
Log.d(
TAG,
"downloadImagesUriList:$downloadImagesUriList "
);
var uuidOfGroupId = UUID.randomUUID().toString()
var uploadedImages = UploadedImages(
categoryDesignID,
user!!.uid,
downloadImagesUriList,
Timestamp.now(),
uuidOfGroupId,
user!!.photoUrl.toString(),
user!!.displayName
)
Log.d(TAG, "uploadedImages:$uploadedImages ");
//Save uploaded images path info to firestore
firestore.collection("uploadedImages")
.document(uuidOfGroupId)
.set(uploadedImages)
.addOnSuccessListener {
Toast.makeText(
context,
"Upload successful",
Toast.LENGTH_LONG
).show()
//TODO Show RecyclerView
var intent = Intent(
context,
CategoryDetailRecyclerViewActivity::class.java
)
Log.d(
TAG,
"categoryDesignID: ${uploadedImages.categoryDesignID}"
);
intent.putExtra(
"categoryDesignID",
uploadedImages.categoryDesignID
)
startActivity(intent)
}
}
}
}
}
CategoryDetailRecyclerViewActivity.kt
class CategoryDetailRecyclerViewActivity : AppCompatActivity() {
val TAG = CategoryDetailRecyclerViewActivity::class.java.simpleName
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_category_detail_recycler_view)
var intent = Intent()
Log.d(TAG, "Intent:$intent ");
var categoryDesignID = intent.getStringExtra("categoryDesignID")
Log.d(TAG, "categoryDesignID: $categoryDesignID");
我的清单:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ui.categoryDetailRecyclerView.CategoryDetailRecyclerViewActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:3)
我不确定why StartActivity(intent) failed
是什么意思,但是根据您的Logcat,它会打印出我希望从代码中得到的内容。
首先,它不会失败。您会在日志中看到onCreate()
内部的代码已执行。这样就开始了活动。
D/CategoryDetailRecyclerViewActivity: Intent:Intent { }
D/CategoryDetailRecyclerViewActivity: categoryDesignID: null
在这里您看到问题之一:您的categoryDesignID为null。那是因为您没有从正确的地方阅读它。要从用于启动“活动”的意图中获取数据,您应该要求该意图,而不是创建一个新意图。
将onCreate代码更改为此,至少应记录正确的ID值:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_category_detail_recycler_view)
// don't create an Intent instance, it is already available and contains what you have send via startActivity(intent)
// var intent = Intent()
Log.d(TAG, "Intent:$intent ");
var categoryDesignID = intent.getStringExtra("categoryDesignID")
Log.d(TAG, "categoryDesignID: $categoryDesignID");
}
答案 1 :(得分:0)
此问题已解决。
我后来将var intent = Intent()更改为 var intent = getIntent(),然后CategoryDetailRecyclerViewActivity可以获取意图。