我想在活动前添加一些弹出窗口,
我环顾四周,在Google和StackOverflow上都找到了一些信息。但是,我没有使其工作。现在希望我可以在代码方面获得帮助。
我相对缺乏Android开发经验。
预先感谢
CardUnfinished.kt
import android.content.Context
import android.content.Intent
import android.nfc.NfcAdapter
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.transition.Slide
import android.transition.TransitionManager
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.Button
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.Toast
import com.brid.azis.vipgame.R
import com.brid.azis.vipgame.R.layout.activity_active_cards
import com.brid.azis.vipgame.R.layout.pop_up_scan_teacher_card
import com.brid.azis.vipgame.test.Adapter.ActiveCardsViewAdapter
import com.brid.azis.vipgame.test.DataModel.DataCard
import com.brid.azis.vipgame.test.Database.DBOnGoingMissionHelper
import com.brid.azis.vipgame.test.Database.missionDB
import com.brid.azis.vipgame.test.Util.NFCutil
import kotlinx.android.synthetic.main.pop_up_scan_teacher_card.*
import org.jetbrains.anko.db.classParser
import org.jetbrains.anko.db.select
import org.jetbrains.anko.toast
class CardsUnfinished : AppCompatActivity() {
var MNfcAdapter: NfcAdapter? = null
var mNfcMessage: String? = null
private var dataCards :MutableList<DataCard> = mutableListOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(activity_active_cards)
MNfcAdapter = NfcAdapter.getDefaultAdapter(this)
Toast.makeText(this,getString(R.string.TOAST_PETUNJUK), Toast.LENGTH_LONG).show()
val list = findViewById<RecyclerView>(R.id.active_mission_list)
initData()
list.layoutManager = LinearLayoutManager(this)
list.adapter = ActiveCardsViewAdapter(dataCards) { dataCard: DataCard -> cardsClicked(dataCard) }
}
private fun initData() {
missionDB.use {
val result = select(DataCard.TABLE_USERCARD)
val cards = result.parseList(classParser<DataCard>())
dataCards.addAll(cards)
}
}
private fun cardsClicked(cards:DataCard){
Toast.makeText(this,"Clicked: ${cards.title}",Toast.LENGTH_SHORT).show()
val inflater: LayoutInflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view = inflater.inflate(R.layout.pop_up_scan_teacher_card,null)
val popupWindow = PopupWindow(
view, // Custom view to show in popup window
LinearLayout.LayoutParams.WRAP_CONTENT, // Width of popup window
LinearLayout.LayoutParams.WRAP_CONTENT // Window height
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
popupWindow.elevation = 10.0F
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
// Create a new slide animation for popup window enter transition
val slideIn = Slide()
slideIn.slideEdge = Gravity.TOP
popupWindow.enterTransition = slideIn
// Slide animation for popup window exit transition
val slideOut = Slide()
slideOut.slideEdge = Gravity.RIGHT
popupWindow.exitTransition = slideOut
}
val buttonPopup = view.findViewById<Button>(R.id.btn_cancelCard)
buttonPopup.setOnClickListener{
// Dismiss the popup window
popupWindow.dismiss()
}
popupWindow.setOnDismissListener {
Toast.makeText(applicationContext,"Popup closed",Toast.LENGTH_SHORT).show()
}
TransitionManager.beginDelayedTransition(layout_popup)
popupWindow.showAtLocation(
layout_popup, // Location to display popup window
Gravity.CENTER, // Exact position of layout to display popup
0, // X offset
0 // Y offset
)
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
toast(mNfcMessage!!)
}
override fun onResume() {
super.onResume()
MNfcAdapter?.let {
NFCutil.enableNFCInForegroundWhileRead(it, this, javaClass)
}
}
override fun onPause() {
super.onPause()
MNfcAdapter?.let {
NFCutil.disableNFCInForeground(it, this)
}
}
}
activity_card_unfinished.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/active_mission_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".test.Activity.CardsUnfinished"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
CardsViewAdapter.kt
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.View
import android.widget.TextView
import com.brid.azis.vipgame.R
import com.brid.azis.vipgame.test.DataModel.DataCard
class CardsViewAdapter(val cards:List<DataCard>, val clickListener: (DataCard) -> Unit):
RecyclerView.Adapter<CardsViewAdapter.CardViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder {
val inflater = LayoutInflater.from(parent.context)
val view = inflater.inflate(R.layout.item_mission,parent,false)
return CardViewHolder(view)
}
override fun getItemCount(): Int = cards.size
override fun onBindViewHolder(holder:CardViewHolder, position: Int) {
holder.bindCards(cards[position],clickListener)
}
class CardViewHolder(view:View):RecyclerView.ViewHolder(view){
val judul = view.findViewById<TextView>(R.id.tv_judulkartu)
val petunjuk = view.findViewById<TextView>(R.id.tv_petunjukkartu)
val tanggal = view.findViewById<TextView>(R.id.tv_tanggalmasukkartu)
fun bindCards(cards:DataCard, clickListener: (DataCard) -> Unit){
judul.text = cards.title
petunjuk.text = cards.instruction
tanggal.text = cards.inputdate
itemView.setOnClickListener{clickListener(cards)}
}
}
}
单击卡片项目时出现我的错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.brid.azis.vipgame, PID: 11277
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.ViewGroup.isLaidOut()' on a null object reference
at android.transition.TransitionManager.beginDelayedTransition(TransitionManager.java:413)
at android.transition.TransitionManager.beginDelayedTransition(TransitionManager.java:386)
at com.brid.azis.vipgame.test.Activity.CardsUnfinished.cardsClicked(CardsUnfinished.kt:115)
at com.brid.azis.vipgame.test.Activity.CardsUnfinished.access$cardsClicked(CardsUnfinished.kt:32)
at com.brid.azis.vipgame.test.Activity.CardsUnfinished$onCreate$1.invoke(CardsUnfinished.kt:51)
at com.brid.azis.vipgame.test.Activity.CardsUnfinished$onCreate$1.invoke(CardsUnfinished.kt:32)
at com.brid.azis.vipgame.test.Adapter.ActiveCardsViewAdapter$CardViewHolder$bindCards$1.onClick(ActiveCardsViewAdapter.kt:41)