为什么 StartActivity(intent) 失败? logcat show w/ActivityThread:



我只是想问为什么StartActivity(intent(失败了? 然后日志猫显示:

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

添加照片片段.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)
}
}
}
}
}   

类别详细信息回收器视图活动.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>

我不确定你所说的why StartActivity(intent) failed是什么意思,但基于你的 Logcat,它会打印我对代码的期望。

首先,它不会失败。您可以在日志中看到onCreate()中的代码已执行。因此,活动开始了。

D/CategoryDetailRecyclerViewActivity: Intent:Intent {  } 
D/CategoryDetailRecyclerViewActivity: categoryDesignID: null

在这里,您会看到其中一个问题:您的类别设计 ID 为空。那是因为你没有从正确的地方阅读它。要从用于启动活动的意图中获取数据,您应该询问意图,而不是创建新意图。

将您的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");
}

这个问题已经解决了。

后来我把var intent = Intent((改成了var intent = getIntent((,然后CategoryDetailRecyclerViewActivity就可以得到intent了。

最新更新