簡體   English   中英

應用程序意外停止。 請在我的Android代碼中再次嘗試“強制關閉”錯誤

[英]The application has stopped unexpectedly. please try again “Force close” error in my android codes

我在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);
        }
    });

  }
}

並收到此錯誤:

應用程序意外停止。 請再試一次

但是當我刪除這些行時:

 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>

應用程序意外停止。 請再試一次但是當我刪除這些行時:沒有錯誤。

顯然,你指的是錯誤的id

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

檢查xml中的button ID。 您應該為button指定不同的ID。

在你的情況下,你應該使用

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

我的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>
final TextView txtCaption = (TextView) findViewById(R.id.txtCaption);
final Button btn1 = (Button) findViewById(R.id.txtCaption);

txtCaption和btn1都指向相同的id(R.id.txtCaption)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM