java - 不幸的是,该应用程序已停止工作 - android

标签 java android android-layout

单击按钮时会发生这种情况

我的 Activity.XML 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<EditText android:id="@+id/enter_number"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/enter_number"
    android:layout_weight="1"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_calculate" 
    android:onClick="calculate"
    />
</LinearLayout>

.java 文件

    package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class Tables extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the message from the intent
        int number=0;
        int i=1;
        int next=0;
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        number=Integer.parseInt(message);
        TextView textView = new TextView(this);

        String[] myString = new String[10]; //create array
        for (i = 1; i <= myString.length; i++) { 
             next= number + next;
             myString[i] =  String.valueOf(number)+'x'+String.valueOf(i)+'='+String.valueOf(next)+'\n';
            // textView.setText(myString[i]);  
            // setContentView(textView);
        }
        StringBuilder builder = new StringBuilder();
        for (String s : myString){
          builder.append(s+" ");
        textView.setText(builder.toString());
        }
        setContentView(textView);

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_tables, menu);
        return true;
    }
}

我认为错误是在显示过程中发生的。但找不到解决办法。

谢谢

最佳答案

您必须在 Activity 中定义一个calculate方法。由于您在布局文件中设置了 onClick 属性,因此当您单击按钮时,Android Framework 将尝试调用 Tables Activity 的 follow 方法:

  public void calculate(View v){
      //put you code here this will be execute when the button is clicked
  }

关于java - 不幸的是,该应用程序已停止工作 - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12102584/

相关文章:

java - 用于识别解析错误的默认值(字符串 -> double /任何数字)

java - 高效地检查和设置变量

java - 如果我想匹配不同的情况,如何使用 str.matches() ?

android - Robolectric HTTP 测试因 NoSuchMethodError : org. apache.http.impl.client.DefaultRequestDirector 失败

java - 单击抽屉导航上的字符串/链接可加载本地 HTML 页面

java - 服务器和客户端之间的通信标准

android - http ://dl-ssl. google.com/android/repository/addons_list.xml 无法访问

java - Android SDK 管理器运行失败

android - RelativeLayout 占用了所有空间,即使我将其高度设置为 "wrap_content"

java - gridview 项目之间的完美线性间距