Class Not found Exception Android - java

I am trying to learn android programming and I am creating an app that starts with a splash screen and loads a menu class after that. the problem is I get this exception
06-04 10:59:37.166: E/AndroidRuntime(926): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.em.example1.MENU" on path: /data/app/com.em.example1-1.apk
I understand what the exception states but I do not understand why this is happening. In my splash screen class I load the Menu activity like this
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread() {
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent mainApp = new Intent("com.em.example1.MENU");
startActivity(mainApp);
}
}
};
timer.start();
and the menu class is defined in the manifest file like this
<activity
android:name="com.em.example1.MENU"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.em.example1.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
When i was loading a main activity with two buttons and a label everything was working ok. But when I changed it (inside my splash screen activity) so it would load Menu Activity it keeps giving me this error.
Thanks in advance

Right click on your project goto properties. Java Build Path. Choose Order export tab. Make sure that Android Private Libraries is selected. If you have referenced library project. do the same for the library project also. Clean and Build.

Maybe you should use this:
Intent mainApp = new Intent(this,com.em.example1.MENU.class);
startActivity(mainApp);

You may use this code, i have made some changes. it may be help u..
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread() {
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
MENU.this.runOnUiThread(new Runnable() {
#Override
public void run() {
Intent mainApp = new Intent(MENU.this,com.em.example1.MENU.class);
MENU.this.startActivity(mainApp);
}
});
}
}
};
timer.start();

the stuff f in manifest before what you listed is what? What you are looking for is that to seee what the app package name is..

Try changing this line in your manifest file.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
And also try this thing
Try going to Project -> Properties -> Java Build Path -> Order & Export and ensure Android Private Libraries are checked for your project and for all other library projects you are using. Clean all projects afterwards and see what happens.

As it turns out I the error was too simple to realize...... I had the word Menu capitalized in Android Manifest in the name and not only in action name. Thanks for trying to help me everyone

Related

Android Nougat, Oreo - How to add long press action to Quick toggles?

I need some help for my app that I'm developing. Current code that I'm using, on long press it launches app info. I want to change that to launch an activity of my app.
The Quick.java class.
#TargetApi(24)
public class Quick extends TileService {
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onTileAdded() {
super.onTileAdded();
}
#Override
public void onTileRemoved() {
super.onTileRemoved();
}
#Override
public void onStartListening() {
super.onStartListening();
}
#Override
public void onStopListening() {
super.onStopListening();
}
#Override
public void onClick() {
super.onClick();
startActivity(Main);
}
}
Just need a onLongClick() method on this code.
In my manifest under <application> tag.
<service
android:name=".Quick"
android:icon="#drawable/ic_quick"
android:label="#string/quick_title"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>
Caution : It's 100% possible to do that in android N and O, for an example take look at this app
Ok I get yes you can implement the Long click listner but here is a issue
Long clicking on your quick settings tile will, by default, go to your app’s info screen. You can override that behavior by adding an intent-filter to one of your activities like so:
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES"/>
</intent-filter>
As you have done in your manifest file here a link that might be helpfull look very closely long press quick setting tile in android

Android display Splash-Screen while loading

I have an Android App, which shows a "Splash Screen" for 3 seconds. After that, the MainActivity gets loaded.
Unfortunately the MainActivity takes additional ~4 seconds to load. On first startup even longer. However when the App is loaded, everything runs smooth.
Now how can I achieve it, that the MainActivity gets loaded, during the display of the Splash Screen? It just should display an image until the whole thing loaded completely.
I have read about Async-Task, but I'm not sure where to put it and how to use it properly. Can someone help me please?
SplashScreen.java
public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_startup);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
}
MainActivity.java
public class MainActivity extends Activity implements OnClickListener, MediaController.MediaPlayerControl {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Some heavy processing
//starting services
//starting Google Text to Speech
//and so on...
}
}
You should not be creating a new thread on startup, instead you should create a view that does not have to wait for your resources to load, as detailed in this article: Splash Screens the Right Way.
As stated in the article, you should create a layer-list drawable instead of a layout XML file:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Fill the background with a solid color -->
<item android:drawable="#color/gray"/>
<!-- Place your bitmap in the center -->
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/ic_launcher"/>
</item>
</layer-list>
Then create a theme using the drawable file as a background. I use the background attribute instead of the windowBackground attribute as suggested in the article, because background takes the status and navigation bars into account, centering the drawable better. I also set windowAnimationStyle to null so that the splash screen does not animate the transition to the MainActivity:
<resources>
<!-- Base application theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<!-- Splash Screen theme -->
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:background">#drawable/background_splash</item>
<item name="android:windowAnimationStyle">#null</item>
</style>
</resources>
Then declare your theme in the manifest for your SplashActivity:
<activity android:name=".SplashActivity"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And finally all you have to do in your SplashActivity is start your MainActivity, and the splash screen will only show for as long as it takes for your app to configure:
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
If there are no specific constraints about the time the splash screen should be shown, you could use the AsyncTask in the following way:
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_startup);
startHeavyProcessing();
}
private void startHeavyProcessing(){
new LongOperation().execute("");
}
private class LongOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
//some heavy processing resulting in a Data String
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "whatever result you have";
}
#Override
protected void onPostExecute(String result) {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
i.putExtra("data", result);
startActivity(i);
finish();
}
#Override
protected void onPreExecute() {}
#Override
protected void onProgressUpdate(Void... values) {}
}
}
If the resulting data if of another nature than a String you could put a Parcelable Object as an extra to your activity. In onCreate you can retrieve the data with:
getIntent().getExtras.getString('data');
How about, in the interest of simplicity, you combine your splash activity with your main activity? That way you get the best of both worlds, namely a splash screen while your data is preparing the first time, and a quick startup when it's been prepared previously. Making the user wait for nothing is not really good form...
Something like:
public class MainActivity extends Activity implements OnClickListener, MediaController.MediaPlayerControl {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initially shows splash screen, the main UI is not visible
setContentView(R.layout.activity_main);
// Start an async task to prepare the data. When it finishes, in
// onPostExecute() get it to call back dataReady()
new PrepareDataAsyncTask(this).execute();
}
public void dataReady() {
// Hide splash screen
// Show real UI
}
}
your splash screen code works fine but when you call next activity then in onCreate() use Asynctask for your heavy tasks...
I have had similar problem. There was a blank loading screen (not even toolbar). Mu culprit was in the manifest in the MainActivity:
android:launchMode="singleInstance"
Just do like in this article:
1 - create a XML layout like this for the splash screen. I called it "background_splash.xml"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/cardview_light_background"/>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/kiss_com_sub_logo"/>
</item>
</layer-list>
2 - Then, go to the styles.xml and write a style like this:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
3 - Write an activity to your splash. I called it SplashActivity.kt
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}
}
4 - Finally, go to you AndroidManifest.xml and add your activity splash: (Note: don't remove anything in the AndroidManifest, just add this before the Main activity).
<activity
android:name=".SplashActivity"
android:label="Kiss"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
This is done. You don't need to worry about the time that your application will demand to start, the splash will be there just for enough time. When your MainActivity is ready, it will be showed.

Android - Unable to create new Intent which triggered from OptionMenu

I am new to Android development and currently stuck when trying to develop a simple app based on This Tutorial
What i want is basically start a new intent when user click on a button via the setting menu (of the said app).
This are some segment of my code:
MainActivity.java
Here i am getting an error with SET_TIME_REQUEST_ID which is a constant that has not been declared anywhere in my code. Should i declare it, i am not sure what is the type of the constant and what value should i assign it with.
*** REST OF THE CODE ****
private void setTime() {
Intent i = new Intent(getBaseContext(), CountdownSetTime.class);
startActivityForResult(i, SET_TIME_REQUEST_ID);
}
*** REST OF THE CODE ***
CountdownSetTime.java
The error i am getting with this part are; context and secondsSet cannot be resolved to any variable. Again, i am not sure what to do with this. Should i declare a variable called secondsSet? If yes, what is the type?
*** REST OF THE CODE ***
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.set_time);
context = this.getApplicationContext(); // ERROR HERE.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayList<Integer> spinnerList = new ArrayList<Integer>();
for (int i = MIN; i <= MAX; i++) {
spinnerList.add(i);
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(context,
android.R.layout.simple_spinner_item, spinnerList);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
secondsSet = (Integer)parent.getItemAtPosition(pos); // ERROR HERE
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
});
}
*** REST OF THE CODE ***
manifest.xml
I am absolutely clueless with this. I am not sure how could i register my new intent.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.gelliesmedia.countdown.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>
<activity
android:name="com.gelliesmedia.countdown.CountdownSetTime"
android:label="#string/app_name"
android:parentActivityName="com.gelliesmedia.countdown.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.gelliesmedia.countdown.MainActivity" />
</activity>
</application>
Could anyone point me to the right direction?
I have read the tutorial you give as a link. That tutorial doesn't give the full code. According to what I see the variables you mention must be defined.
For SET_TIME_REQUEST_ID usually you add this at the beginning with something like that
private static final int SET_TIME_REQUEST_ID = 1;
because onActivityResult(int, int, Intent)
That ID is your internal identification. I put 1 but you can put any number. It is an ID for you so that when the activity closes you can fetch the result.
So yes you have to define it.
Same for secondsSet.
The type seems to be Integer because the parent.getItemAtPostion is cast to Integer. It is used but not defined. Seems to me to be a global variable. The ones you put at the top of your class.
So yes you have to define it also :-)
And finally it is the same for context. It is used but not declared. It seems the tutorial you use declares all these variable globally.
EDIT
The manifest file tells the system that an intent (activity) exist.
You should have some thing like that
<activity
android:name="com.gelliesmedia.countdown.CountdownSetTime">
</activity>

requestFeature() before adding content error when trying to make AndEngine work with Google Play Game Service

I have a game I'm working on, that uses AndEngine.
AndEngine has a "BaseGameActivity" and so does Google Play Game Service. I had to rename BaseGameActivity from AndEngine to AEBaseGameActivity and have it as a parent class BaseGameActivity instead of Activity.
But it is giving me this error:
Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:226)
at org.andengine.util.ActivityUtils.requestFullscreen(ActivityUtils.java:56)
at org.andengine.ui.activity.AEBaseGameActivity.applyEngineOptions(AEBaseGameActivity.java:427)
at org.andengine.ui.activity.AEBaseGameActivity.onCreate(AEBaseGameActivity.java:83)
Now AndEngine has this piece of code:
public static void requestFullscreen(final Activity pActivity) {
final Window window = pActivity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
window.requestFeature(Window.FEATURE_NO_TITLE);
}
if I comment the requestFeature line, my projects runs! But it has an ugly title bar.
Does anyone please know a fix for this ?
EDIT, HERE IS SOME MORE CODE:
PS: AEBaseGameActivity.php extends BaseActivity which extends BaseGameActivity (previously just activity)
AEBaseGameActivity.php
public abstract class AEBaseGameActivity extends BaseActivity implements IGameInterface, IRendererListener {
#Override
protected void onCreate(final Bundle pSavedInstanceState) {
if(BuildConfig.DEBUG) {
Debug.d(this.getClass().getSimpleName() + ".onCreate" + " #(Thread: '" + Thread.currentThread().getName() + "')");
}
super.onCreate(pSavedInstanceState);
this.mGamePaused = true;
this.mEngine = this.onCreateEngine(this.onCreateEngineOptions());
this.mEngine.startUpdateThread();
this.applyEngineOptions(); //REQUEST FULLSCREEN
this.onSetContentView(); //SET CONTENT VIEW
}
...
private void applyEngineOptions() {
final EngineOptions engineOptions = this.mEngine.getEngineOptions();
if(engineOptions.isFullscreen()) {
ActivityUtils.requestFullscreen(this); //ACTIVITY UTIL SHOWN LATER
}
...
}
...
protected void onSetContentView() {
this.mRenderSurfaceView = new RenderSurfaceView(this);
this.mRenderSurfaceView.setRenderer(this.mEngine, this);
this.setContentView(this.mRenderSurfaceView, AEBaseGameActivity.createSurfaceViewLayoutParams());
}
}
ActivityUtils.java
public class ActivityUtils {
public static void requestFullscreen(final Activity pActivity) {
final Window window = pActivity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
window.requestFeature(Window.FEATURE_NO_TITLE); //IF COMMENTING THIS, THE GAME IS RUNNING
}
...
}
EDIT2:
The code is basically only AndEngine, here is the original code:
https://github.com/nicolasgramlich/AndEngine/tree/GLES2/src/org/andengine/ui/activity
My changes:
renamed BaseGameActivity to AEBaseGameActivity
BaseActivity extends BaseGameActivity (taken from Google Play Game Service) instead of Activity
BaseGameActivity and GameHelper.java taken from BaseGameUtils from Google Play Game Service.
Ok so this is what I did:
I commented out the line that is causing the problem (ActivityUtils.java line 56 of AndEngine)
I added this in my activity to the Android Manifest:
android:theme="#android:style/Theme.NoTitleBar"
It ends up looking something like:
<activity
android:name=".GameActivity"
android:theme="#android:style/Theme.NoTitleBar"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Exception requestFeature() must be called before adding content telling everything
call requestFullscreen() before setContenview()
Edit
try REQUEST FULLSCREEN after super.onCreate()
#Override
protected void onCreate(final Bundle pSavedInstanceState) {
super.onCreate(pSavedInstanceState);
this.applyEngineOptions(); //REQUEST FULLSCREEN

How to set alarm when mobile has been idle using android?

Hi I want to set alarm when the phone hasn't been touched. If the screen hasn't been touched for nearly 2 minutes, the alarm sound would be raised. How can I do this? Can anybody help me?
Thanks in advance.
Pass the AlarmService through out the below code. This will find how long your device has been in idle.
idle.java
Handler hl_timeout = new Handler();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try{
hl_timeout.postDelayed(DoOnTimeOut, 15000);
}catch(Exception e)
{
e.printStackTrace();
}
}
// Toast
Thread DoOnTimeOut = new Thread() {
public void run() {
try{
Toast.makeText(getApplicationContext(), "System is idle", Toast.LENGTH_LONG).show();
}catch(Exception e)
{
e.printStackTrace();
}
}
};
#Override
public void onUserInteraction()
{
super.onUserInteraction();
//Remove any previous callback
try{
hl_timeout.removeCallbacks(DoOnTimeOut);
hl_timeout.postDelayed(DoOnTimeOut, 15000);
}catch(Exception e)
{
e.printStackTrace();
}
}
Hope this helps you.
You can create AlarmService to play sound even when application/device is idle
Extend method onUserInteraction of Activity Class to reset timer and start again for two minutes.
If I understand your question correctly, you need to trigger an alarm, when there is no any user interaction happened within some time interval. And one of the main requirements, there is no your activity running in foreground. So the case with onUserInteraction doesn't work for you.
In this case you still can receive updates on every user action if you make your own AccessibilityService.
Add this to your manifest to declare the service:
<service android:name=".MyAccessibilityService">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
</service>
And your service implementation should look similar to this:
public class MyAccessibilityService extends AccessibilityService
{
#Override
public void onServiceConnected() {
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
// we are interested in all types of accessibility events
info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
// we want to receive events in a certain interval
info.notificationTimeout = 100;
setServiceInfo(info);
Log.e("ALEX", "service connected!");
}
#Override
public void onAccessibilityEvent(AccessibilityEvent arg0)
{
Log.e("ALEX", "Event happened!");
}
}
Now any time user touches the screen for example, you will get onAccessibilityEvent() triggered and will be able to restart a timer which will launch your Alarm.
HOWEVER, this approach has one disadvantage: in order to make it working, you will need to go to the phone settings -> Accessibility and enable your application there. Otherwise, system will not launch your service.

Categories