解决方案 »

  1.   

    我是新手,不过我觉得可以在点击前加个final TextView text_temp=text;点击事件中引用text_temp。还有种情况是不定义text为全局变量,在create中定义为final 。还有final 不是静态,静态是static,定义成静态是可以赋值的,也可以把全局text前加static试试,也不知说得对不对
      

  2.   

    问题解决,帖上原代码,给大家看下import android.graphics.Color;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v7.app.ActionBarActivity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;
    public class MainActivity extends ActionBarActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);        if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new PlaceholderFragment())
                        .commit();
            }
        }
        @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);
                final TextView text = (TextView) rootView.findViewById(R.id.textView1);
                Button button1=(Button) rootView.findViewById(R.id.button1);
                Button button2=(Button) rootView.findViewById(R.id.button2);
               
                
                button1.setOnClickListener(new OnClickListener(){
                    public void onClick (View v){
                    text.setBackgroundColor(Color.RED);
                    }
                   });
                   
                   button2.setOnClickListener(new OnClickListener(){
                    public void onClick(View v){
                    text.setBackgroundColor(Color.GREEN);
                    }
                   });     
                return rootView;
                
            }
        }}