我有MainActivity,Adapter,ViewHolder等。
我正在尝试从ViewHolder更改activity_main2.xml文件中textView的值。 我从ViewHolder调用MainActivity内部的函数,但出现此错误。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.soyut.su.epos01, PID: 18042
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
at com.soyut.su.epos01.MainActivity._$_findCachedViewById(Unknown Source:52)
at com.soyut.su.epos01.MainActivity.doSomething(MainActivity.kt:180)
at com.soyut.su.epos01.PartViewHolder$bindItems$1.onClick(MainAdapter.kt:106)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
MainActivity文件
class MainActivity : AppCompatActivity() {
lateinit var mRecyclerView : RecyclerView
private lateinit var sDatabase: DatabaseReference
var trId: Long = 0
var discAmnt : Double = 0.00
var unt = 1
var tr :Trans ?= null
var sb = 0.00
var disc = sb*.1
var tt = sb-disc
var list = ArrayList<Product>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
sDatabase = FirebaseDatabase.getInstance().getReference().child("Sales")
sDatabase.child("Sales").addValueEventListener(object:ValueEventListener{
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
if(p0.exists()) {
trId = p0.childrenCount
println(trId)
}
}
})
mRecyclerView = findViewById(R.id.recyclerView_main)
mRecyclerView.layoutManager = LinearLayoutManager(this)
mRecyclerView.adapter = MainAdapter(this, list)
btnGranola.setOnClickListener{
//add Granola to list
list.add(Product("Granola", unt, 2.40, unt*2.40))
var sbAmnt = list.sumByDouble {it.pAmount }
txtSubAmount.text = "%.2f".format(sbAmnt).toDouble().toString()
txtTotalAmount.text = "%.2f".format(sbAmnt).toDouble().toString()
// MainAdapter(this, list).notifyDataSetChanged()
println("list size from Granola :"+list.size)
}
btnBrownie.setOnClickListener {
list.add(Product("Brownie", 1, 2.60,2.60))
//recView()
val tot: Double = list.sumByDouble {it.pAmount }
txtSubAmount.text = "%.2f".format(tot).toDouble().toString()
txtTotalAmount.text = "%.2f".format(tot).toDouble().toString()
println("list size from Brownie :"+list.size)
// doSomething()
}
btnTea.setOnClickListener {
list.add(Product("Tea", 1, 1.30,1.30))
sb = list.sumByDouble { it.pAmount }
tr = Trans(sb, sb*.1, sb-disc, list)
txtSubAmount.text = "%.2f".format(sb).toDouble().toString()
txtTotalAmount.text = "%.2f".format(sb).toDouble().toString()
println(tr?.Total)
// recView()
}
btnDiscnt.setOnClickListener {
//recView()
var sbAmnt: Double = list.sumByDouble {it.pAmount }
discAmnt = sbAmnt*.1
"%.2f".format(discAmnt).toDouble()
txtDiscntAmnt.text = "%.2f".format(discAmnt).toDouble().toString()
txtTotalAmount.text = "%.2f".format(sbAmnt-discAmnt).toDouble().toString()
}
btnClear.setOnClickListener {
clearTransaction()
}
btnPay.setOnClickListener {
println("Sub Total : ${txtSubAmount.text}")
println("Discount : ${txtDiscntAmnt.text}")
println("Total : ${txtTotalAmount.text}")
}
btnSave.setOnClickListener {
println("xxx" +list.sumByDouble {it.pAmount })
var sub: Double = list.sumByDouble {it.pAmount }
if(discAmnt!=0.00){
discAmnt = sub*.1
var ttl = sub-discAmnt
var tr : Trans = Trans(sub, discAmnt, ttl, list)
sDatabase.push().setValue(tr).addOnCompleteListener {
Toast.makeText(this, "Transaction saved successfuly !", Toast.LENGTH_SHORT).show()
}
} else {
var ttl = sub - discAmnt
var tr= Trans(sub, discAmnt, ttl, list)
sDatabase.push().setValue(tr).addOnCompleteListener {
Toast.makeText(this, "Transaction saved successfuly !", Toast.LENGTH_SHORT).show()
}
}
clearTransaction()
}
btnPay.setOnClickListener {
val intent = Intent(this, SalesListActivity::class.java)
startActivity(intent)
}
}
private fun clearTransaction() {
//list.clear()
txtSubAmount.text = "£ 0.00"
txtDiscntAmnt.text = "£ 0.00"
txtTotalAmount.text = "£ 0.00"
}
fun doSomething(d :Double){
txtSubAmount.text = d.toString()
}
}
适配器和ViewHolder文件。
class MainAdapter(val context: Context, val items : List<Product>) : RecyclerView.Adapter<PartViewHolder>(), InterfaceArray{
override
var list = ArrayList<Product>()
override fun getItemId(position: Int): Long {
return super.getItemId(position)
}
override fun getItemCount(): Int {
return items.size
}
// Inflates the item views
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PartViewHolder {
// LayoutInflater: takes ID from layout defined in XML.
// Instantiates the layout XML into corresponding View objects.
// Use context from main app -> also supplies theme layout values!
val inflater = LayoutInflater.from(parent.context)
// Inflate XML. Last parameter: don't immediately attach new view to the parent view group
val view = inflater.inflate(R.layout.sinlge_row, parent, false)
return PartViewHolder(view)
}
// Binds each product in the ArrayList to a view
override fun onBindViewHolder(holder: PartViewHolder, position: Int) {
holder.bindItems(items[position])
}
}
class PartViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
// Holds the TextView that will add each product to
fun bindItems(prd: Product) {
val textViewName = itemView.findViewById(R.id.txtOrderNumber) as TextView
var textViewUnit = itemView.findViewById(R.id.txtItemUnit) as TextView
val textViewPrice = itemView.findViewById(R.id.txtItemPrice) as TextView
val textViewAmount = itemView.findViewById(R.id.txtItemAmount) as TextView
var id = adapterPosition
var unitN: Int = 1
textViewName.text = prd.pName
textViewUnit.text = prd.pUnit.toString()
textViewPrice.text = prd.pPrice.toString()
var itemPrice = prd.pPrice
var itemAmount = itemPrice.times(unitN)
textViewAmount.text = itemAmount.toString()
itemView.btnPlus.setOnClickListener {
println("item id : " + id)
// increase the Product model single unit
prd.pUnit = unitN++
// println("Here is the " +MainActivity().list.get(id))
// bind txtItemUnit from single_row to changed unitN (single unit)
textViewUnit.text = unitN.toString()
// change the Product model single pAmount
prd.pAmount = prd.pPrice.times(unitN)
// bind txtItemAmount from single_row to Product pAmount
textViewAmount.text = prd.pAmount.toString()
MainActivity().doSomething(prd.pAmount)
}
itemView.btnMinus.setOnClickListener(View.OnClickListener {
if (unitN >= 1) {
prd.pUnit = unitN--
println(prd.pUnit)
textViewUnit.text = unitN.toString()
textViewAmount.text = prd.pPrice.times(unitN).toString()
} else
prd.pUnit = 1
textViewUnit.text = prd.pUnit.toString()
textViewAmount.text = prd.pPrice.times(prd.pUnit).toString()
})
}
}
single_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:text="Item Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtOrderNumber"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="28dp" android:layout_marginStart="28dp" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintVertical_bias="0.476"/>
<Button
android:text="+"
android:layout_width="30dp"
android:layout_height="30dp" android:id="@+id/btnPlus"
app:layout_constraintStart_toEndOf="@+id/txtOrderNumber"
android:layout_marginLeft="64dp" android:layout_marginStart="64dp" android:textSize="8sp"
app:layout_constraintTop_toTopOf="@+id/txtOrderNumber"
app:layout_constraintBottom_toBottomOf="@+id/txtOrderNumber" app:layout_constraintVertical_bias="0.454"/>
<TextView
android:text="Unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtItemUnit"
app:layout_constraintStart_toEndOf="@+id/btnPlus"
android:layout_marginLeft="10dp" android:layout_marginStart="20dp"
app:layout_constraintTop_toTopOf="@+id/btnPlus" app:layout_constraintBottom_toBottomOf="@+id/btnPlus"/>
<Button
android:text="-"
android:layout_width="30dp"
android:id="@+id/btnMinus" android:layout_height="30dp"
android:textSize="8sp"
app:layout_constraintStart_toEndOf="@+id/txtItemUnit"
android:layout_marginLeft="10dp" android:layout_marginStart="20dp"
app:layout_constraintBottom_toBottomOf="@+id/txtItemUnit"
app:layout_constraintTop_toTopOf="@+id/txtItemUnit"/>
<TextView
android:text="Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtItemPrice"
app:layout_constraintStart_toEndOf="@+id/btnMinus"
android:layout_marginLeft="24dp" android:layout_marginStart="24dp"
app:layout_constraintTop_toTopOf="@+id/btnMinus" app:layout_constraintBottom_toBottomOf="@+id/btnMinus"
app:layout_constraintVertical_bias="0.454"/>
<TextView
android:text="Amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtItemAmount"
app:layout_constraintTop_toTopOf="@+id/txtItemPrice"
app:layout_constraintBottom_toBottomOf="@+id/txtItemPrice" app:layout_constraintVertical_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/txtItemPrice" android:layout_marginLeft="40dp"
android:layout_marginStart="40dp"/>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/txtOrderNumber"
android:id="@+id/view" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginStart="10dp"/>
为什么会出现此错误? 还是有其他示例可以从viewHolder更改此textView值? 任何帮助表示赞赏。
谢谢。