【发布时间】:2014-09-08 06:22:21
【问题描述】:
现在,我正在处理 android 短信,但是在这几行中它变成了错误,说 (sendBtn,message,numberTxt) cannot be resolved or its not a field. 任何人都可以帮我解决这个错误吗?
sendSMS = (Button) findViewById(R.id.sendBtn);
msgTxt = (EditText) findViewById(R.id.message);
numTxt = (EditText) findViewById(R.id.numberTxt);
这是我的完整代码:
package com.hp.message;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.widget.Toast;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.telephony.SmsManager;
import android.view.View;
public class Main extends ActionBarActivity {
Button sendSMS;
EditText msgTxt;
EditText numTxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendSMS = (Button) findViewById(R.id.sendBtn);
msgTxt = (EditText) findViewById(R.id.message);
numTxt = (EditText) findViewById(R.id.numberTxt);
sendSMS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
String myMsg = msgTxt.getText().toString();
String theNumber = numTxt.getText().toString();
sendMsg(theNumber, myMsg);
}
});
}
protected void sendMsg(String theNumber, String myMsg) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(theNumber, null, myMsg, null, null);
}
}
Main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hp.message.Main" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
而且我已经通过添加 AndroidManifest.xml 获得了许可
【问题讨论】:
-
main.xml的邮政编码 -
你获得许可了吗?
-
您的
main.xml是否定义了按钮? -
@SilentKiller 已经将其包含在下面...请参阅编辑后的帖子
-
@PratikButani 是的,已经......如那里所写(已编辑帖子)
标签: android eclipse sms messaging