Android Button SetEnabled NullPointerException

时间:2014-09-24 09:55:27

标签: android button nullpointerexception

您好我尝试在Android中使用Button的setEnable函数,但是当我在logcat中运行我的应用程序时,在我的按钮中显示NullPointerException。这是我的mainActivity

package com.kufhed.tasbih;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

	Button addCount, count33, count99;
	int count=0; int max=0;
	Tasbih tasbih;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tasbih=new Tasbih();
        addCount=(Button) findViewById(R.id.addCount);
        count33=(Button) findViewById(R.id.count33);
        count99=(Button) findViewById(R.id.count99);
        addCount.setEnabled(false);
       
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    public void addCount(View v)
    {
    	addCount=(Button) findViewById(R.id.addCount);
        count33=(Button) findViewById(R.id.count33);
        count99=(Button) findViewById(R.id.count99);
    	do{
    		if(tasbih.getCount()!=tasbih.getMax())
    		{
    			tasbih.addCount();
    			addCount.setText(String.valueOf(tasbih.getCount()));
    			
    		}else
    		{
    			addCount.setText("Done");
    			addCount.setEnabled(false);
    	    	this.count33.setEnabled(true);
    	    	this.count99.setEnabled(true);
    		}
    	}while(tasbih.addCount());
    }
    
    public void count33(View v)
    {
    
    	tasbih.setMax(33);
    	this.addCount.setEnabled(true);
    	this.count33.setEnabled(false);
    	this.count99.setEnabled(false);
    }
    public void count99(View v)
    {
    	
    	tasbih.setMax(99);
    	this.addCount.setEnabled(true);
    	this.count33.setEnabled(false);
    	this.count99.setEnabled(false);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

这是我的Fragment_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kufhed.tasbih.MainActivity$PlaceholderFragment" >
    <Button 
        android:id="@+id/addCount"
        android:layout_width="200px"
        android:layout_height="200px"
        android:layout_centerInParent="true"
        android:background="@drawable/tasbih"
        android:text="!"
        android:textSize="50px"
        android:typeface="monospace"
        android:onClick="addCount"
        />

    <Button
        android:id="@+id/count33"
        android:layout_width="100px"
        android:layout_height="100px"
        android:layout_alignBaseline="@+id/count99"
        android:layout_alignBottom="@+id/count99"
        android:layout_toLeftOf="@+id/addCount"
        android:background="@drawable/tasbih_33"
        android:onClick="count33"
         />

    <Button
        android:id="@+id/count99"
        android:layout_width="100px"
        android:layout_height="100px"
        android:layout_below="@+id/addCount"
        android:layout_marginTop="36dp"
        android:layout_toRightOf="@+id/addCount"
        android:background="@drawable/tasbih_99"
        android:onClick="count99"
        />

</RelativeLayout>

感谢帮助我

0 个答案:

没有答案