मेरे पास एक टुकड़ा है जो उपयोगकर्ता को एक संदेश और फ़ोन नंबर में प्रवेश करने की अनुमति देता है जिस पर संदेश वितरित किया जाएगा। मुझे एक त्रुटि मिल रही है "विधि getAplicationContext() को हल नहीं कर सकता है" मैंने जवाब the method getApplicationContext() is undefined पर देखा है, लेकिन इससे मेरी मदद नहीं हुई, शायद मैं इसे गलत कार्यान्वित कर रहा हूं लेकिन मुझे यकीन नहीं है! यह कोड एक गतिविधि के रूप में ठीक काम करता है लेकिन एक टुकड़े के रूप में नहीं।getAplicationContext() त्रुटि एंड्रॉइड
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;
}
}