java - 应用程序意外停止。请重试我的 Android 代码中的 "Force close"错误

标签 java android

我在 Eclipse 中为 Android 编写了以下代码:

package mohammad.negahdari.mystartup4;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MyStartup4Activity extends Activity {

  public int counter;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final TextView txtCaption = (TextView) findViewById(R.id.txtCaption);
    final Button btn1 = (Button) findViewById(R.id.txtCaption);
    txtCaption.setText("Hooora");
    txtCaption.setTextColor(Color.parseColor("#0000ff"));
    txtCaption.setBackgroundColor(Color.parseColor("#ffffff"));
    for (int i = 0; i <= 4; i++)
        Toast.makeText(MyStartup4Activity.this, "Mohammad " + i, Toast.LENGTH_SHORT).show();
    txtCaption.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View arg0) {
             Log.i("LOG", "Clicked");
             Toast.makeText(MyStartup4Activity.this, "Clicked", Toast.LENGTH_SHORT).show();
             counter++;
             txtCaption.setText("Number is : " + counter);
         }
     });

    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Log.i("LOG", "Clicked");
            Toast.makeText(MyStartup4Activity.this, "Clicked", Toast.LENGTH_SHORT).show();
            txtCaption.setVisibility(View.GONE);
        }
    });

  }
}

并收到此错误:

The application has stopped unexpectedly. please try again

但是当我删除这些行时:

 btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Log.i("LOG", "Clicked");
            Toast.makeText(MyStartup4Activity.this, "Clicked", Toast.LENGTH_SHORT).show();
            txtCaption.setVisibility(View.GONE);
        }
    });

我没有错误。

我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >


    <TextView
    android:id="@+id/txtCaption"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ff00ff"
    android:gravity="center"
    android:text="..."
    android:textColor="#00ff00"
    android:textSize="40dip" />

    <Button
      android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>

最佳答案

The application has stopped unexpectedly. please try again But when I delete these lines: have no error.

显然,您引用了错误的 id

final TextView txtCaption = (TextView) findViewById(R.id.txtCaption);
final Button btn1 = (Button) findViewById(R.id.txtCaption);

检查 xml 中的按钮 ID。您应该为您的按钮分配不同的ID。

根据您的情况,您应该使用

final Button btn1 = (Button) findViewById(R.id.btn1);

关于java - 应用程序意外停止。请重试我的 Android 代码中的 "Force close"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44712342/

相关文章:

java - 如何扩展 FlowLayout 以使组件的大小能够占用尽可能多的空间?

java添加增量数字到文件之前存在

android - 由于未知故障,应用程序无法安装在 Android 设备上

android - 使用 Android Studio 将文件转换为字符串,然后转换为 Base64 Fromate

android - 无法从 Util 类型对非静态方法 getIMEI() 进行静态引用

Java正则表达式替换原始字符串中的组值

java - 使用 Dagger 注入(inject)登录 session

java - MariaDB:启用 Java 存储过程

java - 创建部分覆盖父级的 Activity 的最佳方法

java - 如何在 Android 中为 facebook 设置 SessionDefaultAudience