2012-11-15 177 views
-3

开关活动强行关闭应用程序的Android

package com.example.tsabeh.pro; 

import com.example.tsabeh.pro.R; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 

import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class BC extends Activity { 
    int Count; 
    TextView tx; 
    Button a1,a2,a3,a4,a5,a6,a7,a8,a10; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.bc); 


    tx=(TextView)findViewById(R.id.textView1); 
    a1=(Button)findViewById(R.id.button1); 
    a2=(Button)findViewById(R.id.button2); 
    a3=(Button)findViewById(R.id.button3); 
    a5=(Button)findViewById(R.id.button5); 
    a6=(Button)findViewById(R.id.button6); 

    a8=(Button)findViewById(R.id.button8); 
    a10=(Button)findViewById(R.id.button10); 
    Count=0; 

    a1.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Count ++; 
      tx.setText(""+ Count); 
     } 
    }); 
    a1.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Count ++; 
      tx.setText(""+ Count); 
     } 
    }); 
     a2.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Count ++; 
     tx.setText(""+ Count); 
    } 
}); 
    a3.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Count ++; 
     tx.setText(""+ Count); 
    } 
}); 

     a5.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Count ++; 
     tx.setText(""+ Count); 
    } 
}); 
    a6.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Count ++; 
     tx.setText(""+ Count); 
    } 
}); 

a7.setOnClickListener(新View.OnClickListener(){

public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Count ++; 
     tx.setText(""+ Count); 
    } 
}); 

a8.setOnClickListener(新View.OnClickListener(){

public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Count ++; 
     tx.setText(""+ Count); 
    } 
}); 

a10.setOnClickListener(new View.OnClickListener(){

public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Count =0; 
     tx.setText(""+ Count); 
    } 
}); 



} 

}

我的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" 
android:background="@drawable/aad" 
android:orientation="vertical" 
android:weightSum="300" 
> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="99dp" 
    android:layout_marginBottom="20dp" 
    android:gravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="85dip" 
     android:layout_height="70dip" 
     android:layout_gravity="center" 
     android:layout_marginLeft="20dp" 
     android:textSize="50dp" /> 

    <Button 
     android:id="@+id/button10" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/pic4" /> 

</LinearLayout> 



<ScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="316dp" > 





    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/pic2" /> 

     <Button 
      android:id="@+id/button6" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/pic6" /> 

     <Button 
      android:id="@+id/button4" 
      android:layout_width="match_parent" 
      android:layout_height="65dp" 
      android:background="@drawable/pic4" 
      tools:ignore="ObsoleteLayoutParam" /> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="316dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="2.21" 
      android:background="@drawable/pic1" 
      tools:ignore="ObsoleteLayoutParam"/> 

     <Button 
      android:id="@+id/button7" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/pic7" /> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/pic3" /> 

     <Button 
      android:id="@+id/button5" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/pic5" 
      tools:ignore="ObsoleteLayoutParam" /> 

     <Button 
      android:id="@+id/button8" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="26dp" 
      android:background="@drawable/pic8" /> 

     <Button 
      android:id="@+id/button9" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/pic7"/> 
    </LinearLayout> 

</ScrollView> 

日志

强制关闭切换到这个活动时..?

+1

邮政堆栈跟踪? –

+0

为什么你从onCreate方法中删除'a7 =(Button)findViewById(R.id.button7);'' –

+0

发布你的布局和你的logcat。 – prprcupofcoffee

回答

1

按钮a7为空。并且您将onClickListener设置为空引用。所以你猜我有NullPointerException。 邮政堆栈跟踪获取一些信息