getApplicationContext() error Android

2019-01-24 09:24发布

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 class

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条回答
Luminary・发光体
2楼-- · 2019-01-24 10:02

Maybe it's enough with getActivity() as Activity extends Context.

Anyway, please consider whether you need a Context with the lifecycle separated from you Activity's Context to know if you need to use getActivity().getApplicationContext() or just getActivity().

Refer to this thread for more info.

查看更多
Melony?
3楼-- · 2019-01-24 10:05

Use getActivity().getApplicationContext();

查看更多
Deceive 欺骗
4楼-- · 2019-01-24 10:06

You should use the Application context available in the Fragment Activity

getActivity().getApplicationContext();

You shouldn't use the getActivity() as context here because Toast can live longer than the activity that showed it (Home button as been pressed, another activity has been launched, etc.)

http://developer.android.com/guide/topics/ui/notifiers/toasts.html#Basics

查看更多
ら.Afraid
5楼-- · 2019-01-24 10:08

You have to create on non activity class a constructor that takes a context on argument, and on your activity class, you have to pass the context on declaration of object, like this:

//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):
查看更多
叛逆
6楼-- · 2019-01-24 10:21

See the answer here: using context in fragment.

Just call getActivity(), make sure it's not null, and call getApplicationContext() on that.

查看更多
Deceive 欺骗
7楼-- · 2019-01-24 10:24

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)

查看更多
登录 后发表回答