Illegal access exception while sharing Global data - android

i want to share the global data across activities and i have followed
this link
.but i am not getting how to declare it in my manifest. i am posting my manifest code, i have tried it in different ways, but still getting the error. please tell me how to resolve it.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:name=".Myapp">
<activity android:name=".AndroidtestActivity"
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>
This is my main activity
public class AndroidtestActivity extends Activity
{
/** Called when the activity is first created. */
public static final String PREFS_NAME = "MyPrefsFile";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText et = (EditText) findViewById(R.id.text1);
Myapp appState = ((Myapp)getApplicationContext());
String s= appState.getState();
et.setText(s);
}
}
and my Myapp class is
class Myapp extends Application {
private String myState;
public String getState(){
return myState;
}
public void setState(String s){
myState = "hello world";
}
}
i am getting error in the line
Myapp appState = ((Myapp)getApplicationContext());
illegal access exception ,please tell me how to resolve this problem
please help me with this.

Merge the application tags (i.e. put the android:name in the first one and delete the rest):
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:name=".MyApp">
<activity android:name=".AndroidtestActivity"
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>
Then use getApplication() instead of getApplicationContext().

Related

Cannot find getApplicationContext() in class extended from FirebaseMessagingService

I want to send and receive remote notification through Firebase but my custom class could not find getApplicationContext() method.
From my understanding, FirebaseMessagingService extends Service, which extendsContextWrapper` which extends Context.
So I don't know getApplicationContext() is not working. Any help will be appreciated!
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FirebaseMessageService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(remoteMessage.getNotification() != null){
String title = remoteMessage.getNotification().getTitle();
String text = remoteMessage.getNotification().getBody();
NotificationHelper.displayNotification(getApplicationContext(), title, text);
}
super.onMessageReceived(remoteMessage);
}
}
Here's my AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mealz">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme.NoActionBar">
<activity android:name=".Activities.UserActivity"></activity>
<activity android:name=".Activities.LoginActivity" />
<activity android:name=".Activities.SignupActivity" />
<activity android:name=".Activities.SearchRecipeActivity" />
<activity android:name=".Activities.RecipeDetailActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".Notifications.FirebaseMessageService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>
You can achieve this in many way one way is to keep a weak refrence of context make a app class extending android.app.application and on it's create method and make a variable of weak refrence of context and assign it to getApplicationContext() and you may use this context anywhere you want.
public class app extends android.app.Application{
private static WeakReference<Context> referenceCntx;
#Override
public void onCreate() {
super.onCreate();
referenceCntx = new WeakReference<>(getApplicationContext());
}
public static Context getApplicationCntx(){
return referenceCntx.get();
}
}
And don't forget to add this line in manifest
<application
android:name=".app"

Android - Launcher does not have the permission to launch Intent

First off, I know this has been asked before and I have gone through many many posts regarding this. Unfortunately, my case seems to be a bit different. Most of the posts advised to remove the permission statement from application tag. This, unfortunately, does not help my situation, as I do not have permissions inside the application tag. I would very much appreciate any assistance.
Here is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jonasSoftware.blueharvest"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.jonasSoftware.blueharvest.LoginActivity"
android:label="#string/app_name"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.jonasSoftware.blueharvest.ChargeActivity"
android:label="#string/title_activity_charge" >
</activity>
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.jonasSoftware.blueharvest.HomeActivity"
android:label="Home" >
</activity>
<activity
android:name="com.jonasSoftware.blueharvest.ConfigActivity"
android:label="Config" >
</activity>
<activity
android:name="com.jonasSoftware.blueharvest.SettingsActivity"
android:label="Settings" >
</activity>
<activity android:name=".WebServiceDemo" android:label="WebServiceDemo"/>
<activity android:name=".UploadActivity"/>
<activity android:name=".TransferActivity"/>
<activity android:name=".ReceivePO"/>
<activity android:name=".LoginActivity"/>
</application>
</manifest>
The LoginActivity, if needed, is below. For testing purposes, it just starts the HomeActivity intent, with the majority of the code being commented out.
public class LoginActivity extends Activity {
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_USERNAME = "username";
private static String KEY_CREATED_AT = "created_at";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
setTitle("onas Scanner Login");
final Button loginBtn = (Button) findViewById(R.id.btnLogin);
final EditText loginUsername = (EditText) findViewById(R.id.loginUsername);
final EditText loginPassword = (EditText) findViewById(R.id.loginPassword);
final TextView loginErrorMsg = (TextView) findViewById(R.id.login_error);
loginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do stuff
String username = loginUsername.getText().toString();
String password = loginPassword.getText().toString();
Intent homeActivity = new Intent(getApplicationContext(), HomeActivity.class);
homeActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeActivity);
}
});
}
}
If any other information is required, please let me know.
So as I was about to post this, I realized what my mistake was. Looking through the Manifest again I noticed something interesting. The application did not originally have the LoginActivity. When I added the LoginActivity, the following line was added to the Manifest.
<activity android:name=".LoginActivity"/>
I did not know this at the time and so just edited the original launcher activity, as seen below.
<activity
android:name="com.jonasSoftware.blueharvest.LoginActivity"
android:label="#string/app_name"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
the android:name= line had originally been "com.jonasSoftware.blueharvest.HomeActivity". So when I removed the extra line from the Manifest, everything started working again.
I hope this helps someone.
Brad.

registerMediaButtonEventReceiver / handling volume buttons issue

In my application I need to process physical button events such as volume button clicks. For that I'm using audiomanager's registerMediaButtonEventReceiver method. There's an article relevant to my situation, though I can't get it working.
Here's code I'm using:
public class VolumeBroadcastActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
manager.registerMediaButtonEventReceiver(new ComponentName(getPackageName(), RemoteControlReceiver.class.getName()));
}
public class RemoteControlReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(VolumeBroadcastActivity.this, "1",1).show();
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="localhost.volume.broadcast"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".VolumeBroadcastActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="RemoteControlReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
</application>
</manifest>
Your RemoteControlReceiver class is an inner class of VolumeBroadcastActivity. Your manifest doesn't say so and I don't think it could. Make it a regular class and precede its android:name with a dot.
You might need this permission (declare in manifest):
uses-permission android:name="android.permission.BLUETOOTH"

Embed widget in android activity - not responding

I'm trying to make a home screen. I've got the widget to display but I need to send some kind of notify to it so it will start. Perhaps I'm missing something in the AndroidManifest.xml?
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Fredrik" android:versionCode="1" android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".HomeScreen" android:label="#string/app_name"
android:launchMode="singleInstance" android:stateNotNeeded="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
HomeScreen.java:
public class HomeScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final AppWidgetManager widgets = AppWidgetManager
.getInstance(getApplicationContext());
List<AppWidgetProviderInfo> installedProviders = widgets
.getInstalledProviders();
for (AppWidgetProviderInfo ws : installedProviders) {
if (ws.label.startsWith("Music (Large)")) {
AppWidgetHost h = new AppWidgetHost(getApplicationContext(), 10);
int id = h.allocateAppWidgetId();
AppWidgetHostView v= h.createView(this, id, ws);
setContentView(v);
h.startListening();
break;
}
}
}
}
Does anyone have a clue?
According to this post, you cannot program widget insertion yourself: http://groups.google.com/group/android-developers/browse_thread/thread/e4a5b4a87afcf707?pli=1
You have to call an intend that will give the user the ability to pick.
I was still surprised to hear you managed to get it to display...

onCreate() or onNewIntent() not fired when search button pressed

I'm trying to implement search functionality in my app, which is very basic at the moment. When I press the search button on my Nexus, the search intent seems to not fire because neither onCreate() nor onNewIntent() gets called. I have basically copied the whole example you can find on Android developers, but it is still not working. Thanks for your help
res/searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="hello"
android:hint="Search lectures" >
</searchable>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.openday.lectures"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".LecturesListActivity"
android:label="#string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<activity android:name=".SingleLectureActivity"
android:label="Lecture">
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
LecturesListActivity.java (shortened)
public class LecturesListActivity extends ListActivity {
private String categoryDisplayed;
private String facultyDisplayed;
private List<Lecture> lectures;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadLectures();
displayFaculties();
}
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
searchForLectures(query);
}
}
}
Problem with constant string in searchable.xml, same answer as this question

Categories

Resources