gpt4 book ai didi

java - 尝试删除项目时,RecyclerView 适配器返回 null

转载 作者:行者123 更新时间:2023-12-02 10:56:37 25 4
gpt4 key购买 nike

我的 RecyclerView 遇到了一个问题。我目前正在尝试使我的上下文菜单(出现在长按上)可以删除我的回收器 View 中的项目,但没有运气。我已经调试了我的应用程序,每次我单击应删除该项目的按钮时,我的应用程序都会崩溃,并且我得到的信息是我的适配器返回 null

我的主要 Activity :

public class MainActivity extends AppCompatActivity {

//Til LongClickListener
CalculationsAdapter adapter;
ArrayList<Calculation> calculations = new ArrayList<>();


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

RecyclerView rvCalculations = (RecyclerView) findViewById(R.id.rvCalculations);
RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(MainActivity.this, DividerItemDecoration.VERTICAL);
rvCalculations.addItemDecoration(itemDecoration);


//Til CostumItemDecoration
//RecyclerView.ItemDecoration CostumItemDecoration = new DividerItemDecoration(MainActivity.this, DividerItemDecoration.VERTICAL);
//rvCalculations.addItemDecoration(CostumItemDecoration);



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(final MenuItem item) {

switch (item.getItemId()){
case R.id.add_new_calculation:
/* for at starte newcalculation activiteten, gør det i gråt.
Intent intent = new Intent(MainActivity.this, newcalculation.class);
startActivity(intent);
*/

final AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
final View mView = getLayoutInflater().inflate(R.layout.dialog_namecalculation, null);
final EditText mNameEditText = (EditText) mView.findViewById(R.id.editText_calculationName);
Button mNameButton = (Button) mView.findViewById(R.id.button_nameCalculation);

//Viser Alertdialogen
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();

mNameButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

//Her skal der være noget der gør at jeg ikke kan give to af samme navn
//if (mNameEditText.getText() == .getName) ){
//}

if (mNameEditText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, R.string.ErrorMessageNameCalculation,Toast.LENGTH_SHORT).show();

}else{
RecyclerView rvCalculations = (RecyclerView) findViewById(R.id.rvCalculations);
//calculations = Calculation.createCalculationsList(50);
calculations.add(0, new Calculation(" " + mNameEditText.getText()));
CalculationsAdapter adapter = new CalculationsAdapter(calculations);
rvCalculations.setAdapter(adapter);
rvCalculations.setLayoutManager(new LinearLayoutManager(MainActivity.this));
adapter.notifyItemInserted(0);

rvCalculations.scrollToPosition(0);

Toast.makeText(MainActivity.this, R.string.SuccesMessageNameCalculation,Toast.LENGTH_SHORT).show();

dialog.dismiss();

}


}
});


}
return super.onOptionsItemSelected(item);
}

//Til LongClickListener
@Override
public boolean onContextItemSelected(MenuItem item) {

//Hvad itemet i LongClicket skal gøre

switch (item.getItemId()){

case 1:
adapter.removeItem(item.getGroupId());
Toast.makeText(MainActivity.this, "Regningen blev slettet", Toast.LENGTH_SHORT).show();
return true;

case 2:
Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
return true;

default:
return super.onContextItemSelected(item);

}

}


}

日志猫:

08-03 23:33:44.443 1481-1481/hndvrkerregner.hndvrkerregner E/AndroidRuntime: FATAL EXCEPTION: main
Process: package, PID: 1481
java.lang.NullPointerException: Attempt to invoke virtual method 'void package.CalculationsAdapter.removeItem(int)' on a null object reference
at package.MainActivity.onContextItemSelected(MainActivity.java:128)

最佳答案

您已声明类变量 CalculationsAdapter adapter;在您的 Activity 的顶部“主要 Activity ”类。但在onOptionsItemSelected()方法,您将在这行代码中覆盖此声明:

CalculationsAdapter adapter = new CalculationsAdapter(calculations);

这一行应该是:

adapter = new CalculationsAdapter(calculations);

<小时/>

说明:

通过这样做:

CalculationsAdapter adapter = new CalculationsAdapter(calculations);

你正在转向adapter到局部变量中——仅局部于方法 onOptionsItemSelected()因此,adapter那么 null里面onContextItemSelected()

为了保留adapter使用这行代码作为有效的类变量:

关于java - 尝试删除项目时,RecyclerView 适配器返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51680161/

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