Android中getApplicationContext()错误

9
我有一个片段,允许用户输入消息和要接收消息的电话号码。我遇到了一个错误:"cannot resolve method getApplicationContext()"。我查看了这里的答案 the method getApplicationContext() is undefined,但对我没有帮助,也许我实现有误,但我不确定!这段代码作为活动是正常工作的,但作为片段则不行。 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;
    }


}
7个回答

38

Fragment类中不存在getApplicationContext()方法,但是在Activity类中该方法是存在的。因此,您可以使用getActivity().getApplicationContext()从一个片段对象中获取上下文环境(假设该片段已经依附于一个活动,并且getActivity()会返回一个非null对象,这通常是正确的)。


6

Use getActivity().getApplicationContext();


1
你需要在一个非活动类中创建一个构造函数,该构造函数需要接受一个上下文参数。在你的活动类中,你需要在对象声明时传递上下文,像这样:
//ON non activity class :
public class longcomputingtask extends AsyncTask<Void,Integer,Void> {

    private  Context context ;

    public longcomputingtask()
    {
    }

    public longcomputingtask(Context context)
    {
        this.context=context;
    }

//   on activity class :

public Context context;
context=getApplication().getBaseContext();

longcomputingtask calcul =new longcomputingtask(context):

1

1

0

getApplicationContext()在Fragment类中不存在或者说没有定义,这意味着你不能直接在Fragment类中访问它。

因此解决方案是,不要像这样做

Toast.makeText(getApplicationContext(), "SMS sent.",Toast.LENGTH_LONG).show();

你应该像下面这样使用getActivity()方法

Toast.makeText(getActivity().getApplicationContext(), "SMS sent.",Toast.LENGTH_LONG).show();

希望它有所帮助。


0

在这里查看答案:在片段中使用上下文

只需调用getActivity(),确保它不为空,并在其上调用getApplicationContext()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接