How to Call BroadcastReceiver from Activity via Interface in Android - java

I need to set Listener for Activity Class from BroadcastReceiver Class But here in my below code I am getting null pointer exception in BroadcastReceiver class..
Here is my code
Activity Class
public class MainActivity extends ActionBarActivity implements TheListener
{
Message_Incomingsms jv_class_Message_Incomingsms;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jv_class_Message_Incomingsms = new Message_Incomingsms();
jv_class_Message_Incomingsms.setSmsReceivedistener(this);
}
#Override
public void onSmsReceived(String jv_string_address, String jv_string_MessageText)
{
Toast.makeText(getApplicationContext(),"This Message is from Lister number is "+jv_string_address +" Message is "+jv_string_MessageText,Toast.LENGTH_LONG).show();
//Doing some Activity.....
}
}
Interface
public void setSmsReceivedistener(Context context)
{
this.jv_TheListener = (TheListener)context;
}
BroadCastReceiver Class
public class Message_Incomingsms extends BroadcastReceiver
{
String jv_string_phone_received_Number;
String jv_string_phone_received_messageText;
public TheListener jv_TheListener;
#Override
public void onReceive(Context context, Intent intent)
{
Log.d("Received","OnReceive method is called");
jv_smsmanager_incoming_sms = SmsManager.getDefault();
jv_bundle_incoming_sms = intent.getExtras();
if(jv_bundle_incoming_sms!=null)
{
Object[] jv_pdusObj_incoming_sms = (Object[]) jv_bundle_incoming_sms.get("pdus");
for(int jv_i = 0; jv_i < jv_pdusObj_incoming_sms.length ;jv_i++)
{
SmsMessage jv_current_message = SmsMessage.createFromPdu((byte[]) jv_pdusObj_incoming_sms[jv_i]);
jv_string_phone_received_Number =jv_current_message.getDisplayOriginatingAddress();
jv_string_phone_received_messageText = jv_current_message.getDisplayMessageBody();
}
//Here Below line is problem for me Always am getting null value for (jv_TheListener)
//Why value is not getting assigned for this (jv_TheListener) always
jv_TheListener.onSmsReceived(jv_string_phone_received_Number,jv_string_phone_received_messageText);
}
}
public void setSmsReceivedistener(Context context)
{
this.jv_TheListener = (TheListener)context;
}
}
Please provide me solution for above BroadCastClass code where am always getting null exception for (jv_TheListener)
log Cat ( I removed some lines for in code for not to confuse.. so dont refer linenumber based on below error)
04-18 23:45:12.268 4064-4064/com.talkeasy.elavarasans.creativeactionbar E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start receiver com.talkeasy.elavarasans.creativeactionbar.Message_Incomingsms: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2153)
at android.app.ActivityThread.access$1500(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4448)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.talkeasy.elavarasans.creativeactionbar.Message_Incomingsms.onReceive(Message_Incomingsms.java:53)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2146)
            at android.app.ActivityThread.access$1500(ActivityThread.java:127)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
Thanks in Advance...

Related

Error using setText in runInUiThread [duplicate]

This question already has answers here:
android.content.res.Resources$NotFoundException: String resource ID #0x0
(8 answers)
Closed 6 years ago.
I need a thread that waits some time then changes a text in a TextView.
My search found how to use runOnUiThread
I understood the first answer and tried to use it in my code but I get an error when calling setText in the thread.
Here is my code
(start is the onClick function of a button):
public class MainActivity extends AppCompatActivity {
public int i = 0;
private TextView mText;
private Thread thread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mText = (TextView)findViewById(R.id.text);
}
public void start (View v) {
runThread();
}
private void runThread() {
new Thread() {
public void run() {
while (true) {
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
i++;
mText.setText((i));
}
});
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
Here is my error
09-26 20:26:34.054 22254-22254/com.horizon.testtimerthread E/AndroidRuntime: FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:1058)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3866)
at com.horizon.testtimerthread.MainActivity$1$1.run(MainActivity.java:39)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:177)
at android.app.ActivityThread.main(ActivityThread.java:4947)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Thank you in advance for your help.
mText.setText((i)) is the problem.
Android thinks that you are referencing a String ID and is crashing because it cannot find a String with an ID of 1. If you meant to set the text to "1", "2" ect... then write mText.setText((Integer.toString(i)))
Since i has type int, you are calling setText(int resourceId) variant of function, what is obvious not you want. setText(String.valueOf(i)) should work.

Keep getting a NullPointerException for MediaPlayer [duplicate]

This question already exists:
MediaPlayer NullPointerException in local method [duplicate]
Closed 7 years ago.
There is an annoying bug in my code somewhere and I can't sleep because of it!
I kind of get why I might be getting it - I think something to do with onCreate method I think.
This is what I have:
public class PredefinedUserPlayer extends Activity {
MediaPlayer mediaPlayer;
String names;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userdefplayer);
names = "http://X.X.X.X/musikz/musikz01.mp3";
mediaPlayer = new MediaPlayer();
}
public void playSelectedFile() {
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(PredefinedUserPlayer.this, Uri.parse(names));
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
}
The playSelectedFile() method is being called in another class and here is that bit of code here:
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PredefinedUserPlayer predefinedUserPlayer = new PredefinedUserPlayer()
predefinedUserPlayer.playSelectedFile();
}
});
I can't really have the MediaPlayer stuff in the above class which is why I have had to instantiate the PredefinedUserPlayer class. This is the actual stack trace (maybe someone can help me debug and fix this annoying bug):
java.lang.NullPointerException
at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:99)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:882)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:859)
at lukasz.musik.PredefinedUserPlayer.playSelectedFile(PredefinedUserPlayer.java:86)
at lukasz.musik.CustomMusicAdapter$ViewHolder$1.onClick(CustomMusicAdapter.java:85)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
You can't new activity by hand like this:
PredefinedUserPlayer predefinedUserPlayer = new PredefinedUserPlayer()
You can use Broadcast to achieve it,Like this:
**First,Register a BroadcastReceiver in PredefinedUserPlayer Activity:
public static String ACTION_PLAY = "ACTION_PLAY_FILE";
private final BroadcastReceiver mPlayFileReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (ACTION_PLAY.equals(action)) {
playSelectedFile();
}
}
};
IntentFilter intentFilter = new IntentFilter(ACTION_PLAY);
LocalBroadcastManager.getInstance(this).registerReceiver(mPlayFileReceiver,intentFilter);
Second,Send Broadcast with action ACTION_PLAY to inform the receiver to be invoked:
LocalBroadcastReceiver.getInstance(this).sendBroadcastSync(new Intent(PredefinedUserPlayer.ACTION_PLAY));

Unable to create and run app lock service

I have been trying to build a project named App Lock.
My problem is that whenever i try to run the project it shows a message Unfortunately App Lock has been stopped.
Log cat :-
01-15 10:29:58.322 31941-31941/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.pk.applock, PID: 31941
java.lang.RuntimeException: Unable to start service com.pk.applock.ApplockService#21d15610 with Intent { act=com.pk.applock.applock_service.start flg=0x4 cmp=com.pk.applock/.ApplockService (has extras) }: java.lang.NullPointerException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2886)
at android.app.ActivityThread.access$2100(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.pk.applock.ApplockService.getTopTask(ApplockService.java:37)
at com.pk.applock.ApplockService.checkPackageChanged(ApplockService.java:86)
at com.pk.applock.ApplockService.onStartCommand(ApplockService.java:110)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2869)
at android.app.ActivityThread.access$2100(ActivityThread.java:139) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:149) 
at android.app.ActivityThread.main(ActivityThread.java:5257) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) 
at dalvik.system.NativeStart.main(Native Method) 
Here is the complete code of ApplockService.class
public class ApplockService
extends Service{
private Handler handler;
private static PendingIntent pendingIntent;
private String packageName;
private Map<String,Boolean> lockedPackages;
Intent intent;
private ActivityManager manager;
Context context;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
private ActivityManager.RunningTaskInfo getTopTask()
{
return (ActivityManager.RunningTaskInfo)this.manager.getRunningTasks(1).get(0);
}
private boolean init()
{
handler=new Handler();
lockedPackages=new HashMap();
Iterator iterator=PrefUtils.getLocked((Context)this).iterator();
do {
if(!iterator.hasNext()) {
ApplockService.startAlarm((Context)this);
return true;
}
String string=(String)iterator.next();
lockedPackages.put(string,true);
}while (true);
}
private static void startAlarm(Context context)
{
AlarmManager alarmManager=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
PendingIntent repeated_intent=ApplockService.getRunIntent(context);
alarmManager.setRepeating(3, SystemClock.elapsedRealtime(),5,repeated_intent);
}
private static final PendingIntent getRunIntent(Context context)
{
if(pendingIntent==null)
{
Intent intent1=new Intent(context,ApplockService.class);
intent1.setAction("com.pk.applock.applock_service.start");
pendingIntent=PendingIntent.getService(context,1193135,intent1,0);
}
return pendingIntent;
}
public static final void start(Context context)
{
ApplockService.startAlarm(context);
}
public void showLocker(String packageName)
{
Intent intent=LockService.getLockIntent((Context)this,packageName);
intent.setAction(LockService.ACTION_COMPARE);
intent.putExtra(LockService.EXTRA_PACKAGENAME, packageName);
startService(intent);
}
public void checkPackageChanged()
{
String string=getTopTask().topActivity.getPackageName();
onAppOpen(string);
}
private void onAppOpen(String string)
{
if (lockedPackages.containsKey(string))
onLockedAppOpen(string);
}
private void onLockedAppOpen(String string)
{
if(this.lockedPackages.get(string).booleanValue())
{
showLocker(string);
}
}
#Override
public int onStartCommand(Intent intent,int n2,int n3)
{
if(intent==null||"com.pk.applock.applock_service.start".equals(intent.getAction()))
{
if(!init())
init();
checkPackageChanged();
return 2;
}
return 2;
}
}
Please help me!!!
I am new to android!
Thanks in advance.
Seem like manager object is not set yet in ApplockService class. You may put this code to init() function:
this.manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

Android communication between activity and service

I'm trying to implement a system service on Android with inter-process communication based on binding a messenger. For this I'm following this tutorial:
http://www.survivingwithandroid.com/2014/01/android-bound-service-ipc-with-messenger.html
But no matter what I'm trying, i can't get it to work.
I found different tutorials online but in the end they are all based on the same procedure.
The app will crash right away when I'm trying to communicate with the service.
Error message
10-09 15:40:33.490 3468-3468/com.example.stenosis.testservice E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.stenosis.testservice, PID: 3468
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.stenosis.testservice/com.example.stenosis.testservice.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.stenosis.testservice.MyActivity.sendMsg(MyActivity.java:87)
at com.example.stenosis.testservice.MyActivity.onCreate(MyActivity.java:49)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
MyActivity.java:
public class MyActivity extends Activity {
private ServiceConnection sConn;
private Messenger messenger;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Service Connection to handle system callbacks
sConn = new ServiceConnection() {
#Override
public void onServiceDisconnected(ComponentName name) {
messenger = null;
}
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
// We are conntected to the service
messenger = new Messenger(service);
}
};
// We bind to the service
bindService(new Intent(this, MyService.class), sConn, Context.BIND_AUTO_CREATE);
// Try to send a message to the service
sendMsg();
}
// This class handles the Service response
class ResponseHandler extends Handler {
#Override
public void handleMessage(Message msg) {
int respCode = msg.what;
String result = "";
switch (respCode) {
case MyService.TO_UPPER_CASE_RESPONSE: {
result = msg.getData().getString("respData");
System.out.println("result");
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
}
}
public void sendMsg() {
String val = "This is a test";
Message msg = Message
.obtain(null, MyService.TO_UPPER_CASE);
msg.replyTo = new Messenger(new ResponseHandler());
// We pass the value
Bundle b = new Bundle();
b.putString("data", val);
msg.setData(b);
try {
messenger.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
MyService.java
public class MyService extends Service {
public static final int TO_UPPER_CASE = 0;
public static final int TO_UPPER_CASE_RESPONSE = 1;
private Messenger msg = new Messenger(new ConvertHanlder());;
#Override
public IBinder onBind(Intent intent) {
return msg.getBinder();
}
class ConvertHanlder extends Handler {
#Override
public void handleMessage(Message msg) {
// This is the action
int msgType = msg.what;
switch (msgType) {
case TO_UPPER_CASE: {
try {
// Incoming data
String data = msg.getData().getString("data");
Message resp = Message.obtain(null, TO_UPPER_CASE_RESPONSE);
Bundle bResp = new Bundle();
bResp.putString("respData", data.toUpperCase());
resp.setData(bResp);
msg.replyTo.send(resp);
} catch (RemoteException e) {
e.printStackTrace();
}
break;
}
default:
super.handleMessage(msg);
}
}
}
}
AndroidManifest.xml
<service android:name=".MyService" android:process=":convertprc"/>
Your problem is that the messanger property is null. You are experiencing concurrency problem. The bindService method is asynchronous - it returns immediately and performs the binding in a background thread. When the binding is done, the onServiceConnected method is invoked. When you try to send the message, the messanger is still not initialized because the onServiceConnected method is hasn't been executed yet.

Throws NullPointerException while retrieving data from SharedPreferences

First of all, sorry for my bad English !
I have an Activity ( say MainActivity.java ) and a class ( say AsynkTaskClass.java ).
AsynkTaskClass.java contains an AsynkTask. I am calling this AsynkTask from MainActivity.java.
Before calling this AsynkTask, I am saving some data using SharedPreferences.
From the onPostExecute function of AsynkTask, I am calling a normal function ( say displaySharedData() )which defined in MainActivity.java. There I am trying to retrieve the data I stored using SharedPreferences. Then It shows a NullPointerException. But I can access the Shared data from anywhere other than this displaySharedData() function.
This is the Log Output:
W/dalvikvm(3896): threadid=1: thread exiting with uncaught exception
E/AndroidRuntime(3896): FATAL EXCEPTION: main
E/AndroidRuntime(3896): java.lang.NullPointerExceptionE/AndroidRuntime(3896): at com.samApp.project.samApp.ui.MainActivity.displaySharedData(MainActivity.java:52)
E/AndroidRuntime(3896): at com.samApp.project.samApp.s3.AsynkTaskClass$UploadImage.onPostExecute(AsynkTaskClass.java:264)
E/AndroidRuntime(3896): at com.samApp.project.samApp.s3.AsynkTaskClass$UploadImage.onPostExecute(AsynkTaskClass.java:1)
E/AndroidRuntime(3896): at android.os.AsyncTask.finish(AsyncTask.java:417)
E/AndroidRuntime(3896): at android.os.AsyncTask.access$300(AsyncTask.java:127)
E/AndroidRuntime(3896): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
E/AndroidRuntime(3896): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(3896): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(3896): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(3896): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(3896): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(3896): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(3896): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(3896): at dalvik.system.NativeStart.main(Native Method)
MainActivity.java
public class MainActivity extends Activity implements TextView.OnEditorActionListener {
public SharedPreferences myData;
public String mySharedData="com.samApp.project.samApp.ui";
public SharedPreferences.Editor editor;
protected void onCreate(Bundle savedBundle) {
super.onCreate(savedBundle);
myData = PreferenceManager.getDefaultSharedPreferences(this);
editor = myData.edit();
editor.commit();
attachfileButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PHOTO_SELECTED);
}
});
}
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case PHOTO_SELECTED:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
Log.d("URIsssss", String.valueOf(selectedImage));
AsynkTaskClass obj = new AsynkTaskClass ();
AsynkTaskClass.s3context=this;
obj.new UploadImage().execute(selectedImage);
}
}
}
}
protected void onResume() {
super.onResume();
editor.putString("name", "MeSSi");
editor.commit();
}
public void displaySharedData()
{
Log.d("SharedData",myData.getString("name","")); // line No : 52
}
}
AsynkTaskClass.java
public class AsynkTaskClass {
MainActivity mainActvtyObj = new MainActivity();
public static Context s3context;
protected void onPostExecute(S3TaskResult result) {
if(dialog != null)
dialog.dismiss();
if (result.getErrorMessage() != null) {
displayErrorAlert(
AsynkTaskClass.s3context.getString(R.string.upload_failure_title),
result.getErrorMessage());
}
mainActvtyObj.displaySharedData();
}
}
So, Any Help ??
MainActivity mainActvtyObj = new MainActivity();
Never instantiate activity classes with new.
For example, their lifecycle callbacks such as onCreate() are not run. You init your myData in onCreate() and it remains null in this activity instance.
Instead, pass a reference to your activity object to the async task as a parameter.
Finally I got a working solution.
I don't know whether It is perfect! But it works well..
I replaced the displaySharedData() calling statement i.e. mainActvtyObj.displaySharedData(); to the following :
((MainActivity) s3context).displaySharedData();
Now It Works Great !!

Categories