2014-10-29 166 views
-1

我是Android新手,所以我面臨很多問題。我要做一個簡單的程序,它可以從edittext中獲取一個值,並通過單擊按鈕在textview中打印它。這裏是我的代碼: enter image description heresetText和OnClickListener不起作用

程序說,不幸的是它停止工作。但是,當我從setOnClickListener刪除最後幾行時,它工作正常。我也只通過setText測試了它。它也不起作用。謝謝。 logcat的: enter image description here

XML: enter image description here

+0

後你在這裏logcat的..在什麼情況下會 – codePG 2014-10-29 04:01:35

+0

'savedInstanceState'爲空? – Sriram 2014-10-29 04:04:51

+0

我不知道。它是自動生成的。 – 2014-10-29 04:06:31

回答

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中

enter image description here

  • fragment_main.xml
  • enter image description here

  • MainActivity.java
  •  
    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

    +0

    它將工作相同,因爲我們把代碼onCreate .. – 2014-10-29 04:12:46

    +0

    它將如何工作相同的代碼在onCreate ..你有活動主要相同的控制,如果你有意思你做錯了方式..先學習機器人,然後做代碼。 – Gunaseelan 2014-10-29 04:16:56

    +0

    你的控件應該是fragment_main。 – Gunaseelan 2014-10-29 04:17:52

    0

    我想你共享XML是fragment_mainactivity_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; 
        } 
    
    }