【问题标题】:Android Text Messaging安卓短信
【发布时间】: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


【解决方案1】:

你的main.xml必须是这样的,然后只有你才能将sendBtn, message, numberTxt绑定到Activity文件。

<LinearLayout 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" >

    <EditText
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <EditText
        android:id="@+id/numberTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/sendBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</LinearLayout>

【讨论】:

    【解决方案2】:

    试试这个方法,希望能帮助你解决问题。

    ma​​in.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/numberTxt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="phone"
            android:hint="phone no"/>
    
        <EditText
            android:id="@+id/message"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:hint="message"
            android:gravity="top|left"
            android:layout_marginTop="5dp"/>
    
        <Button
            android:id="@+id/sendBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send"
            android:layout_marginTop="5dp"/>
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多