Android學習--寫一個發送短信的apk,注意佈局文件的處理過程!!!

剛開始寫Android程序如圖發現使用了findViewById方法以後輸出的話竟然是null(空指針錯誤),也就是說這個方法沒有成功。網上說這樣寫是在activity_main .xml去找這個ID所表明的控件,而如今使用的ADT在layout下除了activity_main .xml還多生成了一個fragment_main.xml。我就是把控件寫在了後一個佈局文件中。想請問下,使用什麼樣的方法是在fragment_main.xml去尋找控件呢?java

回答:在PlaceHolderFragment的onCreateView裏面初始化textView.android

http://bbs.csdn.net/topics/390740123?page=1#post-397370237這個帖子寫的特好,大拇指豎起來!app

 

  1 package com.example.sms;
  2 
  3 import java.util.ArrayList;
  4 
  5 import android.support.v7.app.ActionBarActivity;
  6 import android.support.v7.app.ActionBar;
  7 import android.support.v4.app.Fragment;
  8 import android.telephony.SmsManager;
  9 import android.os.Bundle;
 10 import android.view.LayoutInflater;
 11 import android.view.Menu;
 12 import android.view.MenuItem;
 13 import android.view.View;
 14 import android.view.ViewGroup;
 15 import android.widget.EditText;
 16 import android.widget.Toast;
 17 import android.os.Build;
 18 
 19 public class MainActivity extends ActionBarActivity {
 20     
 21     static EditText mETNumber;
 22     static EditText mETContent;
 23 
 24     @Override
 25     protected void onCreate(Bundle savedInstanceState) {
 26         super.onCreate(savedInstanceState);
 27         setContentView(R.layout.activity_main);
 28 
 29         if (savedInstanceState == null) {
 30             getSupportFragmentManager().beginTransaction()
 31                     .add(R.id.container, new PlaceholderFragment()).commit();
 32         }
 33     }
 34 
 35     
 36     
 37     @Override
 38     public boolean onCreateOptionsMenu(Menu menu) {
 39 
 40         // Inflate the menu; this adds items to the action bar if it is present.
 41         getMenuInflater().inflate(R.menu.main, menu);
 42         return true;
 43     }
 44     
 45     
 46     
 47     
 48 
 49     @Override
 50     public boolean onOptionsItemSelected(MenuItem item) {
 51         // Handle action bar item clicks here. The action bar will
 52         // automatically handle clicks on the Home/Up button, so long
 53         // as you specify a parent activity in AndroidManifest.xml.
 54         int id = item.getItemId();
 55         if (id == R.id.action_settings) {
 56             return true;
 57         }
 58         return super.onOptionsItemSelected(item);
 59     }
 60 
 61     
 62     
 63     
 64     /**
 65      * A placeholder fragment containing a simple view.
 66      */
 67     public static class PlaceholderFragment extends Fragment {
 68 
 69         public PlaceholderFragment() {
 70         }
 71 
 72         @Override
 73         public View onCreateView(LayoutInflater inflater, ViewGroup container,
 74                 Bundle savedInstanceState) {
 75             View rootView = inflater.inflate(R.layout.fragment_main, container,
 76                     false);
 77             
 78             //因爲在fragment_main裏面定義了,所以須要在這裏定義
 79             mETNumber =(EditText) rootView.findViewById(R.id.et_number);
 80             mETContent = (EditText) rootView.findViewById(R.id.et_content);
 81             
 82             
 83             return rootView;
 84         }
 85     }
 86 
 87     public void send(View v){
 88     String number = mETNumber.getText().toString().trim();
 89     String content = mETContent.getText().toString().trim();
 90         
 91     if("".equals(number)||"".equals(content)){
 92         Toast.makeText(this, R.string.error_message, Toast.LENGTH_LONG).show();//添加show才能夠顯示出來
 93     }else{
 94         //這是發送一條常規短信的寫法
 95         //SmsManager.getDefault().sendTextMessage(number, null, content, null, null); //須要受權,請注意添加
 96         //Toast.makeText(this,R.string.success_message, Toast.LENGTH_LONG).show();
 97         SmsManager smsmanager = SmsManager.getDefault();
 98         ArrayList<String> messages = smsmanager.divideMessage(content);//ArrayList是個集合,裏面的類型是String類型的,這是根據dividemessage的補充說明添加的
 99         for(String message:messages){
100             smsmanager.sendTextMessage(number, null, message, null, null);
101             
102         }
103         
104     }
105 }}

 

過去沒有fragment_main.xml的時候,在onCreate()方法裏面能夠隨便寫code;如今通過試驗,仍是在fragment裏面佈局,方便拖動,寫code時候找到OnCreateView()方法,在裏面寫寫就OK啦。。。ide

另一個收穫是,apk中涉及走流量或者花錢的,須要受權,直接在menifest裏面受權便可。佈局

相關文章
相關標籤/搜索