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.
Related
AndroidManifest.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bidnextjob.bidnextjob">
<!-- Lat lon fetching permission by GPSTracker class start and also use for google map -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.bidnextjob.bidnextjob.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Allows access to the flashlight -->
<permission
android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/MyTheme">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<activity android:name=".LoginActivity" />
<activity android:name=".RegisterActivity" />
<activity
android:name=".HomeActivity"
android:windowSoftInputMode="stateAlwaysHidden" />
<activity android:name=".FilterActivity" />
<activity android:name=".JobDetailsActivity" />
<activity android:name=".EditProfileActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>
<activity
android:name=".ChatActivity"
android:windowSoftInputMode="stateAlwaysHidden" />
<activity android:name=".AboutUsActivity" />
<activity android:name=".HelpActivity" />
<activity android:name=".ReviewActivity" />
<activity
android:name=".CommentActivity"
android:windowSoftInputMode="stateAlwaysHidden" />
<activity android:name=".ForgetPasswordActivity" />
<activity
android:name=".PlaceBidActivity"
android:windowSoftInputMode="stateAlwaysHidden" />
<activity
android:name=".EditBidActivity"
android:windowSoftInputMode="stateAlwaysHidden" />
<activity android:name=".JobDetails1Activity" />
<activity android:name=".NotificationsActivity" />
<activity android:name=".ChooseCategoryActivity" />
<activity android:name=".BidDetailsActivity" />
<activity android:name=".EditAddressActivity" />
<activity android:name=".UpdateProfileActivity" />
<activity android:name=".GiveRatingActivity" />
<activity android:name=".ArchiveActivity"/>
<activity android:name=".AutoCompleteAddress"/>
<activity
android:name=".SocialLoginActivity"
android:label=".SocialLoginActivity" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="<some api key>" />
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
<service android:name="utilities.gcm.GcmIntentService" />
<receiver
android:name="utilities.gcm.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.bidnextjob.bidnextjob" />
</intent-filter>
</receiver>
</application>
</manifest>
I'm accessing a Pojo class having static method for returning value. When I access this static method in my Splash Screen class(Launcher page), I get a NoClassDefFoundError for that Pojo class. How to avoid this exception without making the method non-static?
LogCat
FATAL EXCEPTION: main
Process: com.bidnextjob.bidnextjob, PID: 30515
java.lang.NoClassDefFoundError: shared_pref.SharedStorage
at com.bidnextjob.bidnextjob.SplashActivity.onCreate(SplashActivity.java:21)
at android.app.Activity.performCreate(Activity.java:5280)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2322)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5388)
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:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:655)
at dalvik.system.NativeStart.main(Native Method)
Pojo class
package shared_pref;
public class SharedStorage {
static SharedPreferences preference;
private static String prefData="ExampleStructApp";
public static String UserId= "UserId";
public static String UserType= "UserType";
public static void setValue(Context context,String key,String data){
preference = context.getSharedPreferences(prefData, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preference.edit();
editor.putString(key,data);
editor.commit();
}
public static String getValue(Context context,String key){
preference = context.getSharedPreferences(prefData, Context.MODE_PRIVATE);
String id = preference.getString(key,"");
return id;
}
public static void resetValue(Context context){
preference = context.getSharedPreferences(prefData, Context.MODE_PRIVATE);
preference.edit().clear().commit();
}
}
Splash Screen class
public class SplashActivity extends AppCompatActivity {
private String user_id = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// Here I get NoClassDefFoundError
user_id = SharedStorage.getValue(getApplicationContext(),"UserId");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if(user_id != null && !user_id.isEmpty()){
startActivity(new Intent(SplashActivity.this, HomeActivity.class));
finish();
}else{
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
}
}, 3000);
}
}
This is caused when there is a class file that your code depends on and it is present at compile time but not found at runtime. Look for differences in your build time and runtime classpaths.
I'm having this issue where the sms permission won't work when sending a text message while having the following piece of code in android manifest.
<activity android:name=".engine.MainActivity" android:label="#string/app_name" android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
If I were to replace this code with this:
<activity
android:name=".SendSmsActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Sms starts working. I also tried adding the second code like this under the code that's conflicting:
<activity android:name=".ui.SendSmsActivity">
</activity>
But that doesn't work either. Any help would be greatly appreciated. I've been stuck on this for a few days now.
Android Manifeset
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android_auto">
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application android:allowBackup="true" android:icon="#mipmap/ic_launcher" android:label="#string/app_name"
android:supportsRtl="true" android:theme="#style/AppTheme">
<activity android:name=".engine.MainActivity" android:label="#string/app_name" android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ui.SendSmsActivity">
</activity>
<activity android:name=".ui.MapsActivity1">
</activity>
<meta-data android:name="com.google.android.gms.car.application" android:resource="#xml/automotive_app_desc" />
<meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSyCMFYfJ6aOuxJk3W0vmhF6Nou3TP_qIU6c" />
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<!--
Main music service, provides media browsing and media playback services to
consumers through MediaBrowserService and MediaSession. Consumers connect to it through
MediaBrowser (for browsing) and MediaController (for playback control)
-->
<service android:name=".MyMusicService" android:exported="true">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
<service android:name=".MyMessagingService" />
<receiver android:name=".MessageReadReceiver">
<intent-filter>
<action android:name="com.example.zachboone.myapplication.ACTION_MESSAGE_READ" />
</intent-filter>
</receiver>
<receiver android:name=".MessageReplyReceiver">
<intent-filter>
<action android:name="com.example.zachboone.myapplication.ACTION_MESSAGE_REPLY" />
</intent-filter>
</receiver>
</application>
</manifest>
SendSmsActivity.java
package aaa_android_auto.ui;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android_auto.R;
public class SendSmsActivity extends Activity {
Button buttonSend;
EditText textPhoneNo;
EditText textSMS;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
buttonSend = (Button) findViewById(R.id.buttonSend);
textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
textSMS = (EditText) findViewById(R.id.editTextSMS);
buttonSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String phoneNo = textPhoneNo.getText().toString();
String sms = textSMS.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exampl.fitindya"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="20" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Entry for RegisterActivity.class -->
<activity android:name=".RegisterActivity"
android:label="Register New Account"></activity>
</application>
Don't know why this error is appearing and application crashes. please help guys.can there be any problem anywhere else
here is my class file
public class MainActivity extends ActionBarActivity {
Button login_b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
// Listening to register new account link
registerScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(i);
}
});
}
}
Place this code below the register Activity. I guess that should be the error
<intent-filter>
<action android:name="android.intent.action.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
I'm currently studying a trivia style app and I'm using it in a Fragment. If I use Activities instead of Fragments, the code works:
((ChuckApplication)getApplication()).setCurrentGame(c);
But once I cast it for an activity it keeps on getting an error. The code is:
((ChuckApplication)getActivity().getApplication()).setCurrentGame(c);
The whole code is for this fragment is:
public class Activity_Home_Language extends Fragment implements OnClickListener{
Intent intent;
ImageButton btnToggle;
Button btnExam,btnReview;
TextView txtTitle;
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.layout_home_language, container, false);
btnToggle = (ImageButton) rootView.findViewById(R.id.btnToggle);
btnExam = (Button) rootView.findViewById(R.id.btnExam);
btnReview = (Button) rootView.findViewById(R.id.btnReview);
txtTitle = (TextView) rootView.findViewById(R.id.txtTitle);
btnToggle.setOnClickListener(this);
btnExam.setOnClickListener(this);
btnReview.setOnClickListener(this);
//FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
//transaction.add(R.id.frameContent, new Activity_Home());
//transaction.add(R.id.framePager2, new ViewPagerMunicipalities());
//transaction.add(R.id.frameDestPager, new ViewPagerDes());
//transaction.commit();
//for fading animation
return rootView;
}
#Override
public void onClick(View v) {
if(v==btnToggle){
Activity_Main.mSlideHolder.open();
}
else if(v==btnExam){
FragmentTransaction transaction = getFragmentManager().beginTransaction();
//transaction.addToBackStack(null);
transaction.replace(R.id.frameContent, new Activity_Question_Exam_Home()).commit();
}
else if(v==btnReview){
//enable this to move to move to a Activity or fragment activity
//intent = new Intent(rootView.getContext(), Activity_About.class);
//startActivityForResult(intent,0);
//Get Question set //
List<Question> questions = getQuestionSetFromDb();
//Initialise Game with retrieved question set ///
GamePlay c = new GamePlay();
c.setQuestions(questions);
c.setNumRounds(getNumQuestions());
((ChuckApplication) getActivity().getApplication()).setCurrentGame(c);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frameContent, new Activity_Home_Language_Review()).commit();
//transaction.addToBackStack(null);
//transaction.commit();
//FragmentTransaction transaction = getFragmentManager().beginTransaction();
//transaction.addToBackStack(null);
//transaction.replace(R.id.frameContent, new Activity_Question_Review_Home()).commit();
}
else {
}
}
/**
* Method that retrieves a random set of questions from
* the database for the given difficulty
* #return
* #throws Error
*/
private List<Question> getQuestionSetFromDb() throws Error {
int diff = getDifficultySettings();
int numQuestions = getNumQuestions();
DBHelper myDbHelper = new DBHelper(getActivity());
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
List<Question> questions = myDbHelper.getQuestionSet(diff, numQuestions);
//List<Question> questions = myDbHelper.getQuestionSet(diff, 2);
myDbHelper.close();
return questions;
}
/**
* Method to return the difficulty settings
* #return
*/
private int getDifficultySettings() {
SharedPreferences settings = getActivity().getSharedPreferences(Constants.SETTINGS, 0);
int diff = settings.getInt(Constants.DIFFICULTY, Constants.MEDIUM);
return diff;
}
/**
* Method to return the number of questions for the game
* #return
*/
private int getNumQuestions() {
SharedPreferences settings = getActivity().getSharedPreferences(Constants.SETTINGS, 0);
int numRounds = settings.getInt(Constants.NUM_ROUNDS, 20);
return numRounds;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
}
Please help. I'm stuck at that line.
Here's the application:
package com.example.civilserviceexamreviewer;
import android.app.Application;
import com.example.civilserviceexamreviewer.quiz.GamePlay;
public class ChuckApplication extends Application{
private GamePlay currentGame;
/**
* #param currentGame the currentGame to set
*/
public void setCurrentGame(GamePlay currentGame) {
this.currentGame = currentGame;
}
/**
* #return the currentGame
*/
public GamePlay getCurrentGame() {
return currentGame;
}
}
Here's the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.civilserviceexamreviewer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Splash1"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Splash2"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Activity_Main"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Question_Review_Home"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_About"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Choices"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<application
android:allowBackup="true"
android:name="com.example.civilserviceexamreviewer.ChuckApplication"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
</application>
</manifest>
The way that you're accessing the Application (or its subclass) from within your Fragment is fine and correct. Since the error you're getting is a ClassCastException, I think that your problem is that you haven't declared your custom Application subclass in the AndroidManifest.xml's application tag. For this reason, calling .getApplication() will actually return an object of type android.app.Application which cannot be typecast to your custom Application class. Perhaps you did do this in your original Activity-based project, but forgot to do so when creating the Fragments-based project?
Example of how to declare your Application subclass in the manifest file:
<application
android:name="com.example.civilserviceexamreviewer.ChuckApplication"
android:icon="..."
android:label="...">
</application>
So, the complete manifest you posted in your question should actually be:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name="com.example.civilserviceexamreviewer.ChuckApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Splash1"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Splash2"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Activity_Main"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Question_Review_Home"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_About"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Choices"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
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().