alp alp - 2 months ago 13
Android Question

context problems at the activity in the library

When I start a new activity in my library project, this activity seems like having context problems, doesn't make a toast for instance.

Here is my log

05-20 09:59:41.340: W/System.err(4624): at com.companymobile.companycorelibrary.data.smartsync.SmartSyncDataBaseHelper.getLastUpdate(SmartSyncDataBaseHelper.java:292)
05-20 09:59:41.340: W/System.err(4624): at com.companymobile.companycorelibrary.data.smartsync.SmartSyncManager.getActivities(SmartSyncManager.java:213)
05-20 09:59:41.340: W/System.err(4624): at com.companymobile.companyviewlibrary.activities.EventsActivity.onCreate(EventsActivity.java:60)
05-20 09:59:41.340: W/System.err(4624): at android.app.Activity.performCreate(Activity.java:4469)
05-20 09:59:41.340: W/System.err(4624): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
05-20 09:59:41.340: W/System.err(4624): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
05-20 09:59:41.340: W/System.err(4624): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
05-20 09:59:41.340: W/System.err(4624): at android.app.ActivityThread.access$600(ActivityThread.java:127)
05-20 09:59:41.340: W/System.err(4624): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
05-20 09:59:41.340: W/System.err(4624): at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 09:59:41.340: W/System.err(4624): at android.os.Looper.loop(Looper.java:137)
05-20 09:59:41.340: W/System.err(4624): at android.app.ActivityThread.main(ActivityThread.java:4507)
05-20 09:59:41.340: W/System.err(4624): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 09:59:41.340: W/System.err(4624): at java.lang.reflect.Method.invoke(Method.java:511)
05-20 09:59:41.340: W/System.err(4624): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
05-20 09:59:41.340: W/System.err(4624): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
05-20 09:59:41.340: W/System.err(4624): at dalvik.system.NativeStart.main(Native Method)
05-20 09:59:41.370: W/System.err(4624): com.novoda.location.exception.NoProviderAvailable
05-20 09:59:41.370: W/System.err(4624): at com.novoda.location.LocationUpdateManager.startActiveLocationUpdates(LocationUpdateManager.java:50)
05-20 09:59:41.370: W/System.err(4624): at com.novoda.location.DefaultLocator.startListeningForLocationUpdates(DefaultLocator.java:128)
05-20 09:59:41.370: W/System.err(4624): at com.novoda.location.DefaultLocator.startActiveLocationUpdates(DefaultLocator.java:94)
05-20 09:59:41.370: W/System.err(4624): at com.companymobile.companyviewlibrary.activities.EventsActivity.onResume(EventsActivity.java:118)
05-20 09:59:41.370: W/System.err(4624): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1157)
05-20 09:59:41.370: W/System.err(4624): at android.app.Activity.performResume(Activity.java:4543)
05-20 09:59:41.370: W/System.err(4624): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2446)
05-20 09:59:41.370: W/System.err(4624): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2484)
05-20 09:59:41.370: W/System.err(4624): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1998)
05-20 09:59:41.370: W/System.err(4624): at android.app.ActivityThread.access$600(ActivityThread.java:127)
05-20 09:59:41.370: W/System.err(4624): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
05-20 09:59:41.370: W/System.err(4624): at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 09:59:41.370: W/System.err(4624): at android.os.Looper.loop(Looper.java:137)
05-20 09:59:41.370: W/System.err(4624): at android.app.ActivityThread.main(ActivityThread.java:4507)
05-20 09:59:41.370: W/System.err(4624): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 09:59:41.370: W/System.err(4624): at java.lang.reflect.Method.invoke(Method.java:511)
05-20 09:59:41.370: W/System.err(4624): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
05-20 09:59:41.370: W/System.err(4624): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
05-20 09:59:41.370: W/System.err(4624): at dalvik.system.NativeStart.main(Native Method)


this is my main project which will call library activity.

public class TestActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Intent intent = new Intent(this, com.companymobile.companyviewlibrary.activities.EventsActivity.class);
startActivity(intent);
}
}


This is my library activity.

public class EventsActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(null);
setContentView(R.layout.activity_events);
Toast.makeText(getBaseContext(), "create", Toast.LENGTH_SHORT).show();
Log.e("SmartSyncManager", ""+"create");


when new activity starts, I can see its layout but not logs and toasts.
I have tried
getApplicationContext()
,
EventsActivity.this getBaseContext()
, yet none could solve the situation.

Answer

You should use the activity to pass through to the Toast function. So either try

Toast.makeText(this, "create", Toast.LENGTH_SHORT).show();

or

Toast.makeText(EventsActivity.this, "create", Toast.LENGTH_SHORT).show();

Perhaps the line: super.onCreate(null); is incorrect, and the bundle should be parsed to it.