onclick not working after starting another activity - java

I have two Activities MainActivity and Register when i intent from MainActivity to Register the button listener in Register class is not working.
Here is the code
MainActivity
public class MainActivity extends AppCompatActivity {
// defining all the components
AlertDialog builder;
public EditText Email,Password;
public ImageButton fb,twitter;
public Button register;
public String email;
public String password;
public BackgroungWorker backgroungWorker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
builder = new AlertDialog.Builder(this).create();
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
Email= (EditText) findViewById(R.id.email);
Password= (EditText) findViewById(R.id.pass);
fb= (ImageButton) findViewById(R.id.fb_button);
twitter= (ImageButton) findViewById(R.id.tw_button);
register = (Button) findViewById(R.id.register);
}
// for listening to the events
public void eventHappend(View view)
{
if(view.getId()==R.id.login)
{
email = Email.getText().toString();
password = Password.getText().toString();
if(email.equals(""))
{
showMessage("Error","Email cannot be blank");
return;
}
if(password.equals(""))
{
showMessage("Error","Enter your password");
return;
}
backgroungWorker = new BackgroungWorker(this);
backgroungWorker.execute("Login",email,password);
}
if(view.getId()==R.id.register)
{
// INTENT TO REGISTER ACTIVITY
Intent intent = new Intent(MainActivity.this,Register.class);
startActivity(intent);
}
if(view.getId()==R.id.forgot)
{
}
if(view.getId()== R.id.fb_button)
{
showToast("Facebook!!");
}
if(view.getId()==R.id.tw_button)
{
showToast("Twitter");
}
}
public void showToast(String s)
{
Toast toast = new Toast(this);
toast.makeText(this,s, Toast.LENGTH_SHORT).show();
}
public void showMessage(String title,String message)
{
builder.setTitle(title);
builder.setMessage(message);
builder.setCancelable(true);
builder.show();
}}
Register
public class Register extends Activity {
String user_name,pass,confim_password,email;
AlertDialog builder;
private EditText name,mail,password,confirm_pass;
private Button register;
private BackgroungWorker backgroungWorker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
mail = (EditText) findViewById(R.id.email);
name = (EditText) findViewById(R.id.name);
password = (EditText) findViewById(R.id.pass);
confirm_pass= (EditText) findViewById(R.id.confirmpass);
register = (Button) findViewById(R.id.register_btn);
builder = new AlertDialog.Builder(this).create();
}
public void registerClicked(View view) // this is not working
{
if(view.getId()==R.id.register_btn)
{
if(name.getText().equals(""))
{
showMessage("Error","Name Cannot be left blank");
return;
}
if(mail.getText().equals(""))
{
showMessage("Error","Email Cannot be left blank");
return;
}
if(password.getText().equals(""))
{
showMessage("Error","Password Cannot be left blank");
return;
}
if(password.getText().equals(confirm_pass.getText().toString()))
{
backgroungWorker = new BackgroungWorker(this);
backgroungWorker.execute("Register",name.getText().toString(),password.getText().toString(),mail.getText().toString());
}
}
}
public void showMessage(String title,String message)
{
builder.setTitle(title);
builder.setMessage(message);
builder.setCancelable(true);
builder.show();
}}
register.xml
`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
android:background=
android:backgroundTint="#02b864">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#02b864">
<ImageView
android:layout_width="212dp"
android:layout_height="176dp"
android:id="#+id/imageView"
android:layout_gravity="center_horizontal"
android:background="#drawable/splash"
android:layout_marginTop="20dp"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp">
<EditText
android:layout_width="wrap_content"
android:layout_height="50dp"
android:hint="Name"
android:id="#+id/name"
android:drawableLeft="#drawable/ic_username"
android:drawableTint="#color/colorPrimaryDark"
android:drawablePadding="#dimen/activity_vertical_margin"
android:focusable="true"
android:background="#drawable/editext_bg_gray"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:singleLine="true"
android:layout_marginTop="20dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="50dp"
android:hint="Email"
android:id="#+id/email"
android:drawableLeft="#drawable/ic_username"
android:drawableTint="#color/colorPrimaryDark"
android:drawablePadding="#dimen/activity_vertical_margin"
android:focusable="true"
android:background="#drawable/editext_bg_gray"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:singleLine="true"
android:layout_marginTop="5dp"
/>
<EditText
android:inputType="textPassword"
android:ems="10"
android:id="#+id/pass"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:hint="Password"
android:drawableLeft="#drawable/ic_pwd"
android:drawablePadding="#dimen/activity_horizontal_margin"
android:drawableTint="#color/colorPrimaryDark"
android:focusable="true"
android:background="#drawable/editext_bg_gray"
android:layout_gravity="center_horizontal"
android:layout_marginRight="25dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="5dp"
android:singleLine="true" />
<EditText
android:inputType="textPassword"
android:ems="10"
android:id="#+id/confirmpass"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:hint="Confirm Password"
android:drawableLeft="#drawable/ic_pwd"
android:drawablePadding="#dimen/activity_horizontal_margin"
android:drawableTint="#color/colorPrimaryDark"
android:focusable="true"
android:background="#drawable/editext_bg_gray"
android:layout_gravity="center_horizontal"
android:layout_marginRight="25dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="5dp"
android:singleLine="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/register_btn"
android:onClick="registerClicked"
android:clickable="true"
android:layout_gravity="center_horizontal"
android:background="#e5e7e8"
android:text="Register"
android:src="#drawable/editext_bg_gray"
android:textColor="#0fbb6c"
android:singleLine="true"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="50dp"
android:textStyle="normal"
android:textAlignment="center"
android:textSize="22dp"
/>
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>`
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.umarsafdar.dev96.chat">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
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=".Splash"
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=".Register"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.DEFAULT" />
</action>
</intent-filter>
</activity>
</application>
</manifest>
thanks in advance.

Related

I'm trying to add Splash Screen to my app

I have been trying to implement splash Screen to my app with help of many codes avaible in sites , but none worked for me.
Each time the app is crashing after displaying splash screen for 3 secs.
I don't know where i'm going wrong , please make corrections to my code to display splash screen correctly ! Thank You !
//This is my Main activity
1. public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button) this.findViewById(R.id.gg);
final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.gg);
one.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Good Game", Toast.LENGTH_SHORT).show();
mp1.start();
}
});
Button two = (Button) this.findViewById(R.id.gm);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.gm);
two.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Oh, They got me!", Toast.LENGTH_SHORT).show();
mp2.start();
}
});
Button three = (Button) this.findViewById(R.id.cb);
final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.cb);
three.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Come on boy", Toast.LENGTH_SHORT).show();
mp3.start();
}
});
Button four = (Button) this.findViewById(R.id.ns);
final MediaPlayer mp4 = MediaPlayer.create(this, R.raw.ns);
four.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Nice shot", Toast.LENGTH_SHORT).show();
mp4.start();
}
});
Button five = (Button) this.findViewById(R.id.wp);
final MediaPlayer mp5 = MediaPlayer.create(this, R.raw.wp);
five.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(MainActivity.this, "You wanna piece of me!", Toast.LENGTH_SHORT).show();
mp5.start();
}
});
Button six = (Button) this.findViewById(R.id.bi);
final MediaPlayer mp6 = MediaPlayer.create(this, R.raw.bi);
six.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Bring it", Toast.LENGTH_SHORT).show();
mp6.start();
}
});
Button seven = (Button) this.findViewById(R.id.lg);
final MediaPlayer mp7 = MediaPlayer.create(this, R.raw.lg);
seven.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Let’s go, Yeah!", Toast.LENGTH_SHORT).show();
mp7.start();
}
});
Button eight = (Button) this.findViewById(R.id.ru);
final MediaPlayer mp8 = MediaPlayer.create(this, R.raw.ru);
eight.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Ready Up", Toast.LENGTH_SHORT).show();
mp8.start();
}
});
Button nine = (Button) this.findViewById(R.id.nn);
final MediaPlayer mp9 = MediaPlayer.create(this, R.raw.nn);
nine.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Nooooo", Toast.LENGTH_SHORT).show();
mp9.start();
}
});
Button ten = (Button) this.findViewById(R.id.cm);
final MediaPlayer mp10 = MediaPlayer.create(this, R.raw.cm);
ten.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Cover me", Toast.LENGTH_SHORT).show();
mp10.start();
}
});
Button eleven = (Button) this.findViewById(R.id.hh);
final MediaPlayer mp11 = MediaPlayer.create(this, R.raw.hh);
eleven.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Hoo-ya", Toast.LENGTH_SHORT).show();
mp11.start();
}
});
Button twelve = (Button) this.findViewById(R.id.mo);
final MediaPlayer mp12 = MediaPlayer.create(this, R.raw.mo);
twelve.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Move out", Toast.LENGTH_SHORT).show();
mp12.start();
}
});
Button thirteen = (Button) this.findViewById(R.id.gs);
final MediaPlayer mp13 = MediaPlayer.create(this, R.raw.gs);
thirteen.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Get Some", Toast.LENGTH_SHORT).show();
mp13.start();
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Your Messege",Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
//This is my activity_main.xml
2. <android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.android.mmm.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end"
app:srcCompat="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
//This is content_main.xml
3. <android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.android.mmm.MainActivity"
tools:showIn="#layout/activity_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000">
<Button
android:layout_width="100dp"
android:layout_height="48dp"
android:text="GG"
android:id="#+id/gg"
android:textSize="18sp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#000000"
tools:ignore="HardcodedText"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"/>
<Button
android:id="#+id/gm"
android:layout_width="100dp"
android:layout_height="48dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:text="GM"
android:textAllCaps="true"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<Button
android:layout_width="100dp"
android:layout_height="48dp"
android:text="CB"
android:id="#+id/cb"
android:textSize="18sp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#000000"
tools:ignore="HardcodedText"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="100dp"
android:layout_height="48dp"
android:text="NS"
android:id="#+id/ns"
android:textSize="18sp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#000000"
tools:ignore="HardcodedText"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="100dp"
android:layout_height="48dp"
android:text="WP"
android:id="#+id/wp"
android:textSize="18sp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#000000"
tools:ignore="HardcodedText"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="100dp"
android:layout_height="48dp"
android:text="BI"
android:id="#+id/bi"
android:textSize="18sp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#000000"
tools:ignore="HardcodedText"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="100dp"
android:layout_height="48dp"
android:text="LG"
android:id="#+id/lg"
android:textSize="18sp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#000000"
tools:ignore="HardcodedText"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="100dp"
android:layout_height="48dp"
android:text="RU"
android:id="#+id/ru"
android:textSize="18sp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#000000"
tools:ignore="HardcodedText"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="100dp"
android:layout_height="48dp"
android:text="NN"
android:id="#+id/nn"
android:textSize="18sp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#000000"
tools:ignore="HardcodedText"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="100dp"
android:layout_height="48dp"
android:text="CM"
android:id="#+id/cm"
android:textSize="18sp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#000000"
tools:ignore="HardcodedText"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="100dp"
android:layout_height="48dp"
android:text="HH"
android:id="#+id/hh"
android:textSize="18sp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#000000"
tools:ignore="HardcodedText"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="100dp"
android:layout_height="48dp"
android:text="MO"
android:id="#+id/mo"
android:textSize="18sp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#000000"
tools:ignore="HardcodedText"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="100dp"
android:layout_height="48dp"
android:text="GS"
android:id="#+id/gs"
android:textSize="18sp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#000000"
tools:ignore="HardcodedText"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
//This is AndroidManifest.xml
4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.mmm">
<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">
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
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=".MainActivity"
android:label="#string/app_name" >
</activity>
</application>
//This is styles.xml
5. <resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
//This is my SplashActivity.Java
public class SplashScreen extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
6. //This is activity_splash.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/wwe_logo" />
</RelativeLayout>
AppCompatActivity has it's own toolbar so you need to remove it by using android:theme="#style/AppTheme.NoActionBar" in manifest
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
</activity>
Although if you are not doing much customization in your toolbar then your can also skip creating your own toolbar by removing it from XML and removing it's initialization from java code as well but by doing this , you will lose the feature of collapsing toolbar
Making a splash screen is actually pretty easy.
1. Create drawable
In my case, I created a drawable named splash.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item android:drawable="#color/colorWhite" />
<item>
<bitmap
android:gravity="center"
android:src="#drawable/sensesmart_icon" />
</item>
2. Add drawable to your styles.xml
Here I just added my drawable to styles:
<style name="SplashTheme" parent="AppTheme">
<item name="android:windowBackground">#drawable/splash</item>
</style>
3. Add splash to manifest
You want to actually tell your app that the splash layout should show before the app has loaded:
<activity android:name=".BaseActivity"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme"> <!--Splash screen-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
4. Set theme on your starting activity
In my case, BaseActivity is the activity which starts my application, so inside my onCreate(), I start with setting my theme:
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
setContentView(R.layout.activity_base);
super.onCreate(savedInstanceState);
}
This is the only thing you need to do. If you use this method, the splash screen doesn't stay for longer than it needs. Your contentView will start as soon as you have loaded your app.
Hope this helps!

android - interstitial ad showing on click

In my new game I want to show interstitial ads. With a button click the game restarts and I want the ads to show up only every 15th time. The problem is that it is shown every time the game starts. Where is the fault?
public class MainActivity extends AppCompatActivity {
 
    InterstitialAd mInterstitialAd;
public int playcount = 0;
 
    #Override
    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
mInterstitialAd = new InterstitialAd(this);
// set the ad unit ID
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
AdRequest adRequest = new AdRequest.Builder()
.build();
// Load ads into Interstitial Ads
mInterstitialAd.loadAd(adRequest);
TextView playctv = (TextView) findViewById(R.id.textView);
SharedPreferences prefsplay = this.getSharedPreferences("myPrefsKey",
Context.MODE_PRIVATE);
playcount = prefsplay.getInt("play_number", 0);
playctv.setText(String.valueOf(playcount));
}
public void restart(View view) {
Intent intent = new Intent(this, GameScreen.class);
startActivity(intent);
playcount++;
SharedPreferences prefsplay = this
.getSharedPreferences("myPrefsKey",
Context.MODE_PRIVATE);
prefsplay.edit().putInt("play_number", playcount)
.apply();
}
private void showInterstitial() {
if (mInterstitialAd.isLoaded())
mInterstitialAd.show();
}
private void adView() {
mAdView = (AdView) findViewById(R.id.adView);
MobileAds.initialize(getApplicationContext(), getResources().getString(R.string.interstitial_full_screen));
final AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
public void gameover() {
if (playcount == 10) {
playcount = 0;
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
mInterstitialAd.show(); //We are executing this code only if ad is loaded, no need to check it again
SharedPreferences prefsplay = getApplicationContext()
.getSharedPreferences("myPrefsKey",
Context.MODE_PRIVATE);
prefsplay.edit().putInt("play_number", playcount)
.apply();
AdRequest adRequest = new AdRequest.Builder()
.build();
}
});
}
}
}
Activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:id="#+id/rect" >
<ImageView
android:id="#+id/imageview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageview1"
android:layout_alignTop="#+id/imageview1"
android:alpha="0.7"
android:gravity="center"
android:text="Score"
android:textSize="15sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:alpha="0.9"
android:gravity="center"
android:text="sco"
android:textSize="25sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="0:500"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:alpha="0.7"
android:layout_alignTop="#+id/textView2"
android:text="Highscore"
android:textSize="15sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView3"
android:layout_alignRight="#+id/textView4"
android:text="Hs"
android:textSize="25sp" />
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
ads:adSize="SMART_BANNER" ads:adUnitId="#string/banner_home_footer"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
</com.google.android.gms.ads.AdView>
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_default"
android:onClick="restart"
android:src="#android:drawable/ic_menu_revert"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_alignLeft="#+id/imageButton1"
android:layout_alignStart="#+id/imageButton1"
android:id="#+id/textView" />
</RelativeLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx"
android:versionCode="2"
android:versionName="1.1" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="#drawable/logoone"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:screenOrientation="sensorPortrait"
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=".GameScreen"
android:screenOrientation="sensorPortrait"
android:label="#string/app_name" />
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
Your code needs a lot of fixing. I'm putting one by one here.
The restart function should use startActivityForResult
private final int ACTION_GAME = 100;
public void restart(View view) {
Intent intent = new Intent(this, GameScreen.class);
startActivityForResult(intent, ACTION_GAME);
playcount++;
SharedPreferences prefsplay = this
.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
prefsplay.edit().putInt("play_number", playcount).apply();
}
As you've started the GameScreen activity for a result, you need to override the onActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACTION_GAME:
gameover();
break;
default:
break;
}
}
Now in the gameover function you need to check the saved value in your preference for the playcount.
public void gameover() {
// Get the saved value
SharedPreferences prefsplay = this
.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
playcount = prefsplay.getInt("play_number", 0);
if (playcount >= 10) {
playcount = 0;
showInterstitial();
SharedPreferences prefsplay = getApplicationContext()
.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
prefsplay.edit().putInt("play_number", playcount).apply();
// Start loading the new ad
loadNewAd();
}
}
I've introduced loadNewAd() function which might look like this
private void loadNewAd() {
final AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
And the onCreate function needs to be modified like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
mInterstitialAd = new InterstitialAd(this);
// set the ad unit ID
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
// Call adView here
adView();
TextView playctv = (TextView) findViewById(R.id.textView);
SharedPreferences prefsplay = this.getSharedPreferences("myPrefsKey",
Context.MODE_PRIVATE);
playcount = prefsplay.getInt("play_number", 0);
playctv.setText(String.valueOf(playcount));
}
Finally, as you're using startActivityForResult you need to set the result while finishing your GameScreen activity. So when you finish or return from the GameScreen activity, you need to do this.
GameScreen.setResult(Activity.RESULT_OK);
GameScreen.finish();

Source not found Error and App closes Abruptly

I'm new to Android programming and have been searching this problem for a day now and couldn't come up with a solution. Kindly help me in solving this small problem which is creating a big problem for me.
The error is that when I press the login button in my MainActivity, the app says that unfortunately it has stopped working and the debug window shows an error "Source not found" and further it says ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2249
I've seen similar posts related to this but those posts couldn't help me.
Following my code.
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Base.AppCompat.Light.DarkActionBar" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Second"
android:label="#string/title_activity_second" >
</activity>
<activity
android:name=".Registration"
android:label="#string/title_activity_registration" >
</activity>
</application>
</manifest>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
final Button button2=(Button) findViewById(R.id.button2_id);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent j = new Intent(MainActivity.this,Registration.class);
startActivity(j);
}
});
}
}
MainActivity_xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.firstapp.MainActivity" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textView1"
android:text="Welcome To Foodparkk"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/button_id"
android:layout_below="#+id/textView3"
android:layout_marginTop="45dp"
android:contentDescription="#null"
android:src="#drawable/robo" />
<Button
android:id="#+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textView3"
android:text="Login" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText3"
android:layout_alignTop="#+id/button2_id"
android:text="E-Mail"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText3"
android:layout_below="#+id/editText3"
android:layout_marginTop="19dp"
android:text="Password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:ems="10"
android:inputType="textPassword" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:ems="10"
android:inputType="textEmailAddress" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button2_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button_id"
android:layout_alignRight="#+id/button_id"
android:layout_marginBottom="42dp"
android:text="Register" />
</RelativeLayout>
activity_second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.firstapp.Second">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/spinner1"
android:layout_alignParentTop="true"
android:layout_marginTop="44dp"
android:text="Select your Meal"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
</RelativeLayout>
Second.java
public class Second extends ActionBarActivity implements OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(this);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.food_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected (AdapterView<?> parent) {
// code
}
}
In your second.java class you are referencing your layout of main activity
public class Second extends ActionBarActivity implements OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Change SetContentView to
setContentView(R.layout.activity_second);
A "Source Not Found" error will be thrown when a class or activity can't be found or instantiated.
Please add the full LogCat log or at least the full ErrorStack.

The app I am working on crashes when i try to go from one intent to the other, I've tried everything. code:

The Start class:
It as a very simple program, i designed two screen, and by pressing a button on the main screen i wanted the app to open the second screen, but unfortunately its not happening, the app keeps crushing over and over again.
package com.example.snakesnladders;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class Start extends Activity implements OnClickListener {
Button start, settings;
TextView snakes, and, ladders;
ImageView snakePic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
init();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
private void init() {
start = (Button) findViewById(R.id.btStart);
settings = (Button) findViewById(R.id.btSettings);
snakes = (TextView) findViewById(R.id.tvSnakes);
and = (TextView) findViewById(R.id.tvAnd);
ladders = (TextView) findViewById(R.id.tvLadders);
snakePic = (ImageView) findViewById(R.id.snakePic);
start.setOnClickListener(this);
settings.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btStart:
break;
case R.id.btSettings:
Intent i = new Intent("com.example.snakesnladders.SET");
startActivity(i);
break;
default: break;
}
}
}
The Set class:
package com.example.snakesnladders;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class Set extends Activity implements OnClickListener {
Button sound, difficulty, back;
TextView settings;
ImageView snakePic;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.setscreen);
init();
}
private void init() {
sound = (Button) findViewById(R.id.btSound);
difficulty = (Button) findViewById(R.id.btDifficulty);
back = (Button) findViewById(R.id.btBack);
settings = (TextView) findViewById(R.id.tvSetPage);
snakePic = (ImageView) findViewById(R.id.setSnakePic);
sound.setOnClickListener(this);
difficulty.setOnClickListener(this);
back.setOnClickListener(this);
}
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.btSound:
String s = sound.getText().toString();
if (s.equals("Sound:on")) {
sound.setText("Sound:off");
ControlSounds.player.stop();
} else {
sound.setText("Sound:on");
ControlSounds.player.start();
}
break;
case R.id.btDifficulty:
break;
case R.id.btBack:
Intent i = new Intent(Set.this, Start.class);
startActivity(i);
finish();
break;
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
The manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.snakesnladders"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Start"
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=".Set"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.snakesnladders.SET" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
mainscreen.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="175dp"
android:orientation="vertical" >
<TextView
android:id="#+id/tvSnakes"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="Snakes"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/green"
android:textSize="30sp" />
<TextView
android:id="#+id/tvAnd"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="#string/and"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/green"
android:textSize="30sp" />
<TextView
android:id="#+id/tvLadders"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="Ladders"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/green"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="125dp"
android:orientation="vertical" >
<Button
android:id="#+id/btStart"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Start New Game"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<Button
android:id="#+id/btSettings"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Settings"
android:textColor="#FFFFFF"
android:textSize="30sp" />
</LinearLayout>
<ImageView
android:id="#+id/snakePic"
android:layout_width="wrap_content"
android:layout_height="125dp"
android:layout_gravity="center"
android:layout_weight="0.47"
android:background="#color/black"
android:src="#drawable/snake" />
</LinearLayout>
setscreen.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:orientation="vertical" >
<TextView
android:id="#+id/tvSetPage"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_gravity="center"
android:text="Settings"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/green"
android:textSize="40sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:orientation="vertical" >
<Button
android:id="#+id/btSound"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Sound:on"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<Button
android:id="#+id/btDifficulty"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Difficulty:easy"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<Button
android:id="#+id/btBack"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Back To Menu"
android:textColor="#FFFFFF"
android:textSize="30sp" />
</LinearLayout>
<ImageView
android:id="#+id/setSnakePic"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.25"
android:background="#color/black"
android:src="#drawable/snake1" />
</LinearLayout>
Change this
Intent i = new Intent("com.example.snakesnladders.SET");
startActivity(i);
To
Intent i = new Intent(Start.this,Set.class);
startActivity(i);
And Change this
<activity
android:name=".Set"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.snakesnladders.SET" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
To
<activity
android:name=".Set"
android:label="#string/app_name" >
</activity>
Use Explicit Intent's
To know why read
http://developer.android.com/guide/components/intents-filters.html
Explicit intents specify the component to start by name (the
fully-qualified class name). You'll typically use an explicit intent
to start a component in your own app, because you know the class name
of the activity or service you want to start. For example, start a new
activity in response to a user action or start a service to download a
file in the background.

Error with listview in dialog

I have been on this error now for a while. I am trying to open a list view in my dialog but its not opening for me this is my code.
LOGCAST
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mybasicapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Slashscreen"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".menu"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MENU" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".tutorialsone"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="com.mybasicapp.TUTORIALSONE" />
<category android:name="android.intent.category.DEFAULT" />
Java code:
public class menu extends Activity{
Button ImagebuttonOpenDialog;
String KEY_TEXTPSS = "TEXTPSS";
static final int CUSTOM_DIALOG_ID = 0;
ListView dialog_ListView;
String[] listContent = {
"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImagebuttonOpenDialog = (Button)findViewById(R.id.imgbtn1);
ImagebuttonOpenDialog.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0) {
showDialog(CUSTOM_DIALOG_ID);
}});
}
#Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch(id) {
case CUSTOM_DIALOG_ID:
dialog = new Dialog(menu.this);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Custom Dialog");
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setOnCancelListener(new OnCancelListener(){
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(menu.this,
"OnCancelListener",
Toast.LENGTH_LONG).show();
}});
dialog.setOnDismissListener(new OnDismissListener(){
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(menu.this,
"OnDismissListener",
Toast.LENGTH_LONG).show();
}});
//Prepare ListView in dialog
dialog_ListView = (ListView)dialog.findViewById(R.id.dialoglist);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listContent);
dialog_ListView.setAdapter(adapter);
dialog_ListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(menu.this,
parent.getItemAtPosition(position).toString() + " clicked",
Toast.LENGTH_LONG).show();
dismissDialog(CUSTOM_DIALOG_ID);
}});
break;
}
return dialog;
}
#Override
protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
// TODO Auto-generated method stub
super.onPrepareDialog(id, dialog, bundle);
switch(id) {
case CUSTOM_DIALOG_ID:
//
break;
}
}
}
activity_main xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/carbon" >
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="0dp"
android:background="#drawable/bottombar" >
</FrameLayout>
<ImageButton
android:id="#+id/imgbtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="42dp"
android:layout_marginTop="79dp"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imgbtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imgbtn1"
android:layout_marginRight="46dp"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imgbtn1"
android:layout_below="#+id/imgbtn1"
android:layout_marginTop="36dp"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imgbtn2"
android:layout_alignTop="#+id/imageButton3"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton3"
android:layout_below="#+id/imageButton3"
android:layout_marginTop="49dp"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton4"
android:layout_alignTop="#+id/imageButton5"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton5"
android:layout_below="#+id/imageButton5"
android:layout_marginTop="42dp"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton6"
android:layout_alignTop="#+id/imageButton7"
android:src="#android:drawable/btn_star" />
<ImageView
android:id="#+id/imageView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/kj" />
custom.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customdialog"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20dp"
android:minWidth="300dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"/>
<ListView
android:id="#+id/dialoglist"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/kj" />
</LinearLayout>
ALSO
I looked over the java code again and found that my code had (button) where in my XML had image button i have change my java code so you can see where i am at the moment..still no luck.I don't know how to copy log cast
From my count, buttonOpenDialog is null so you are getting NPE when you try to add a listener to it. Check your activity_main that you have a Button with an id of im1. You probably misspelled the id or that one simply doesn't exist in your layout. If you don't see a problem with that then post the activity_main.xml.
Also, this isn't your problem but you should consider using Java standard naming conventions. Class names should start with a capital letter. So you would have Main instead of main as your class name.
When you post a question please post the logcat in your question as it always makes it easier for members to read then needing to follow a link that sometimes isn't of the best quality.

Categories

Resources