gpt4 book ai didi

java - 在类之间传递值以及更新回收器 View 时如何修复 "System services not available to Activities before onCreate"?

转载 作者:行者123 更新时间:2023-12-01 23:36:08 26 4
gpt4 key购买 nike

我正在制作接受收银应用程序的订单。使用回收器 View 显示订单队列。但是,我在类之间传递 ArrayList 值以及更新回收器 View 时遇到问题。在这种情况下,我不会有大量的回收器 View ,因此性能不会成为太大的问题,但我们将非常赞赏执行此操作的优化方法。我只是不知道如何解决这个问题。

我尝试在 MainActivty 类中创建一个 set 方法,我也尝试过 put extra 的 Intent ,但我不认为我完全理解 put extra 。

主要 Activity 类:

public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener {
private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
public ArrayList<String> orderList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);


// set up the RecyclerView
recyclerView = findViewById(R.id.recyle_view_activity);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new MyRecyclerViewAdapter(this, orderList);
((MyRecyclerViewAdapter) mAdapter).setClickListener(this);


recyclerView.addItemDecoration(new DividerItemDecoration(this,
DividerItemDecoration.VERTICAL));


recyclerView.setAdapter(mAdapter);


}

//This is the set method I attempted:
public void setOrderAdd(ArrayList<String> data){
if (data != null){
mAdapter = new MyRecyclerViewAdapter(this,data); //Error refrences here
}
}
}

其他类:

public class regularCoffee extends AppCompatActivity implements AdapterView.OnItemSelectedListener, Serializable {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_regular_coffee);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
addToOrder();

}

public void addToOrder(){
//Reg Addons:
final CheckBox leaveRoomReg = findViewById(R.id.leave_room_reg);
final CheckBox cinnamonReg = findViewById(R.id.cinnamon_reg);
final CheckBox chocolateSyrupReg = findViewById(R.id.chocolate_syrup_reg);
final CheckBox whiteChocolateSyrupReg = findViewById(R.id.white_chocolate_syrup_reg);
final CheckBox caramelReg = findViewById(R.id.caramelReg);
final CheckBox hazelnutReg = findViewById(R.id.hazel_nut_reg);

final CheckBox[] RegCoffeeAddOns = {leaveRoomReg,cinnamonReg,chocolateSyrupReg,whiteChocolateSyrupReg,caramelReg,hazelnutReg};

//Decaf Addons:
final Button leaveRoomDecaf = findViewById(R.id.leave_room_decaf);
final Button cinnamonDecaf = findViewById(R.id.cinnamon_decaf);
final Button chocolateSyrupDecaf = findViewById(R.id.chocolate_syrup_decaf);
final Button whiteChocolateDecaf = findViewById(R.id.white_chocolate_syrup_decaf);
final Button caramelDecaf = findViewById(R.id.caramel_decaf);
final Button hazelnutDecaf = findViewById(R.id.hazel_nut_decaf);

final Button[] DecafCoffeeAddOns = {leaveRoomDecaf,cinnamonDecaf,caramelDecaf,chocolateSyrupDecaf,whiteChocolateDecaf,hazelnutDecaf};

Button addToOrderButton = findViewById(R.id.addToOrderReg);



addToOrderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity addToOrderArea = new MainActivity();
String forOrder;
ArrayList<String> tempArray = new ArrayList<>();

if (regCoffeeQuantity > 0){
forOrder = "Regular Coffee (x" + regCoffeeQuantity + ") \n ";
for (int i = 0; i < RegCoffeeAddOns.length; i++){
if (RegCoffeeAddOns[i].isChecked()){
forOrder = forOrder + "| " + RegCoffeeAddOns[i].getText().toString() + " ";
System.out.println(forOrder);
}
}

addToOrderArea.setOrderAdd(tempArray); //Error refrences here

}
}
});
}

RecyclerView适配器:

package com.example.holygroundsapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

private List<String> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;

// data is passed into the constructor
MyRecyclerViewAdapter(Context context, List<String> data) {
this.mInflater = LayoutInflater.from(context); //Error refrences here.
this.mData = data;
}


// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
return new ViewHolder(view);

}

// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String animal = mData.get(position);
holder.myTextView.setText(animal);
}

// total number of rows
@Override
public int getItemCount() {
return mData.size();
}


// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;

ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.tvAnimalName);
itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}

// convenience method for getting data at click position
String getItem(int id) {
return mData.get(id);
}

// allows clicks events to be caught
void setClickListener(MainActivity itemClickListener) {
this.mClickListener = itemClickListener;
}

// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}

这是错误:

 java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:5774)
at android.view.LayoutInflater.from(LayoutInflater.java:233)
at com.example.holygroundsapplication.MyRecyclerViewAdapter.<init>(MyRecyclerViewAdapter.java:21)
at com.example.holygroundsapplication.MainActivity.setOrderAdd(MainActivity.java:89)
at com.example.holygroundsapplication.regularCoffee$8.onClick(regularCoffee.java:262)
...

最佳答案

在您的 onClick 函数中,您有

MainActivity addToOrderArea = new MainActivity();

您不应自行创建 Activity 实例。这是Android系统所做的事情。因为您自己创建了 Activity,所以它没有被初始化(即 onCreate() 没有被调用)。 setOrderAdd() 中创建的适配器需要初始化 Activity,因此会引发异常。

为了防止这些问题,您需要使用 startActivity() 创建 Activity。您可以通过向起始 Intent 添加额外内容来将数据传递到新 Activity。

关于java - 在类之间传递值以及更新回收器 View 时如何修复 "System services not available to Activities before onCreate"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58276389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com