1
我创建了一个使用片段的android.I创建了一个sms片段,用于将sms发送到特定的号码,但是当它运行良好但是当我输入消息然后按发送时,应用程序崩溃并关闭。帮帮我。在片段内发送短信应用程序
public class SMS extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// View v = inflater.inflate(R.layout.fragment, null);
View v = inflater.inflate(R.layout.sms, container, false);
return v;
}
public void sendsms(View v)
{
//refernce the edittext
EditText message=(EditText) v.findViewById(R.id.Messege);
//get the phone the number and the message
String number="0712345678";
String msg=message.getText().toString();
//use the sms manager to send message
SmsManager sm=SmsManager.getDefault();
sm.sendTextMessage(number, null, msg, null, null);
Toast.makeText(getActivity(),"Messege sent",Toast.LENGTH_LONG).show();
}
}
以下是我用过的xml布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@drawable/log"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send your feedback" />
<EditText
android:id="@+id/Messege"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:hint="Enter your messege"
android:layout_marginTop="15dp"
android:ems="10" >
</EditText>
<Button
android:id="@+id/send"
android:onClick="sendsms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send SMS" />
</LinearLayout>