setText和OnClickListener不起作用

时间:2014-10-29 03:57:45

标签: android android-button

我是Android的新手,所以我遇到了很多问题。我要做一个简单的程序,它可以从edittext中获取值,并通过单击按钮将其打印在textview中。这是我的代码: enter image description here

该计划说,不幸的是它停止了工作。但是,当我从setOnClickListener删除最后几行时,它工作正常。我也只用setText测试过它。它也不起作用。谢谢。 logcat的: enter image description here

XML: enter image description here

2 个答案:

答案 0 :(得分:1)

您的代码应该在PlaceHolderFragment中,如下所示..

public class PlaceHolderFragment extends Fragment {
    private EditText et;
    private Button b;
    private TextView tv;

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

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        et=(EditText) getView().findViewById(R.id.EditText1);
        b = (Button) getView().findViewById(R.id.Button1);
        tv = (Button) getView().findViewById(R.id.textView3);
        b.setOnClicklistener(new OnClickListener(){
            @Override
            public void onClick(View arg0){
                tv.setText("Pressed");
            }
        });
    }
}

并从onCreate()

中删除这些代码
et=(EditText) findViewById(R.id.EditText1);
b = (Button) findViewById(R.id.Button1);
tv = (Button) findViewById(R.id.textView3);
b.setOnClicklistener(new OnClickListener(){
    @Override
    public void onClick(View arg0){
        tv.setText("Pressed");
    }
});

试试这个......

<强>更新

  1. activity_main.xml中
  2. enter image description here

    1. fragment_main.xml
    2. enter image description here

      1. MainActivity.java
      2. import android.support.annotation.Nullable;
        import android.support.v7.app.ActionBarActivity;
        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.View.OnClickListener;
        import android.view.ViewGroup;
        import android.widget.Button;
        import android.widget.EditText;
        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 {
        
                private EditText et;
                private Button b;
                private TextView tv;
        
                public PlaceholderFragment() {
                }
        
                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                    View rootView = inflater.inflate(R.layout.fragment_main, container,
                            false);
                    return rootView;
                }
        
                @Override
                public void onActivityCreated(@Nullable Bundle savedInstanceState) {
                    // TODO Auto-generated method stub
                    super.onActivityCreated(savedInstanceState);
        
                    et = (EditText) getView().findViewById(R.id.editText1);
                    b = (Button) getView().findViewById(R.id.button1);
                    tv = (TextView) getView().findViewById(R.id.textView1);
                    b.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View arg0) {
                            tv.setText("Pressed");
                        }
                    });
                }
            }
        }
        

        输出:

        enter image description here

答案 1 :(得分:0)

我认为您分享的xml是fragment_main而不是activity_main。确保你正在膨胀正确的xml并仅引用它的控件... NULL Pointer Exception仅由于此而生成。

根据您的代码, textview,edittext和按钮必须位于activity_main.xml

 et=(EditText) findViewById(R.id.EditText1);
 b = (Button) findViewById(R.id.Button1);
 tv = (TextView) findViewById(R.id.textView3);

确认。

如果控件位于frgament_main中,则需要使用以下代码:

public class PlaceHolderFragment extends Fragment {
    private EditText et;
    private Button b;
    private TextView tv;

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

        et=(EditText) rootView.findViewById(R.id.EditText1);
        b = (Button) rootView.findViewById(R.id.Button1);
        tv = (TextView) rootView.findViewById(R.id.textView3);
        b.setOnClicklistener(new OnClickListener(){
            @Override
            public void onClick(View arg0){
                tv.setText("Pressed");
            }
        });
        return rootView;
    }

}