我的原木猫
2022-07-29 21:42:03.128 24303-24303/com.example。newcadviewerapp E/AndroidRuntime:致命异常:main
过程:com.example。newcadviewerapp,PID:24303
java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.newcadviewerapp/com.example.newcadviewerapp.ShareMedia}:java.langs.ClassCastException:androidx.appcompat.widget。AppCompatImageView无法转换为android.widget.Button
在android.app.ActivityThread.performLaunchActivity(ActivityTh绪.java:3729)
在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivity Item.java:85)
在android.app.servertransaction.TransactionExecutor.execute(TransactionExcutor.java:100)
在android.os.Handler.dispatchMessage(Handler.java:106)
在android.app.ActivityThread.main(ActivityThread.java:8296)
位于java.lang.reflect.Method。调用(本机方法)
运行(RuntimeInit.java:612)
位于com.android.internal.os.ZygoteInit.main(zygodeinit.java:1006)
原因:java.lang.ClassCastException:androidx.appcompat.widget。AppCompatImageView无法转换为android.widget.Button
在android.app.Activity.performCreate(Activity.java:8153)
在android.app.Instrumentation.callActivityOnCreate(instrumentment.java:1310)
在android.app.ActivityThread.performLaunchActivity(ActivityTh绪.java:3698)
在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivity Item.java:85)
在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExcutor.java:140)
在android.app.servertransaction.TransactionExecutor.execute(TransactionExcutor.java:100)
在android.app.ActivityThread$H.handleMessage(ActivityThead.java:2326)
在android.os.Handler.dispatchMessage(Handler.java:106)
在android.app.ActivityThread.main(ActivityThread.java:8296)
运行(RuntimeInit.java:612)
位于com.android.internal.os.ZygoteInit.main(zygodeinit.java:1006)
share\u photo2.xml
)应用程序崩溃的原因:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/app_bar"
android:background="@android:color/white"
>
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@+id/profile_toolbar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/close_share_post_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/to_decline_share_post"
android:src="@drawable/cross">
</ImageView>
<ImageView
android:id="@+id/share_post_buttom"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:contentDescription="@string/share_the_post_into_feed"
android:src="@drawable/tick">
</ImageView>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="@id/app_bar">
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="@+id/image_preview"
android:src="@drawable/add_image_icon"
android:scaleType="centerCrop"
android:layout_marginTop="10dp"
android:contentDescription="@string/preveiw_of_the_image_to_be_posted">
</ImageView>
<EditText
android:id="@+id/post_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="5dp"
android:layout_marginTop="10dp"
android:hint="@string/say_something_about_the_post"
android:inputType="text"
android:minHeight="48dp"
android:textColorHint="#757575"
android:autofillHints="">
</EditText>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<include layout="@layout/bottom_nav" />
</RelativeLayout>
</RelativeLayout>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="55dp"
>
<ImageButton
android:id="@+id/add_picture_button_bottomnav"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="44dp"
android:contentDescription="@string/add_pictures"
app:layout_constraintEnd_toStartOf="@+id/imageButton3"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_input_add" />
<ImageButton
android:id="@+id/search_button_bottomnav"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="4dp"
android:layout_weight="1"
android:contentDescription="@string/search_for_recruiter_and_peers"
app:layout_constraintEnd_toStartOf="@+id/add_picture_button_bottomnav"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_menu_search" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="54dp"
android:layout_height="50dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="52dp"
android:contentDescription="@string/profile_page"
android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/img_1" />
</androidx.constraintlayout.widget.ConstraintLayout>
</merge>
共享媒体。kt:
package com.example.newcadviewerapp
import android.app.Activity
import android.app.Instrumentation
import android.media.Image
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.google.firebase.storage.FirebaseStorage
import com.google.firebase.storage.StorageReference
import com.squareup.picasso.Picasso
class ShareMedia: AppCompatActivity() {
lateinit var viewPager2: ViewPager2
lateinit var storagePostPicRef : StorageReference
private lateinit var image_preview : ImageView
lateinit var share_post_buttom:Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.share_photo2)
image_preview=findViewById(R.id.image_preview)
share_post_buttom=findViewById(R.id.share_post_buttom)
val contract=registerForActivityResult(ActivityResultContracts.GetContent()){ // We get the uri of the image
image_preview.setImageURI(it)
}
storagePostPicRef=FirebaseStorage.getInstance().reference.child("Post Pictures")
image_preview.setOnClickListener ()
{
contract.launch("image/*")
}
}
}
AndroidManifest。xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newcadviewerapp">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<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/Theme.NewCADViewerApp">
<activity
android:name=".LoginActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".JoinActivity"/>
<activity android:name=".MainActivity"/>
<activity android:name=".RecruiterProfile"/>
<activity android:name=".ShareMedia">
</activity>
</application>
</manifest>