Trying to migrate from Parse to OneSignal and I am stuck on how to start a new Activity after the user clicks on a push notification. My handler is working, the log shows the text, the issue seems to be how to gain access to the application context from within my push opened handler. The example code is vague, getApplicationContext() does not work without first doing something else.
One post I came upon, unrelated to OneSignal, suggests extending the Application class to gain access to the application context. This did not produce any syntax errors but my app crashes.
Code:
package com.linkedresponder.onesignal;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import org.json.JSONObject;
class NotificationOpenHandler extends Application implements OneSignal.NotificationOpenedHandler {
// This fires when a notification is opened by tapping on it.
private Context mContext;
#Override
public void onCreate() {
this.mContext = getApplicationContext();
}
#Override
public void notificationOpened(OSNotificationOpenResult result) {
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String customKey;
if (data != null) {
customKey = data.optString("customkey", null);
if (customKey != null) {
Log.i("OneSignalExample", "customkey set with value: " + customKey);
} else {
Log.i("OneSignalExample", "No data");
}
}
if (actionType == OSNotificationAction.ActionType.ActionTaken)
Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID);
Intent intent = new Intent(mContext, PushClicked.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
Error:
er.onesignal E/AndroidRuntime: FATAL EXCEPTION: main Process: com.linkedresponder.onesignal, PID: 5680
java.lang.RuntimeException: Unable to start receiver com.onesignal.NotificationOpenedReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3018)
at android.app.ActivityThread.-wrap18(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:128)
at android.content.Intent.<init>(Intent.java:4868)
at com.linkedresponder.onesignal.NotificationOpenHandler.notificationOpened(NotificationOpenHandler.java:41)
at com.onesignal.OneSignal.fireNotificationOpenedHandler(OneSignal.java:1009)
at com.onesignal.OneSignal.runNotificationOpenedCallback(OneSignal.java:954)
at com.onesignal.OneSignal.handleNotificationOpen(OneSignal.java:1041)
at com.onesignal.NotificationOpenedProcessor.processIntent(NotificationOpenedProcessor.java:101)
at com.onesignal.NotificationOpenedProcessor.processFromActivity(NotificationOpenedProcessor.java:57)
at com.onesignal.NotificationOpenedReceiver.onReceive(NotificationOpenedReceiver.java:11)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3011)
at android.app.ActivityThread.-wrap18(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Here's how I made it work:
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
OneSignal.startInit(this)
.setNotificationOpenedHandler(new MyNotificationOpenedHandler(this))
.init();
}
}
The NotificationOpenedHandler custom class
public class MyNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
private Application application;
public MyNotificationOpenedHandler(Application application) {
this.application = application;
}
#Override
public void notificationOpened(OSNotificationOpenResult result) {
// Get custom datas from notification
JSONObject data = result.notification.payload.additionalData;
if (data != null) {
String myCustomData = data.optString("key", null);
}
// React to button pressed
OSNotificationAction.ActionType actionType = result.action.type;
if (actionType == OSNotificationAction.ActionType.ActionTaken)
Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID);
// Launch new activity using Application object
startApp();
}
private void startApp() {
Intent intent = new Intent(application, MyActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
application.startActivity(intent);
}
}
Don't forget to add this to your manifest:
<application ...>
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
</application>
The short answer to this issue is to include your handler for the push notification open within the same class where you initialize OneSignal:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Init OneSignal
OneSignal.startInit(this).setNotificationOpenedHandler(new NotificationOpenHandler()).init();
Toolbar mToolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_launcher);
getSupportActionBar().setDisplayShowTitleEnabled(false);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new RecordingsFragment())
.commit();
}
}
class NotificationOpenHandler implements OneSignal.NotificationOpenedHandler {
// This fires when a notification is opened by tapping on it.
#Override
public void notificationOpened(OSNotificationOpenResult result) {
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String stationName = data.optString("stationName");
String timestamp = data.optString("timestamp");
String filename = data.optString("filename");
String url = getString(R.string.callResourceUrl) + filename;
Intent intent = new Intent(getApplicationContext(), CallActivity.class);
intent.putExtra("stationName", stationName);
intent.putExtra("time", timestamp);
intent.putExtra("url", url);
// intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}
Updated code in 2022
OneSignal.NotificationOpenedHandler replaced by OneSignal.OSNotificationOpenedHandler
and result.notification.payload.additionalData replaced by result.notification.additionalData
Full code
class OneSignalNotificationOpenHandler(private val context : Context) : OneSignal.OSNotificationOpenedHandler {
override fun notificationOpened(result: OSNotificationOpenedResult?) {
if (result == null) return
val type = result.action.type
val data = result.notification.additionalData
val name = data.optString("name")
val intent = Intent(context, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT or Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("name", name)
context.startActivity(intent)
}
}
Add it to OneSignel after OneSignal.initWithContext(this)
OneSignal.setNotificationOpenedHandler(OneSignalNotificationOpenHandler(applicationContext))
Don't forget to Add the following to your AndroidManifest.xml to prevent the launching of your main Activity
<application ...>
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
</application>
Related
I am trying to implement oauth2 to enable users to login with Reddit. I have created my app on reddit with the appropriate redirect uri.
What I did:
A MainActivity with a login button. Clicking the login button, starts the authorization flow. To create the authorization request, we need to pass a pending intent that the library uses to call the appropriate component that we want it to call after authorization is successful.
Problem:
When the pending intent is made using an implicit intent (setting only action string while creating intent), the library gets a cancelled exception while invoking the pending intent. I have mentioned the action string in the intent filter for the MainActivity in manifest file also.
What I have tried:
1. I tried creating pending intent using an explicit intent (defining the activity class I want to open while creating intent), my activity's onStart is getting called with the correct intent.
2. I tried by directly invoking the pending intent (with implicit intent) from the activity itself and it got called successfully.
Observation:
1. If I use an older version of the library (v0.2.0), the pending intent with implicit intent works fine.
Current version of OpenId AppAuth library - 0.7.1
Tested on Android 9 (Pie) - OnePlus 3T
Below is my MainActivity.java
package com.prateekgrover.redditline;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.prateekgrover.redditline.services.RedditAuthService;
import net.openid.appauth.AuthState;
import net.openid.appauth.AuthorizationException;
import net.openid.appauth.AuthorizationRequest;
import net.openid.appauth.AuthorizationResponse;
import net.openid.appauth.AuthorizationService;
import net.openid.appauth.AuthorizationServiceConfiguration;
import net.openid.appauth.TokenRequest;
import net.openid.appauth.TokenResponse;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
private String USED_INTENT = "1";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button loginButton = findViewById(R.id.reddit_login);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Intent intent = new Intent(MainActivity.this, RedditAuthService.class);
// startService(intent);
performRedditAuthAction(MainActivity.this, "com.prateekgrover.redditline.HANDLE_AUTHORIZATION_RESPONSE");
}
});
}
public void performRedditAuthAction(Context context, String actionRedirect) {
String uuid = UUID.randomUUID().toString();
AuthorizationServiceConfiguration serviceConfiguration = new AuthorizationServiceConfiguration(
Uri.parse("https://www.reddit.com/api/v1/authorize") /* auth endpoint */,
Uri.parse("https://www.reddit.com/api/v1/access_token") /* token endpoint */
);
String clientId = "<my client id>";
Uri redirectUri = Uri.parse("com.prateekgrover.redditline://oauth2callback");
AuthorizationRequest.Builder builder = new AuthorizationRequest.Builder(
serviceConfiguration,
clientId,
"code",
redirectUri
);
builder.setState(uuid);
builder.setScopes("identity", "mysubreddits", "read", "save", "submit", "subscribe", "vote");
AuthorizationRequest request = builder.build();
AuthorizationService authorizationService = new AuthorizationService(context);
String action = actionRedirect;
Intent postAuthorizationIntent = new Intent("com.prateekgrover.redditline.HANDLE_AUTHORIZATION_RESPONSE");
PendingIntent pendingIntent = PendingIntent.getActivity(this, request.hashCode(), postAuthorizationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
authorizationService.performAuthorizationRequest(request, pendingIntent);
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent != null && intent.getAction() != null) {
String action = intent.getAction();
switch (action) {
case "com.prateekgrover.redditline.HANDLE_AUTHORIZATION_RESPONSE":
redirectIntent(intent);
break;
default:
}
}
}
private void redirectIntent(#Nullable Intent intent) {
if (!intent.hasExtra(USED_INTENT)) {
handleAuthorizationResponse(intent);
intent.putExtra(USED_INTENT, true);
}
}
private void handleAuthorizationResponse(Intent intent) {
AuthorizationResponse response = AuthorizationResponse.fromIntent(intent);
AuthorizationException error = AuthorizationException.fromIntent(intent);
final AuthState authState = new AuthState(response, error);
if (response != null) {
AuthorizationService service = new AuthorizationService(this);
service.performTokenRequest(response.createTokenExchangeRequest(), new AuthorizationService.TokenResponseCallback() {
#Override
public void onTokenRequestCompleted(#Nullable TokenResponse tokenResponse, #Nullable AuthorizationException exception) {
if (exception != null) {
} else {
if (tokenResponse != null) {
authState.update(tokenResponse, exception);
System.out.println(tokenResponse.accessToken + " refresh_token " + tokenResponse.refreshToken);
}
}
}
});
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
#Override
protected void onStart() {
super.onStart();
Intent intent = getIntent();
if (intent != null && intent.getAction() != null) {
String action = intent.getAction();
switch (action) {
case "com.prateekgrover.redditline.HANDLE_AUTHORIZATION_RESPONSE":
redirectIntent(intent);
break;
default:
}
}
}
}
Manifest File:
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="com.prateekgrover.redditline.HANDLE_AUTHORIZATION_RESPONSE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Relevant parts of the library - mCompleteIntent is the PendingIntent that I sending to the library
private void extractState(Bundle state) {
if (state == null) {
Logger.warn("No stored state - unable to handle response");
finish();
return;
}
mAuthIntent = state.getParcelable(KEY_AUTH_INTENT);
mAuthorizationStarted = state.getBoolean(KEY_AUTHORIZATION_STARTED, false);
try {
String authRequestJson = state.getString(KEY_AUTH_REQUEST, null);
mAuthRequest = authRequestJson != null
? AuthorizationRequest.jsonDeserialize(authRequestJson)
: null;
} catch (JSONException ex) {
throw new IllegalStateException("Unable to deserialize authorization request", ex);
}
mCompleteIntent = state.getParcelable(KEY_COMPLETE_INTENT);
mCancelIntent = state.getParcelable(KEY_CANCEL_INTENT);
}
private void handleAuthorizationComplete() {
Uri responseUri = getIntent().getData();
Intent responseData = extractResponseData(responseUri);
if (responseData == null) {
Logger.error("Failed to extract OAuth2 response from redirect");
return;
}
responseData.setData(responseUri);
if (mCompleteIntent != null) {
Logger.debug("Authorization complete - invoking completion intent");
try {
mCompleteIntent.send(this, 0, responseData);
} catch (CanceledException ex) {
Logger.error("Failed to send completion intent", ex);
}
} else {
setResult(RESULT_OK, responseData);
}
}
In case anybody else stumbles upon this issue.
Use the example app within app-auth android github project.
Don't use Google CodeLabs app-auth example! The code from the question above is from Google CodeLabs, it is very old and no longer works (state at July 2020).
I did the same mistake, app-auth links codelabs on their own page/readme, so I started using codelabs code and ended up with lots of problems and errors.
The new app-auth version 0.7.x uses a json configuration file and the example app shows how to handle errors around pending intents etc. .
So I'm working on an app and I had this part working for days, and out of no where it just stopped working for no reason...
I also had the same error when I was trying to use another headless fragment in my MainActivity, but ended up replacing the fragment with inner methods inside of the MainActivity and everything went back to working properly.
However, I can't rewrite every bit of code I have just to avoid using fragments. The fragment code is below.
public class IMEIFragment extends Fragment implements ActivityCompat.OnRequestPermissionsResultCallback{
public static final String TAG_IMEI = "IMEILoader";
protected Activity mActivity;
private String RecordedIMEI;
//public static final String CHECK_INTERNET = "network_connection";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return null; //Do we need this at all?
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
Activity activity = context instanceof Activity ? (Activity) context : null;
mActivity = activity;
}
//Is this needed?
#SuppressWarnings("deprecation")
#Override
public void onAttach(Activity activity) {
activity = getActivity();
if (isAdded() && activity != null) {
super.onAttach(activity);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
mActivity = activity;
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onDetach() {
super.onDetach();
mActivity = null;
}
public String loadIMEI(Context context) {
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// READ_PHONE_STATE permission has not been granted.
requestPermissions(context);
} else {
// READ_PHONE_STATE permission is already been granted.
RecordedIMEI = permissionGrantedActions(context);
}
if (RecordedIMEI != null) {
Log.i("loadIMEIService", "IMEI number returned!");
}
} else {
// READ_PHONE_STATE permission is already been granted.
RecordedIMEI = permissionGrantedActions(context);
}
if (RecordedIMEI != null) {
Log.i("loadIMEIService", "IMEI number returned!");
}
return RecordedIMEI;
}
private void requestPermissions(Context context) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
Log.i("loadIMEIService", "READ_PHONE_STATE permission not granted, asking for it...");
// TODO create proper notification content
PermissionHelper.requestPermissions(((PriceActivity) getActivity()),
new String[]{Manifest.permission.READ_PHONE_STATE},
Constants.PERM_REQUEST_PHONE_STATE,
getString(R.string.notify_perm_title),
getString(R.string.notify_perm_body),
R.drawable.ic_security);
}
}
// Callback received when a permissions request has been completed.
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
boolean isGranted = false;
for (int i = 0; i < grantResults.length; i++)
if (permissions[i].equals(Manifest.permission.READ_PHONE_STATE) && (grantResults[i] == PackageManager.PERMISSION_GRANTED))
isGranted = true;
if (isGranted) {
Context context = getActivity().getApplicationContext();
permissionGrantedActions(context);
}
else
Log.w("loadIMEIService", "READ_PHONE_STATE permission not granted. loadIMEI will not be available.");
}
public String permissionGrantedActions(Context context) {
//Have an object of TelephonyManager
TelephonyManager tm =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
//Get IMEI Number of Phone
String IMEINumber = tm.getDeviceId();
if(IMEINumber != null) {
Log.i("loadIMEIService", "IMEI number recorded!");
}
return IMEINumber;
}
}
Error is below:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.project1, PID: 5498
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.project1/com.android.project1.main.MainActivity}: java.lang.IllegalStateException: Fragment IMEIFragment{3e80da7 IMEILoader} not attached to Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Fragment IMEIFragment{3e80da7 IMEILoader} not attached to Activity
at android.app.Fragment.getResources(Fragment.java:805)
at android.app.Fragment.getString(Fragment.java:827)
at com.android.project1.fragments.IMEIFragment.requestPermissions(IMEIFragment.java:107)
at com.android.project1.fragments.IMEIFragment.loadIMEI(IMEIFragment.java:80)
at com.android.project1.main.MainActivity.onCreate(MainActivity.java:108)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
And here's the relevant part of my MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDeviceCode = (TextView) findViewById(R.id.device_code);
// Initializing headless fragment
mFragment =
(IMEIFragment) getFragmentManager()
.findFragmentByTag("IMEILoader");
if (mFragment == null) {
mFragment = new IMEIFragment();
getFragmentManager().beginTransaction()
.add(mFragment, "IMEILoader").commit();
}
if (mFragment != null) {
mNumber = mFragment.loadIMEI(MainActivity.this);
mDeviceCode.setText(Html.fromHtml("<b>IMEI</b>: " + mNumber));
}
I literally had the exact same code working for over a week. Anyone knows what could be the problem?
Edit 1: The error is pointing to requestPermissions inside my fragment
Fragments should be self contained as much as possible. You are calling directly into your IMEIFragment from the activity,
Caused by: java.lang.IllegalStateException: Fragment IMEIFragment{3e80da7 IMEILoader} not attached to Activity
at android.app.Fragment.getResources(Fragment.java:805)
at android.app.Fragment.getString(Fragment.java:827)
at com.android.project1.fragments.IMEIFragment.requestPermissions(IMEIFragment.java:107)
at com.android.project1.fragments.IMEIFragment.loadIMEI(IMEIFragment.java:80)
at com.android.project1.main.MainActivity.onCreate(MainActivity.java:108)
You can't do that. Adding the fragment via a transaction from the activity is an asynchronous operation. E.g., when the commit() method completes, the fragment is not initialized. Moreover, you have no way of knowing when it's initialized. That's why it should be self contained. The fragment decides when to call loadIMEI(), not the activity.
If you really need it to be initiated by the activity, you can add a callback from the fragment to the activity like,
void onFragmentReady(Fragment f);
Or something.
And yes, onCreateView() should return something. If your fragment really doesn't have any UI at all, you don't need it to be a fragment.
I am making an app that has 2 java classes: MainActivity and SecondActivity. MainActivity initiates the SecondActivity by using an onClick method.
The SecondActivity has 2 arrays stored in the strings.xml file. The SecondActivity class wants to store all the array variables from strings.xml to itself, in order to use it for some methods. I think i am doing it the wrong way, as i tried storing the arrays directly in the SecondActivity and it worked but when i am storing the arrays in strings.xml it gives a NullPointerException.
Here is the SecondActivity code:
package com.android.ict.seneca.androidpocketguide;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SecondActivity extends Activity {
private final String CLASS_NAME = "SecondActivity";
String [] Vocabularies = getResources().getStringArray(R.array.Vocabularies_array);
String [] meanings = getResources().getStringArray(R.array.meanings_array);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_dropdown_item_1line, Vocabularies);
//ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, meanings);
AutoCompleteTextView textView =
(AutoCompleteTextView) findViewById(R.id.vocabularies);
textView.setThreshold(2); // minimum number of characters to be typed
textView.setAdapter(adapter); // connect the list of suggestions to the View object
}
public void onStart(){
super.onStart();
Log.d( CLASS_NAME, "onStart invoked!" );
}
public void onPause(){
super.onPause();
Log.d( CLASS_NAME, "onPause invoked!!" );
}
public void onResume(){
super.onResume();
Log.d( CLASS_NAME, "onResume invoked!!" );
}
public void onStop(){
super.onStop();
Log.d( CLASS_NAME, "onStop invoked!!!" );
}
public void onDestroy(){
super.onDestroy();
Log.d( CLASS_NAME, "onDestroy INVOKED!!!" );
}
public void onRestart(){
super.onRestart();
Log.d( CLASS_NAME, "onRestart invoked!!" );
}
// event handler for the button
public void onClick(View view) {
Intent data = new Intent();
EditText usr =
(EditText) findViewById(R.id.vocabularies); // get user input
// set the data part (an URI reference) of an intent
data.setData(Uri.parse(usr.getText().toString()));
String s = data.getData().toString();
int flag = 0;
for(int i=0; i<Vocabularies.length;i++)
{
if(s.equals(Vocabularies[i]))
{
flag = i;
}
}
Toast.makeText(this, "YOU'VE ENTERED: " + s, Toast.LENGTH_LONG ).show();
String m = meanings[flag];
String disp = "Vocabulary: " + s + "\n" + "Meaning: " + m;
TextView textViewObj1 = (TextView) findViewById(R.id.meaning);
textViewObj1.setText(disp);
//finish(); //--- close the activity AND RETURN CONTROL TO THE REQUESTING ACTIVITY
//EditText txt_username = (EditText) findViewById(R.id.txt_username);
} // end onClick
}
Here is the MainActivity code:
package com.android.ict.seneca.androidpocketguide;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private final String CLASS_NAME = "FirstActiviy";
private int request_Code = 1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d( CLASS_NAME, "onCreate invoked!" );
}
public void onStart(){
super.onStart();
Log.d( CLASS_NAME, "onStart invoked!" );
}
public void onPause(){
super.onPause();
Log.d( CLASS_NAME, "onPause invoked!!" );
}
public void onResume(){
super.onResume();
Log.d( CLASS_NAME, "onResume invoked!!" );
}
public void onStop(){
super.onStop();
Log.d( CLASS_NAME, "onStop invoked!!!" );
}
public void onDestroy(){
super.onDestroy();
Log.d( CLASS_NAME, "onDestroy INVOKED!!!" );
}
public void onRestart(){
super.onRestart();
Log.d( CLASS_NAME, "onRestart invoked!!" );
}
// event handler for the button (activity 2)
public void onClick(View view) {
startActivityForResult( new Intent(
"com.android.ict.seneca.androidpocketguide.second"), // declared by an intent filter
request_Code); // (requesting) activity ID
Log.d( CLASS_NAME, "after startActivityForResult" );
//intent.setData(Uri.parse("com.seneca.lab2b.abbas"));
//startActivity(intent);
}
/*
public void onActivityResult( int requestCode, // used to match the REQUESTING activity ID
int resultCode, // result code set by the sender (i.e. the TARGET activity)
Intent data ) // an intent passed by the sender (i.e. the TARGET activity)
{
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
//TextToSpeech(data.getData().toString());
Intent intent = getIntent();
String s = data.getData().toString();
TextView textViewObj1 = (TextView) findViewById(R.id.line);
textViewObj1.setText(s);
Toast.makeText(
this,
"MESSAGE RECEIVED: " + s, // retrieve data from the intent
// API Question: What is the return type of getData()?
Toast.LENGTH_LONG ).show();
}
}
Log.d( CLASS_NAME, "onActivityResult: what is next?" );
}
// event handler for a button (activity 3)
public void onClick3(View view) {
Intent dataIntent = new Intent( this, ThirdActivity.class );//"com.seneca.lab2b.third"); // explicit intent
// the putExtra( ) method
dataIntent.putExtra( "greeting", "Good morning Android!" );
dataIntent.putExtra( "number", 123 );
// create a Bundle object
Bundle bundle = new Bundle();
bundle.putString( "My name", "Abbas Zoeb" );
bundle.putInt( "Student ID", 56789 );
// putExtras( ): store the bundle in the intent
dataIntent.putExtras( bundle );
// launch the third activity
startActivity( dataIntent );
//startActivity( new Intent( this, ThirdActivity.class ) );
}
*/
}
Here is the error log:
09-21 23:43:19.907 26183-26183/com.android.ict.seneca.androidpocketguide E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.ict.seneca.androidpocketguide, PID: 26183
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.ict.seneca.androidpocketguide/com.android.ict.seneca.androidpocketguide.SecondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:87)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
at com.android.ict.seneca.androidpocketguide.SecondActivity.(SecondActivity.java:22)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Here is the string.xml file:
<resources>
<string name="app_name">Android Pocket Guide</string>
<string-array name="Vocabularies_array">
<item>activity</item>
<item>AVD</item>
<item>ART</item>
<item>Dalvik</item>
<item>intent</item>
<item>intent filter</item>
<item>explicit intent</item>
<item>implicit intent</item>
<item>LogCat</item>
<item>bundle</item>
<item>Gradle</item>
<item>Android Device Monitor</item>
<item>SDK manager</item>
<item>minSdkVersion</item>
</string-array>
<string-array name="meanings_array">
<item>An activity represents a single screen with a user interface just like window or frame of Java</item>
<item>An Android Virtual Device (AVD) is an emulator configuration that lets you model an actual device by defining hardware and software options to be emulated by the Android Emulator.</item>
<item>Android Runtime (ART) is an application runtime environment used by the Android operating system.</item>
<item>Dalvik is a part of the software stack that makes up the Android platform.</item>
<item>Intent can be defined as a simple message objects which is used to communicate from 1 activity to another.</item>
<item>Specifies the types of intents that an activity, service, or broadcast receiver can respond to</item>
<item>An explicit intent is one that you use to launch a specific app component, such as a particular activity or service in your app</item>
<item>Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it.</item>
<item>Logcat is a command-line tool that dumps a log of system messages, including stack traces when the device throws an error and messages that you have written from your app with the Log class.</item>
<item>A Bundle is a set of key/value pairs, where it implements an interface called Parcelable.</item>
<item>Gradle is a custom build tool used to build android packages (apk files) by managing dependencies and providing custom build logic.</item>
<item>Android Device Monitor is a standalone tool that provides a UI for several Android application debugging and analysis tools.</item>
<item>A software development kit that enables developers to create applications for the Android platform.</item>
<item>An integer designating the minimum API Level required for the application to run.</item>
</string-array>
</resources>
Try this way
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Vocabularies = getResources().getStringArray(R.array.Vocabularies_array);
meanings = getResources().getStringArray(R.array.meanings_array);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_dropdown_item_1line, Vocabularies);
//ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, meanings);
AutoCompleteTextView textView =
(AutoCompleteTextView) findViewById(R.id.vocabularies);
textView.setThreshold(2); // minimum number of characters to be typed
textView.setAdapter(adapter); // connect the list of suggestions to the View object
}
Try declaring your arrays in the global context, then assigning them some value during one of the activity's lifecycle stages (i.e. onCreate(), onResume(), etc.).
The issue you're having is with getResources(). I would create a global variable of type Context. Then, what I would do in onCreate() is this:
context = this;
vocabularies = context.getResources().getStringArray(R.array.Vocabularies_array);
meanings = context.getResources().getStringArray(R.array.meanings_array);
Hope that helps!
These variable initializations...
String [] Vocabularies = getResources().getStringArray(R.array.Vocabularies_array);
String [] meanings = getResources().getStringArray(R.array.meanings_array);
...probably need to go in a constructor, or even more likely in onCreate(...), specifically after
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Initialization of the variables in some method like oncreate/onPostcreated e.g.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Vocabularies = getResources().getStringArray(R.array.Vocabularies_array);
meanings = getResources().getStringArray(R.array.meanings_array);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_dropdown_item_1line, Vocabularies);
//ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, meanings);
AutoCompleteTextView textView =
(AutoCompleteTextView) findViewById(R.id.vocabularies);
textView.setThreshold(2); // minimum number of characters to be typed
textView.setAdapter(adapter); // connect the list of suggestions to the View object
}
Hi first of all i searched some similar questions like mine but unfortunately i couldn't find the similarity of my code to them so please here me out
I have a Main Activity Class
public class MainActivity extends AppCompatActivity {
public ProgressDialog loading;
public String[] itemer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RetrofitHandler handler = new RetrofitHandler();
handler.getContacts(MainActivity.this);
}
public void getter(String[] response, int size) {
String[] itemer;
itemer = new String[size];
if (response != null) {
itemer = response;
Toast.makeText(MainActivity.this, itemer[0], Toast.LENGTH_SHORT).show();
}
}
And a Handler for my result
public class RetrofitHandler {
public String[] item;
public static final String ROOT_URL = "http://api.androidhive.info";
public List<Contacts> contacts;
// final MainActivity main = new MainActivity();
public void getContacts(final Context context) {
final ProgressDialog loading = ProgressDialog.show(context, "Fetching Data", "Please wait...", false, false);
RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ROOT_URL).build();
ContactsAPI api = adapter.create(ContactsAPI.class);
api.getContacts(new Callback<Contacts>() {
#Override
public void success(Contacts contacts, Response response) {
loading.dismiss();
MainActivity update = new MainActivity();
List<Contact> contactList = contacts.getContacts();
item = new String[contactList.size()];
int size = contactList.size();
for (int i = 0; i < contactList.size(); i++) {
item[i] = contactList.get(i).getName();
}
update.getter(item, size);
}
#Override
public void failure(RetrofitError error) {
Toast.makeText(context, "Error Occured", Toast.LENGTH_LONG).show();
}
});
}
But I get an error on my response in the main activity here is my log
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:87)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
at android.widget.Toast.<init>(Toast.java:102)
at android.widget.Toast.makeText(Toast.java:259)
at com.exist.kelvs.retrofit2.MainActivity.getter(MainActivity.java:55)
at com.exist.kelvs.retrofit2.RetrofitHandler$1.success(RetrofitHandler.java:41)
at com.exist.kelvs.retrofit2.RetrofitHandler$1.success(RetrofitHandler.java:28)
at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I maked sure that the handler item where not null tested it with toast but when i pass it to getter it gives me the error where did i do wrong? :(
MainActivity update = new MainActivity();
Never instantiate activities with new. They are not initialized to be useful.
Instead, you can pass your activity as a reference where needed. Change
public void getContacts(final Context context)
to e.g.
public void getContacts(final MainActivity mainActivity)
and use mainActivity where you need an activity Context (such as with Dialogs) and when you need to invoke a method on MainActivity.
Note that generally passing activity references to async operations can be prone to significant memory leaks, and you need to take activity lifecycle into account - the activity might not be active when the async operation finishes.
try to delete the toast in your main activity or replace Mainactivity.this to getApplicationContext() .
from :
Toast.makeText(MainActivity.this, itemer[0], Toast.LENGTH_SHORT).show();
to :
Toast.makeText(getApplicationContext(), itemer[0], Toast.LENGTH_SHORT).show();
I'm trying to add a Splash Screen to my Android Rss Reader application, so that i can check network connectivity and determine which screen the user sees first, depending on connectivity.
Without the SplashScreen, the MainActivity.java works fine, but when I set SplashScreenActivity as the launcher within the Manifest, the Asynctask within MainActivity throws a null pointer when it runs. Does anybody know what the reason for this might be?
Thanks for your time!
SplashScreenActivity.java
enter code here
public class SplashScreenActivity extends MainActivity {
boolean alreadyExecuted = false;
private static String TAG = SplashScreenActivity.class.getName();
private static long SLEEP_TIME = 5;
#Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes notification bar
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
// Start timer and launch main activity
IntentLauncher launcher = new IntentLauncher();
launcher.start();
}
private class IntentLauncher extends Thread {
#Override
/**
* Sleep for some time and than start new activity.
*/
public void run() {
try {
// Sleeping
Thread.sleep(SLEEP_TIME*2000);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
//If this method is called once, set boolean alreadyExecuted to true
if(!alreadyExecuted)
{
Check();
alreadyExecuted=true;
}
}
}
//Check if network is available. If so go to MainActivity screen, which requires internet
//If not, go to MyFeeds screen
public void Check()
{
if (NetworkCheck.getInstance(this).isOnline()) {
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
SplashScreenActivity.this.startActivity(intent);
SplashScreenActivity.this.finish();
}
else
{
Intent intent = new Intent(SplashScreenActivity.this, MyFeedsScreen.class);
SplashScreenActivity.this.startActivity(intent);
SplashScreenActivity.this.finish();
}
}}
MainActivity.java
public class MainActivity extends Activity {
private MainActivity local;
private DatabaseHandler db;
//Method to create main application view
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set view
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
//**************************create a button to move to the user's saved feeds screen*****************************
Button myFeedsButton = (Button) findViewById(R.id.myFeedsButton);
myFeedsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
startActivity(new Intent(MainActivity.this, MyFeedsScreen.class));
}
});
//*****************************************************************************************************************
//Create new instance of database handler
db = new DatabaseHandler(this);
//set local ref to this activity
local = this;
//Calls the doInBackground method
GetRSSDataTask task = new GetRSSDataTask();
//start download Rss task - execute method calls the
task.execute("http://pitchfork.com/rss/reviews/best/albums/");
//debug thread name
Log.d("RssReaderApp", Thread.currentThread().getName());
}
//*******************************************************************************************************************
private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem>>
{
#Override
protected List<RssItem> doInBackground(String... urls) {
//debug task thread name
Log.d("RssReaderApp", Thread.currentThread().getName());
try {
//create a new reader
RssReader rssReader = new RssReader(urls[0]);
//Parse RSS, get items
return rssReader.getItems();
} catch (Exception e) {
Log.e("RssReaderApp", e.getMessage());
}
return null;
}//doInBackground
//is invoked on UI thread after background tasks are complete.
// Results of background task are passed here as a parameter
#Override
protected void onPostExecute(List<RssItem> result)
{
//Gets listview from main.xml
final ListView listItems = (ListView) findViewById(R.id.listMainView);
//Creates a new list adapter - displays an array of strings in listview
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(local, android.R.layout.simple_list_item_1, result);
//Set list adapter for listView
listItems.setAdapter(adapter);
//OnItemClick listener set to allow user to access content from title
listItems.setOnItemClickListener(new ListListener(result, local));
//*******************************LONG CLICK FUNCTIONALITY******************************************
//Set new long click listener which should allow item to be stored to db
listItems.setLongClickable(true);
listItems.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
try {
db.open();
RssItem fromList = (RssItem) listItems.getItemAtPosition(position);
RssItem item = new RssItem();
item.title = fromList.title;
item._id = fromList._id;
//item.completeTextLink = fromList.completeTextLink;
//item.mainBody = fromList.mainBody;
item.link = fromList.link;
item.description = fromList.description;
// item.page = fromList.page;
db.insertRssItem(item);
db.close();
} catch (SQLException e) {
e.printStackTrace();
}
Toast.makeText(getBaseContext(), "Item saved in My Feeds!", Toast.LENGTH_SHORT).show();
return true;
}
});
}//onPostExecute
}//getRssTaskClass}//class
Android Manifest, with SplashScreenActvity, rather than MainActivity now set as launcher:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:label="#string/app_name">
<activity android:name="com.example.rory.RssReaderApp.MainActivity">
</activity>
<activity android:name="com.example.rory.RssReaderApp.MyFeedsScreen"></activity>
<activity android:name=".DetailsActivity"></activity>
<activity android:name="com.example.rory.RssReaderApp.SplashScreenActivity">
android:icon="#drawable/newspaper"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
LogCat error:
08-19 02:23:52.467 15372-15372/com.example.rory.readerapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.rory.readerapp, PID: 15372
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.rory.RssReaderApp.MainActivity$GetRSSDataTask.onPostExecute(MainActivity.java:113)
at com.example.rory.RssReaderApp.MainActivity$GetRSSDataTask.onPostExecute(MainActivity.java:79)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Try referencing your MainActivity in your onPostExecute when setting your listItems.
final ListView listItems = (ListView) local.findViewById(R.id.listMainView);
Solved the problem! I had accidentally made the SplashScreenActivity.java class extend MainActivity rather than Activity.
Thanks for the help guys!