-3

切换活动

   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(new View.OnClickListener() {

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

a8.setOnClickListener(new 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>

日志

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

4

1 回答 1

1

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

于 2012-11-15T17:17:31.073 回答