尝试删除项目时,RecyclerView Adapter返回null

时间:2018-08-03 21:14:53

标签: java android android-recyclerview adapter

我遇到if("404".equals(SampleResult.getResponseCode())) { // Success SampleResult.setSuccessful(true); // Change sampler status to success AssertionResult.setFailure(false); // Set assertion status to success as well } else { AssertionResult.setFailure(true); // Set assertion status to failure } 问题。我目前正在尝试使它的上下文菜单(出现在Long CLick上)可以删除回收者视图中的项目,但是没有运气。 我已经调试了我的应用程序,每当我单击应该删除该项目的按钮时,我的应用程序就会崩溃,并且我收到有关适配器返回RecyclerView的信息。

我的MainActivity:

null

LogCat:

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);

    }

}


}

1 个答案:

答案 0 :(得分:1)

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

CalculationsAdapter adapter = new CalculationsAdapter(calculations);

此行应为:

adapter = new CalculationsAdapter(calculations);


说明:

这样做:

CalculationsAdapter adapter = new CalculationsAdapter(calculations);

您正在将adapter变成局部变量-仅对方法onOptionsItemSelected()局部,因此adapternull内是onContextItemSelected()

为了通过此行代码将adapter保留为有效的类变量,请执行以下操作: