2017-06-23 78 views
1

我在Eclipse中寫了下面的代碼爲Android:應用程序意外停止。請嘗試在我的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> 
+0

你的日誌中有什麼錯誤? –

+2

使用Android Studio代替Eclipse。 –

+1

使用'final Button btn1 =(Button)findViewById(R.id.btn1);'。請看下面的@JohnJoe答案。 –

回答

0

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); 

檢查button ID XML格式。您應該爲您的button分配不同的ID。

在你的情況,你應該使用

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

非常感謝你 –

0

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

兩個txtCaption和BTN1指向同一個ID(R.id.txtCaption)

+0

非常感謝你 –

相關問題