不幸的是,OnClickListner已经停止了

时间:2018-05-25 15:23:47

标签: android onclicklistener

我写了一段简单的代码,导致应用停止工作。我找到了错误出现的位置(OnClickListner),但我不知道如何解决它

    package com.example.alireza.mygrocerylist.Activities.Activities;

import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.alireza.mygrocerylist.R;

public class MainActivity extends AppCompatActivity {

    private AlertDialog.Builder dialogBuilder;
    private AlertDialog dialog;
    private EditText groceryItem, quantity;
    private Button saveButton;

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

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                //        .setAction("Action", null).show();
                createPopupDialog();

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    **private  void createPopupDialog() {
        dialogBuilder = new AlertDialog.Builder(this);
        View view = getLayoutInflater().inflate(R.layout.popup, null);
        groceryItem = (EditText) findViewById(R.id.groceryItem);
        quantity = (EditText) findViewById(R.id.geroceryQty);
        saveButton = (Button) findViewById(R.id.saveButtom);
        dialogBuilder.setView(view);
        dialog = dialogBuilder.create();
        dialog.show();
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveGroceryToDB(v);
            }
        });**



    }

    private void saveGroceryToDB(View v) {

    }

    public boolean onKeyUp (int keyCode, KeyEvent event) {
        //here you can start your activity, i.e. this way:
        //Toast.makeText(MainActivity.this,"First",Toast.LENGTH_LONG).show();
        return true;
    }

}

当代码执行时,在运行期间它来到createPopupDialog(),遗憾的是我得到了错误,我的Grocery List已经停止了!但如果我拿出来 saveButton.setOnClickListener(新的View.OnClickListener()及其覆盖方法应用程序再次运行! 你可以帮我解决这个错误吗?

1 个答案:

答案 0 :(得分:0)

无法直接访问对话框的视图。请这样做: -

[[self missionOperator] uploadMissionWithCompletion:^(NSError * _Nullable error) {
    if (error){
        ShowMessage(@"Upload Mission failed", error.description, @"", nil, @"OK");
    }else {
        ShowMessage(@"Upload Mission Finished", @"", @"", nil, @"OK");
    }
}];

//同样,也适用于其他观点