Android的getApplicationContext()错误 [英] getApplicationContext() error Android
问题描述
我有一个片段,允许用户输入消息以及将消息传递到的电话号码.我收到一个错误无法解析方法getApplicationContext()",我在这里查看了答案该方法getApplicationContext()是未定义的,但是它没有帮助我,也许我实现错误,但是我不确定!此代码可以很好地用作活动,但不能作为片段.
I have a fragment that allows a user to enter in a message and a phone number to which the message will be delivered. I am getting an error "cannot resolve method getApplicationContext()" I have looked at the answer here the method getApplicationContext() is undefined but it didn't help me, maybe I am implementing it wrong but I am not sure! This code works fine as an activity but not as a fragment.
FragmentTab1类
package com.androidbegin.absfragtabhost;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Fragment;
import android.app.Activity;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class FragmentTab3 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenttab3, container, false);
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
sendBtn = (Button) rootView.findViewById(R.id.btnSendSMS);
txtphoneNo = (EditText) rootView.findViewById(R.id.editTextPhoneNo);
txtMessage = (EditText) rootView.findViewById(R.id.editTextSMS);
sendBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
});
return rootView;
}
Button sendBtn;
EditText txtphoneNo;
EditText txtMessage;
protected void sendSMSMessage() {
Log.i("Send SMS", "");
String phoneNo = txtphoneNo.getText().toString();
String message = txtMessage.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS failed, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
推荐答案
类Fragment
中不存在getApplicationContext()方法.但是,它确实存在于类Activity
中,因此您可以使用getActivity().getApplicationContext()
从片段对象中获取上下文. (假设该片段已附加到活动中,因此getActivity()
将返回一个非空对象,通常为true)
The method getApplicationContext() does not exist in the class Fragment
. It does exist, however, in the class Activity
, so you can use getActivity().getApplicationContext()
to get the context from within a fragment object. (Assuming the fragment is attached to an activity so getActivity()
will return a non-null object, which is usually true)
这篇关于Android的getApplicationContext()错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!