【问题标题】:This code of android app runs once only and app stops working此android应用程序代码仅运行一次,应用程序停止工作
【发布时间】:2017-05-25 09:00:27
【问题描述】:

这是堆栈代码; 单击按钮时,按钮编号被添加到堆栈中,并在 textview 上显示推送,类似地,单击时,推送编号从堆栈中推送。但只完成一次操作或交替,我不能推两次。

    Button b1 = (Button) findViewById(R.id.btn1);
    b1.setOnClickListener(this);
    Button b2 = (Button) findViewById(R.id.btn2);
    b2.setOnClickListener(this);
    EditText e1 = (EditText) findViewById(R.id.etn1);
     x =  e1.getId();

// 点击事件监听器

    @Override
    public void onClick(View v) {
        TextView t1 = (TextView) findViewById(R.id.tvn);

      if (v.getId()== R.id.btn1) {
         Stack s1 = new Stack();
          s1.push(x);
          EditText e1 = (EditText) findViewById(R.id.etn1);
          e1.setId(0);

          t1.setText("Pushed");
          t1.setAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_in_left));
      }
      else if (v.getId() == R.id.btn2) {
          Stack s2 = new Stack();
          s2.pop();
          t1.setText("Poped");
          t1.setAnimation(AnimationUtils.loadAnimation(MainActivity.this, 
          android.R.anim.slide_in_left));
      }
    }

【问题讨论】:

  • 如果你给我们堆栈跟踪,你会帮助我们
  • 在 onCreateMethod 中初始化视图

标签: java android stack


【解决方案1】:

您很可能在s2.pop() 收到NullPointerException。您正在尝试pop 一个null 对象,因为您的堆栈s2 中没有object

1. 尝试使用单个Stack 并将其声明为global 并将其用于pushpop 操作。

2.在任何pop 操作之前检查stack 是否为empty

试试这个:

public class YourActivity extends AppCompatActivity {

    ........
    ................

    Stack stack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ........
        ................

        Button b1 = (Button) findViewById(R.id.btn1);
        b1.setOnClickListener(this);
        Button b2 = (Button) findViewById(R.id.btn2);
        b2.setOnClickListener(this);
        EditText e1 = (EditText) findViewById(R.id.etn1);

        // Stack
        stack = new Stack();
    }

    @Override
    public void onClick(View v) {
        TextView t1 = (TextView) findViewById(R.id.tvn);

        // Get id
        x =  e1.getId();

        if (v.getId()== R.id.btn1) {

            // Push
            stack.push(x);

            EditText e1 = (EditText) findViewById(R.id.etn1);
            e1.setId(0);

            t1.setText("Pushed");
            t1.setAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_in_left));
        }
        else if (v.getId() == R.id.btn2) {

            if (stack.empty()) {
                // Show message
                Toast.makeText(getApplicationContext(), "Stack is empty!", Toast.LENGTH_SHORT).show();
            } else {
                // Pop
                stack.pop();

                t1.setText("Poped");
               t1.setAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_in_left));
            }
        }
    }
}

希望对你有帮助~

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
  • 1970-01-01
  • 2017-08-29
  • 2014-12-08
  • 2014-04-21
  • 1970-01-01
相关资源
最近更新 更多