Activity after splash screen - java

Once my splash screen display for 1000ms I receive an error stating "The application has stopped unexpectedly. Please try again." It seems an activity that is supposed to start after the splash screen is not working. before the splash screen, everything worked fine. Logcat
shows the following error " E/AndroidRuntime(5480): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxx.home/com.xxxxx.home.xxxxx}: java.lang.NullPointerException. I beleive the issue is with my Splash Class but cannot pin point where. Any insight would be greatly appreciated.
public class Splash extends Activity{
private final int SPLASH_DISPLAY_LENGTH = 1000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent openxxxxx = new Intent("com.xxxxx.home.XXXXX");
startActivity(openxxxxx);
}
}, SPLASH_DISPLAY_LENGTH);
}
}

Here is the complete code you can use this,
package com.fsp.slideview;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
public class ImageSplashActivity extends Activity {
/**
* The thread to process splash screen events
*/
private Thread mSplashThread;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
final ImageSplashActivity sPlashScreen = this;
mSplashThread = new Thread() {
#Override
public void run() {
try {
synchronized (this) {
wait(2000);
}
} catch (InterruptedException ex) {
}
finish();
Intent intent = new Intent();
intent.setClass(sPlashScreen, SlideMainActivity.class);
startActivity(intent);
}
};
mSplashThread.start();
}
#Override
public boolean onTouchEvent(MotionEvent evt) {
if (evt.getAction() == MotionEvent.ACTION_DOWN) {
synchronized (mSplashThread) {
mSplashThread.notifyAll();
}
}
return true;
}
}

Related

Game crashes after switching from Main activity to Game Panel class (Android Studio)

I made 2D game and it worked just fine. I decided to create a simple "Start Screen" that ought to show high score and "Start" button that should start the game.
I thought I could use just a regular intent to go from one activity to another, but it doesn't work. App simply crashes after I click "Start" button.
I'm using Scene Manager so not all code is written in Game Panel.
Here's my MainActivity
package com.example.korisnik_pc.a2dgame;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Button button;
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // MAKE IT FULLSCREEN
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // REMOVES TOOLBAR
// setting the screen dimensions
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
Constants.SCREEN_HEIGHT = dm.heightPixels;
Constants.SCREEN_WIDTH = dm.widthPixels;
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, GamePanel.class);
startActivity(intent);
}
});
}
Game Panel
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {
private MainThread thread;
private SceneManager sceneManager;
public GamePanel(Context context) {
super(context);
getHolder().addCallback(this);
thread = new MainThread(getHolder(), this);
sceneManager = new SceneManager();
setFocusable(true);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
thread = new MainThread(getHolder(), this);
thread.setRunning(true);
thread.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (true) {
try {
thread.setRunning(false);
thread.join();
} catch (Exception e) {
e.printStackTrace();
}
retry = false;
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
sceneManager.receiveTouch(event);
return true;
}
public void update() {
sceneManager.update();
}
public void draw(Canvas canvas) {
super.draw(canvas);
sceneManager.draw(canvas);
}
}

timer java start activity

I am implementing a splash screen as per the tutorial here, however the splash screen disappears very quickly almost instantly. What would be the best way to include a timer to only start the new activity after for example 1 second. My Splash screen Activity file looks as follows:
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
The approach relies on a drawable and style resource.
Code for Splash :-
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
public class Splash extends AppCompatActivity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_activity);
// Showing splash screen with a timer.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// Start your application main_activity
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
// Close this activity
finish();
}
}, SPLASH_TIME_OUT); // Timer
}
}
The simplest way would be to use Handler:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}, DateUtils.SECOND_IN_MILLIS);
public class SplahActivity extends Activity {
public static final int Tick = 1000;
public static final int Complete = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new CountDownTimer(Complete, Tick) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
//start Activity
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}.start();
}
Use this
timerHandler = new Handler();
getmi_runnable = new Runnable() {
#Override
public void run() {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
finish();
}
};
timerHandler.postDelayed(getmi_runnable, 4000L);

How to keep IOIO connected when android screen goes off?

I am using the following code with IOIO to act as a motion detector, the problem is the IOIO is disconnected whenever my phone screen goes off! I do not want the screen to stay on all the time to keep the IOIO connected!
any solution please?
package com.LookHin.ioio_pir_motion_sensor;
import ioio.lib.api.AnalogInput;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.android.IOIOActivity;
import android.content.Intent;
import android.graphics.Color;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends IOIOActivity {
private ToggleButton toggleButton1;
private TextView textView1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_about:
//Toast.makeText(getApplicationContext(), "Show About", Toast.LENGTH_SHORT).show();
Intent about = new Intent(this, AboutActivity.class);
startActivity(about);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
class Looper extends BaseIOIOLooper {
private DigitalOutput digital_led0;
private AnalogInput deigital_input;
int i = 0;
private float InputStatus;
#Override
protected void setup() throws ConnectionLostException {
digital_led0 = ioio_.openDigitalOutput(0,true);
deigital_input = ioio_.openAnalogInput(45);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "IOIO Connect", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public void loop() throws ConnectionLostException {
try{
digital_led0.write(!toggleButton1.isChecked());
InputStatus = deigital_input.getVoltage();
runOnUiThread(new Runnable() {
#Override
public void run() {
textView1.setText(String.format("%.02f",InputStatus)+" v.");
if(InputStatus >= 3.0){
textView1.setBackgroundColor(Color.RED);
if (i == 0){
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
i = 1;
};
}else{
textView1.setBackgroundColor(Color.TRANSPARENT);
i = 0;
}
}
});
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
#Override
protected IOIOLooper createIOIOLooper() {
return new Looper();
}
}
Option 1) Lock the screen so it always stays awake
public void onCreate(Bundle savedInstanceState) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Option 2) Fix your response to onPause().
When the screen goes off the onPause() method is called and you should handle it as otherwise your activity will be closed.
#Override
protected void onPause() {
// Your code
super.onPause();
}
The onPause() normally calls the ioio.disconnect() so this should be overrode.
IOIOService let´s you run your code on the background even if application goes to the background.

How to close a splash activity?

I have an Android program that includes splash activity. when I run the program everything working perfectly but when press the back button I see my splash activity again .to be able to avoid this it seems I need to close my splash but I can't
here is my code
package com.tesbih;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity {
#Override
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 openStartingPoint = new Intent ("com.tesbih.TESBIHMAINACTIVITY");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
}
Add a TimerTask to your code.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
TimerTask splash = new TimerTask(){
public void run() {
onDestroy();
startActivity(new Intent(Splash.this, MainActivity.class));
}
};
Timer splashScreenTimer = new Timer();
// Timer set to 4.5 seconds
splashScreenTimer.schedule(splash, 5000);
}
Call finish() to finish your Splash Activity.
Do like this
Intent openStartingPoint = new Intent ("com.tesbih.TESBIHMAINACTIVITY");
startActivity(openStartingPoint);
finish();

Implementation of periodic task in android

I want to create a small android app that would show the system time in periodic intervals after clicking on a button ( i.e. setting the activity up )...The code for button creation and setting the periodic activity via Intent goes like this :
package com.example.timeupdate;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button;
TextView show;
#Override
protected void onCreate(Bundle I_Love_Biriyani) {
super.onCreate(I_Love_Biriyani);
setContentView(R.layout.activity_main);
button = (Button) findViewById (R.id.pressButton);
show = (TextView) findViewById (R.id.Show);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent openTimeUpdater = new Intent("com.example.timeupdate.TIMEUPDATER");
startActivity(openTimeUpdater);
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And here is the code for repeating the timer( for say 5 seconds ) where I used TimerTask class to perform the job :
package com.example.timeupdate;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class TimeUpdater extends Activity {
TextView Show;
TimerTask timer= new TimerTask(){
#Override
public void run() {
// TODO Auto-generated method stub
Date d = new Date();
Show.setText(""+d);
}
};
#Override
protected void onCreate(Bundle hotovaga) throws IllegalStateException {
// TODO Auto-generated method stub
super.onCreate(hotovaga);
setContentView(R.layout.new_update);
Show = (TextView) findViewById (R.id.time);
Timer t = new Timer();
t.scheduleAtFixedRate(timer , 0 , 5000);
}
}
After clicking on the button the time is shown only once then application is getting stopped showing a dialog-message. Need explanations to do this job in the same fashion.
You are trying to access an UI element inside non-UI thread.
Show.setText(""+d);
Instead, wrap it up in runOnUiThread interface to get proper output.
Use below code for your TimeUpdater class
public class TimeUpdater extends Activity {
TextView Show = null;
Calendar c;
int seconds;
int minutes;
int hours;
TimerTask timer= new TimerTask(){
#Override
public void run() {
c = Calendar.getInstance();
seconds = c.get(Calendar.SECOND);
minutes = c.get(Calendar.MINUTE);
hours = c.get(Calendar.HOUR);
runOnUiThread(new Runnable() {
#Override
public void run() {
Show.setText(hours + ":" + minutes + ":" + seconds);
}
});
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_update);
Show = (TextView) findViewById (R.id.time);
Timer t = new Timer();
t.scheduleAtFixedRate(timer , 0 , 5000);
}
}
Using an actual Timer (java.util.Timer) in conjunction with runOnUiThread() is one way to solve this issue, and below is an example of how to implement it.
public class myActivity extends Activity {
private Timer myTimer;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
#Override
public void run() {
TimerMethod();
}
}, 0, 1000);
}
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.
//We call the method that will work with the UI
//through the runOnUiThread method.
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
//This method runs in the same thread as the UI.
// Set your textView data here.
//Do something to the UI thread here
}
};
}
use PeriodicTask from Play-Service, it is the newest tool from Google to schedule a job background.

Categories

Resources