Android Studio change activity crash - android

Activity crash on mobile, but on simulator works, whats wrong? Can anyone help me please?
Its a simple app that show some button than when clicked show a image.
6 Activity
Main Activity
package com.example.mobilejoy.kids1;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Transições de Cena
ImageButton toolbar2 = (ImageButton) findViewById(R.id.toolbarIcon2);
toolbar2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(myIntent);
finish();
}
});
ImageButton toolbar3 = (ImageButton) findViewById(R.id.toolbarIcon3);
toolbar3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity.this,ThirdActivity.class);
startActivity(myIntent);
finish();
}
});
ImageButton toolbar4 = (ImageButton) findViewById(R.id.toolbarIcon4);
toolbar4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity.this,FourthActivity.class);
startActivity(myIntent);
finish();
}
});
ImageButton toolbar5 = (ImageButton) findViewById(R.id.toolbarIcon5);
toolbar5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity.this,FifthActivity.class);
startActivity(myIntent);
finish();
}
});
ImageButton toolbar6 = (ImageButton) findViewById(R.id.toolbarIcon6);
toolbar6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity.this,SixthActivity.class);
startActivity(myIntent);
finish();
}
});
//Evento clicar botão executar audio
final MediaPlayer first121 = MediaPlayer.create(this, R.raw.first121);
ImageButton imageButton121 = (ImageButton) this.findViewById(R.id.imageButton121);
imageButton121.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
first121.start();
}
});
}
}
Second Activity
package com.example.mobilejoy.kids1;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
/**
* Created by mobilejoy on 9/13/16.
*/
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//transição entre cenas
ImageButton toolbar1 = (ImageButton) findViewById(R.id.toolbarIcon1);
toolbar1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(SecondActivity.this,MainActivity.class);
startActivity(myIntent);
finish();
}
});
ImageButton toolbar3 = (ImageButton) findViewById(R.id.toolbarIcon3);
toolbar3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(myIntent);
finish();
}
});
ImageButton toolbar4 = (ImageButton) findViewById(R.id.toolbarIcon4);
toolbar4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(SecondActivity.this,FourthActivity.class);
startActivity(myIntent);
finish();
}
});
ImageButton toolbar5 = (ImageButton) findViewById(R.id.toolbarIcon5);
toolbar5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(SecondActivity.this,FifthActivity.class);
startActivity(myIntent);
finish();
}
});
ImageButton toolbar6 = (ImageButton) findViewById(R.id.toolbarIcon6);
toolbar6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(SecondActivity.this,SixthActivity.class);
startActivity(myIntent);
finish();
}
});
}
}
and the XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mobilejoy.kids1">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.mobilejoy.kids1.SecondActivity"></activity>
<activity android:name="com.example.mobilejoy.kids1.ThirdActivity"></activity>
<activity android:name="com.example.mobilejoy.kids1.FourthActivity"></activity>
<activity android:name="com.example.mobilejoy.kids1.FifthActivity"></activity>
<activity android:name="com.example.mobilejoy.kids1.SixthActivity"></activity>
</application>
</manifest>
End.

Related

Reload Activity in Android Studio

Can you please help me about this game that I created, I want to reload the same activity, like restart game in quiz game. I hope you guys can help me, this is the code:
package com.example.splash;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.core.widget.ImageViewCompat;
import android.content.Intent;
import android.media.Image;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class stagenumbereasy1 extends AppCompatActivity {
Button congratsback,congratshome,congratsforward,reload;
ImageView congrats,tryagain;
Animation fromsmall,fromnothing;
MediaPlayer click,congrats1,die;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_stagenumbereasy1);
click = MediaPlayer.create(this, R.raw.click);
congrats1 = MediaPlayer.create(this, R.raw.congrats);
die = MediaPlayer.create(this, R.raw.die);
final Button butt8=(Button)findViewById(R.id.button8);
final Button butt9=(Button)findViewById(R.id.button9);
final Button butt10=(Button)findViewById(R.id.button10);
final Button butt11=(Button)findViewById(R.id.button11);
final Button back=(Button)findViewById(R.id.button12);
final LinearLayout overbox=(LinearLayout)findViewById(R.id.overbox);
final LinearLayout congratsbox=(LinearLayout)findViewById(R.id.congratsbox);
final LinearLayout tryagainbox = (LinearLayout)findViewById(R.id.tryagainbox);
final LinearLayout trybox=(LinearLayout)findViewById(R.id.trybox);
fromsmall = AnimationUtils.loadAnimation(this, R.anim.fromsmall);
fromnothing = AnimationUtils.loadAnimation(this, R.anim.fromnothing);
congratsbox.setAlpha(0);
overbox.setAlpha(0);
tryagainbox.setAlpha(0);
trybox.setAlpha(0);
congratsback=(Button)findViewById(R.id.congratsback);
congratshome=(Button)findViewById(R.id.congratshome);
congratsforward=(Button)findViewById(R.id.congratsforward);
congrats=(ImageView)findViewById(R.id.congrats);
reload=(Button)findViewById(R.id.reload);
tryagain=(ImageView)findViewById(R.id.try1);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent back=new Intent (stagenumbereasy1.this,stagenumber.class);
click.start();
startActivity(back);
}
});
butt8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
click.start();
congrats1.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
overbox.setAlpha(1);
overbox.startAnimation(fromnothing);
congratsbox.setAlpha(1);
congratsbox.startAnimation(fromsmall);
}
});
congratshome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent home = new Intent(stagenumbereasy1.this, Home.class);
click.start();
startActivity(home);
}
});
congratsforward.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent easy2 = new Intent(stagenumbereasy1.this, stagenumbereasy2.class);
click.start();
startActivity(easy2);
}
});
butt9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
click.start();
die.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
trybox.setAlpha(1);
trybox.startAnimation(fromnothing);
tryagainbox.setAlpha(1);
tryagainbox.startAnimation(fromsmall);
reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reload = new Intent(stagenumbereasy1.this, stagenumbereasy1reload.class);
click.start();
startActivity(reload);
finish();
}
});
}
});
butt10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
click.start();
die.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
trybox.setAlpha(1);
trybox.startAnimation(fromnothing);
tryagainbox.setAlpha(1);
tryagainbox.startAnimation(fromsmall);
reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reload = new Intent(stagenumbereasy1.this, stagenumbereasy1reload.class);
click.start();
startActivity(reload);
finish();
}
});
}
});
butt11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
click.start();
die.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
trybox.setAlpha(1);
trybox.startAnimation(fromnothing);
tryagainbox.setAlpha(1);
tryagainbox.startAnimation(fromsmall);
reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent refresh = new Intent(stagenumbereasy1.this, stagenumbereasy1reload.class);
click.start();
startActivity(refresh);
finish();
}
});
}
});
}
}
Image of my game
I actually create another activity to make it happens but when I click the reload button it is going back to home. This is my thesis game.
I don't know if exists a recommended answer in Android docs. But, I think you can call startActivity() and finish(), like:
// Java
startActivity(new Intent(MyActivity.class));
finish();
// Kotlin
startActivity(Intent(MyActivity::java.class));
finish();
This will create a new instance of the activity and destroy the current.
For better performance, you should make tests with Fragments instead resetting an Activity.
You could try recreate(); which would recreate the activity
You could also try Intent intent = new Intent(MyActivity.this, MyActivity.class);
startActivity(intent);
Just simply use intent flags one is INTENT_FLAGS_NEW_TASK
it restart activity
Intent.setflags()
In your current activity recall it
Intent(main.this,main.class)

Connecting Two buttons in one activity to two other activities

I'm trying to make a simple math game with two modes, addition and subtraction. I figured out how to create a button that will link the "Addition button" to the addition activity but I can't seem to figure out how to create a second "Subtraction Button" that will link to the subtraction activity. Here's my broken code:
package com.example.kirky_000.madmath;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.content.Intent;
public class MainMenu extends ActionBarActivity {
Button button;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
Your code just have some syntax error which is solved as per given code...
package com.example.kirky_000.madmath;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.content.Intent;
public class MainMenu extends ActionBarActivity {
Button button;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
}
Your second instruction to add listener is inside the first OnClickListener.
So the listener is never added to the second button. You code should be like this :
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
});
}
You need to have different click listeners for different buttons. Right now, you're putting the click listener for the second button inside the click listener for the first button. Separate them like this..
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
)};
Some points before the solution (those will help you for further coding).
Declare context after buttons declaration : So after Button
button, button2, write :
final Context context;
Always keep in mind : Always initialize objects in onCreate()
method. So in OnCreate() after
setContentView(R.layout.activity_main_menu);, write :
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
//then define context
context = MainMenu.this;
//or context = getApplicationContext();
Now addListenerOnButton() function will be like this (just replace
addListenerOnButton() with following code):
public void addListenerOnButton(){
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
Now what is the wong in your code ?
Your onClick listener code is wrong...
enjoy the coding :) (and android :))...
you can even do like this
public void mainClickHandler(View v)
{
switch (v.getId()) {
case R.id.button:
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
case R.id.button2:
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
}

How to make two button to open 2 layout

package com.example.luke.sinhalasindu;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class HomePage extends Activity implements OnClickListener {
Button bntoartistpage;
Button bntonewmp3page;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
bntoartistpage = (Button) findViewById(R.id.bntoartistpage);
bntoartistpage.setOnClickListener(this);
bntonewmp3page = (Button) findViewById(R.id.bntonewmp3page);
bntonewmp3page.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Intent inent = new Intent(this, Artist.class);
// calling an activity using <intent-filter> action name
// Intent inent = new Intent("com.example.luke.sinhalasindu");
startActivity(inent); }
#Override
public void onClick(View view){
Intent inent = new Intent(this, NewMp3.class);
// calling an activity using <intent-filter> action name
// Intent inent = new Intent("com.example.luke.sinhalasindu");
startActivity(inent); }
}
Button bntoartistpage = (Button) findViewById(R.id.bntoartistpage);
bntoartistpage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent inent = new Intent(this, Artist.class);
// calling an activity using <intent-filter> action name
// Intent inent = new Intent("com.example.luke.sinhalasindu");
startActivity(inent);
}
});
Button bntonewmp3page = (Button) findViewById(R.id.bntonewmp3page);
bntonewmp3page.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent inent = new Intent(this, NewMp3.class);
// calling an activity using <intent-filter> action name
// Intent inent = new Intent("com.example.luke.sinhalasindu");
startActivity(inent);
}
});
You can't use two onClick() methods in one Activity. Use this
#Override
public void onClick(View view) {
if(view.getId() == R.id.bntoartistpage)
{
Intent inent = new Intent(this, Artist.class);
// calling an activity using <intent-filter> action name
// Intent inent = new Intent("com.example.luke.sinhalasindu");
startActivity(inent);
}
else if(view.getId() == R.id.bntonewmp3page)
{
Intent inent = new Intent(this, NewMp3.class);
// calling an activity using <intent-filter> action name
// Intent inent = new Intent("com.example.luke.sinhalasindu");
startActivity(inent);
}
}

How do I make multiple buttons that have links?

I have made all the buttons and all the links work just fine but the problem I am having is that i have to start from the top button and select it before proceeding to the subsequent buttons for each of those to work. In other words none one of the buttons work until I select each one in order. I'm sure I'm missing something easy here just don't know much at all about what I'm doing here. Any help appreciated, thanks!
package rainbow.cheetah.go.sms;
import android.app.Activity;
import android.os.Bundle;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class RainbowCheetahGOSMSActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.More);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://play.google.com/store/search?q=stealthychief&c=apps"));
startActivity(myWebLink);
Button btn = (Button) findViewById(R.id.match);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://play.google.com/store/search?q=rainbow+cheetah+stealthychief&c=apps"));
startActivity(myWebLink);
Button btn = (Button) findViewById(R.id.rate);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://play.google.com/store/apps/details?id=rainbow.cheetah.go.sms&feature=search_result#?t=W251bGwsMSwxLDEsInJhaW5ib3cuY2hlZXRhaC5nby5zbXMiXQ.."));
startActivity(myWebLink);
Button btn = (Button) findViewById(R.id.twitter);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://twitter.com/#!/Stealthychief"));
startActivity(myWebLink);
Button btn = (Button) findViewById(R.id.google);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://plus.google.com/u/0/105194414710791941012/posts"));
startActivity(myWebLink); }
});
}
});
}
});
}
});
}});
}};
This is because you completely messed up your parentheses. I think you meant something like this:
public class RainbowCheetahGOSMSActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.More);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(
android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri
.parse("https://play.google.com/store/search?q=stealthychief&c=apps"));
startActivity(myWebLink);
}
});
Button btn2 = (Button) findViewById(R.id.match);
btn2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://play.google.com/store/search?q=rainbow+cheetah+stealthychief&c=apps"));
startActivity(myWebLink);
}
});
Button btn3 = (Button) findViewById(R.id.rate);
btn3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://play.google.com/store/apps/details?id=rainbow.cheetah.go.sms&feature=search_result#?t=W251bGwsMSwxLDEsInJhaW5ib3cuY2hlZXRhaC5nby5zbXMiXQ.."));
startActivity(myWebLink);
}
});
Button btn4 = (Button) findViewById(R.id.twitter);
btn4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://twitter.com/#!/Stealthychief"));
startActivity(myWebLink);
}
});
Button btn5 = (Button) findViewById(R.id.google);
btn5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://plus.google.com/u/0/105194414710791941012/posts"));
startActivity(myWebLink);
}
});
}
}

unknown Exception android

This is my main file, which has image buttons, and it gives me an exception. When I click on courses image button, it just closes the application.
It works for rest of buttons (rest activities just consist a text view and button) whereas in courses view, I added 3 more buttons (before I added these 3 buttons, it used to work for switching between main and courses).
What i am trying to do is this:
Make 3 buttons in courses activity. They will navigate me to other views like "higher education" or "further education". In next views I will create a list which will display name of courses (like "CCNA" or "Health diploma"!). When the user clicks on course, it will display a picture and some text information about the course.
package com.NVT.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class Main extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageButton CoursesButton = (ImageButton)findViewById(R.id.CoursesButton);
ImageButton ILoveNescotButton = (ImageButton)findViewById(R.id.ILoveNescotButton);
ImageButton CampusMapButton = (ImageButton)findViewById(R.id.CampusMapButton);
ImageButton GettingHereButton = (ImageButton)findViewById(R.id.GettingHereButton);
CoursesButton.setOnClickListener(new ImageButton.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), Courses.class);
startActivityForResult(myIntent, 0);
}
});
ILoveNescotButton.setOnClickListener(new ImageButton.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), ILoveNescot.class);
startActivityForResult(myIntent, 0);
}
});
CampusMapButton.setOnClickListener(new ImageButton.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), CampusMap.class);
startActivityForResult(myIntent, 0);
}
});
GettingHereButton.setOnClickListener(new ImageButton.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), GettingHere.class);
startActivityForResult(myIntent, 0);
}
});
}
}
Code for Courses View "Courses.java" is given below,,,,
package com.NVT.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Courses extends Activity
{
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.courses);
Button ButtonFurtherEducation = (Button)findViewById(R.id.Button_Courses_FurtherEducation);
Button ButtonHigherEducation = (Button)findViewById(R.id.Button_Courses_HigherEducation);
Button ButtonEmployernTraining = (Button)findViewById(R.id.Button_Courses_EmployersnTraining);
Button next = (Button) findViewById(R.id.Button01);
ButtonFurtherEducation.setOnClickListener(new Button.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), FurtherEducationCourses.class);
startActivityForResult(myIntent, 0);
}
});
ButtonHigherEducation.setOnClickListener(new Button.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), HigherEducationCourses.class);
startActivityForResult(myIntent, 0);
}
});
ButtonEmployernTraining.setOnClickListener(new Button.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), EmployersTrainingCourses.class);
startActivityForResult(myIntent, 0);
}
});
next.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
and code for the further Education Activity is given below,,,,
package com.NVT.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class FurtherEducationCourses extends Activity
{
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.further_education);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
my Manifest coding as below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.NVT.android"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Main"
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=".Courses">
</activity>
<activity android:name=".CampusMap">
</activity>
<activity android:name=".GettingHere">
</activity>
<activity android:name=".ILoveNescot">
</activity>
<activity android:name=".FurtherEducationCourses">
</activity>
<activity android:name=".HigherEducationCourses">
</activity>
<activity android:name=".EmployersTrainingCourses">
</activity>
</application>
<uses-sdk android:minSdkVersion="9" />
</manifest>
You will see in the logcat screen that the first line with class name from your application is 'Courses.java'. So, the error occurs there. In the logcat you will see NullPointerException at Courses.java line 63. In line 63 of Courses.java you have set the onclick listener for the next button. So, I think the next button stay null when this line executes. I think the main error is in line no 21
Button next = (Button) findViewById(R.id.Button01);
I think the id of the next button in the layout file (corses.xml) is not Button01. Please fix this thing then there will be no errors.
Just try it. :)
According to the logcat screenshot, your problem is that Button next is null, since it is a NullPointerException in line 63, which is when you call next.setOnClickListener, which means that R.id.Button01 is not in R.layout.courses, could you verify if this information is correct?

Categories

Resources