Android Application Force Crash at main Activity Launch - java

I just started working on this application I feel like I've done everything right (This isn't my first application I've been working on) but when I try to run it It crashes?? It gives me no errors.
Here's the code from the main activity, then from manufest and then from the xml layout.
`
package com.theory.game;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class GameTheoryActivity extends Activity implements View.OnClickListener{
Button play, settings;
int i;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play.setOnClickListener(this);
settings.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bPlay:
Random irand = new Random();
i = irand.nextInt(4);
switch(i){
case 0:
Intent GoMillionair = new Intent(GameTheoryActivity.this, millionair.class);
startActivity(GoMillionair);
return;
case 1:
return;
case 2:
return;
case 3:
return;
}
return;
case R.id.bSettings:
return;
}
}
}
Manufest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.theory.game"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application >
<activity
android:name=".GameTheoryActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".millionair" >
<intent-filter>
<action android:name="com.theory.game.MILLIONAIR" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/backgroundimage"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="390dp"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/bPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.38"
android:background="#drawable/play" />
<Button
android:id="#+id/bSettings"
android:layout_width="match_parent"
android:layout_height="58dp"
android:layout_weight="0.36"
android:background="#drawable/settings" />
</LinearLayout>
</LinearLayout>
`
And I have another class called millionair which is just a normal class nothing much, nothing wrong there I guess. Please check whats up with this and let me know why my application wont start saying it "has stopped working".. Thanks

My guess is that you are getting a NullPointerException because both play and settings objects are null.
change your onCreate() to look like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play = (Button)findViewById(R.id.bPlay);
settings = (Button)findViewById(R.id.bSettings);
play.setOnClickListener(this);
settings.setOnClickListener(this);
}

You'll probably get a NullPointerException because you forgot to assign the buttons to your fields (Button play, settings;).
Here's an example on how to assign them:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play = (Button) findViewById(R.id.bPlay);
settings = (Button) findViewById(R.id.bSettings);
}

Looks like play and settings will both be null when you try to use then during the onCreate() method:
play.setOnClickListener(this);
settings.setOnClickListener(this);
make sure to initiate them like so:
play = (Button)findViewById(R.id.bPlay);
settings = (Button)findViewById(R.id.bSettings)
play.setOnClickListener(this);
settings.setOnClickListener(this);

Related

Splash screen alpha animation not working

So I have an app that currently has two Activities. A SplashScreenActivity and a MainActivity. The transition between the two activities works just fine but the problem lies in the SplashScreenActivity itself. In it, there is a single ImageView that is supposed to be initially invisible and then fade in. But instead the image remains invisible the entire time. Please help. Here's the code:
The SplashScreenActivity:
package com.degioncloud;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
ImageView logo = (ImageView) findViewById(R.id.iv_logo);
logo.setImageAlpha(0);
logo.animate().alpha(1f).setDuration(1200).withEndAction(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(i);
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
finish();
}
});
}
}
The XML of the SplashScreenActivity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".SplashScreenActivity">
<ImageView
android:id="#+id/iv_logo"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="#mipmap/ic_launcher"
android:layout_centerInParent="true" />
</RelativeLayout>
My Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.degioncloud">
<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/Theme.DegionCloud">
<activity android:name=".MainActivity" />
<activity android:name=".SplashScreenActivity"
android:theme="#style/SplashScreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The MainActivty is empty so I don't see a need to share that but if you need any other file then let me know and I will send a copy.
You need to call start() for the animation to begin:
logo.animate().alpha(1f).setDuration(1200).withEndAction(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(i);
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
finish();
}
}).start();

My app keeps force closing when using 2 intents

Why do my android apps keep closing when using 2 intent? when I delete one of them, it's work. especially if I delete the "Reg" Button in the java files.
Thanks
XML Files:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
tools:context=".LoginAct">
<TextView
android:id="#+id/daftar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/daftar"
android:textAlignment="center"
android:textColor="#color/Linktext"
android:clickable="true"
android:focusable="true"
android:layout_marginTop="10dp">
</TextView>
<Button
android:id="#+id/pass"
android:layout_width="wrap_content"
android:layout_height="23dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:background="#color/colorPrimaryDark"
android:clickable="true"
android:focusable="true"
android:text="#string/lewat"
android:textColor="#color/textwhite" />
Java Files (If I deleted the "Reg" Button from this file, the app is working, but otherwise it will force close after the splash screen):
package com.soerja.ngalamhistory;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class LoginAct extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_login);
Button Pass = (Button) findViewById(R.id.pass);
Button Reg = (Button) findViewById(R.id.daftar);
Pass.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(LoginAct.this, MainActivity.class);
startActivity(intent);
}
});
Reg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(LoginAct.this, RegisAct.class);
startActivity(intent);
}
});
}
}
AndroidManifest Files:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.soerja.ngalamhistory">
<application
android:allowBackup="true"
android:icon="#mipmap/logo"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SplashAct"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginAct"
android:label="#string/applogin"
android:theme="#style/LogTheme"></activity>
<activity
android:name=".MainActivity"
android:label="#string/home"
android:theme="#style/AppTheme"></activity>
<activity android:name=".RegisAct"
android:label="#string/appdaftar"
android:theme="#style/LogTheme"></activity>
</application>
</manifest>
I would really appreciate your help because this is my school project :)
You are casting a TextView into a Button. Probably the problem is a invalid cast exception.
In XML, you define the "daftar" as a TextView, but in java you are trying to use it as a Button.
Change it
Button Reg = (Button) findViewById(R.id.daftar);
To it
TextView Reg = (TextView) findViewById(R.id.daftar);
Or change your XML implamentation, defining "daftar" as a Button
here you are using your text view id
android:id="#+id/daftar"
as a reference to declaring button
Button Reg = (Button) findViewById(R.id.daftar);
so i think ,that may be the reason for crash
try this method :
add this in your text xml code:
android:onClick="daftar"
add for java class use this:
public void daftar(View v)
{
Intent intent = new Intent(this, RegisAct.class);
startActivity(intent);
}
and remove the old java code you used for this text view:
i hope this helps.

How to open URL links within my android application

I am using two buttons to open two url links. When i press button it goes to browser. But I want to open these url links within my application. Can somebody help me to solve my issue. My code as below :
Activity Main :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.vkumar.myapplication.MainActivity"
android:background="#d3a7a7">
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="GOOGLE"
android:onClick="browser1"
android:id="#+id/browser1"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:background="#android:color/holo_blue_dark" />
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="MAIL"
android:id="#+id/browser2"
android:onClick="browser2"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:background="#android:color/holo_blue_dark" />
</LinearLayout>
Main Activity :
package com.example.vkumar.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button browser1 = (Button) findViewById(R.id.browser1);
Button browser2 = (Button) findViewById(R.id.browser2);
Button browser3 = (Button) findViewById(R.id.browser3);
Button browser4 = (Button) findViewById(R.id.browser4);
Button browser5 = (Button) findViewById(R.id.browser5);
}
public void browser1 (View view) {
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.google.co.in/"));
startActivity(intent);
}
public void browser2 (View v) {
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.gmail.com/"));
startActivity(intent);
}
}
Android manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.vkumar.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<application android:allowBackup="true" android:icon="#mipmap/ic_launcher" android:label="MY APP" 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>
</application>
</manifest>
simple pass the url to webView on button click then show inside your application. see below example.
On Button link pass the url to webview activity
Intent intent = new Intent(context, WebViewActivity.class);
intent.putExtra("URL", "https://www.google.co.in/");
startActivity(intent);
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
String url = getIntent().getStringExtra("URL");
webView.loadUrl(url );
}
}

When I press back on my program i get a blank screen [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
After getting to the chapter3_1.xml page which pulls up fine when i press my phones back key and it takes me to the previous button. press back again and there's a blank page that isn't supposed to be there. press back again and you get to the original button. no errors no crashes.
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.th3ramr0d.ar670_1quickreference"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainMenu"
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=".SubMenu1"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.ar670_1quickreference.SUBMENU1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Chapter3"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.ar670_1quickreference.CHAPTER3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Chapter3_1"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.ar670_1quickreference.CHAPTER3_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
MainMenu.java
package com.th3ramr0d.ar670_1quickreference;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainMenu extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnChpt3 = (Button) findViewById(R.id.btnChpt3);
btnChpt3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.th3ramr0d.ar670_1quickreference.CHAPTER3"));
}
});
}
}
SubMenu1.java
package com.th3ramr0d.ar670_1quickreference;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SubMenu1 extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chapter3);
Button btnChpt3 = (Button) findViewById(R.id.btnChpt3_1);
btnChpt3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.th3ramr0d.ar670_1quickreference.CHAPTER3_1"));
}
});
}
}
Chapter3.java
package com.th3ramr0d.ar670_1quickreference;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Chapter3 extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
startActivity(new Intent("com.th3ramr0d.ar670_1quickreference.SUBMENU1"));
}
}
Chapter3_1.java
package com.th3ramr0d.ar670_1quickreference;
import android.app.Activity;
import android.os.Bundle;
public class Chapter3_1 extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chapter3_1);
}
}
activity_main.xml
<LinearLayout 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.th3ramr0d.ar670_1quickreference.MainActivity" >
<Button
android:id="#+id/btnChpt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chapter 3" />
</LinearLayout>
chapter3.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:orientation="vertical" >
<Button
android:id="#+id/btnChpt3_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fuckmylife" />
</LinearLayout>
chapter3_1.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:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the content of chapter 3_1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
I think you are using fragment in your application.
And when you pushing the fragment you'r are adding it to back stack thats why when you press back button it will show a blank screen which is the framelayout on which you are showing the fragment view.
Refer the link :-
Fragmnet manager
In your manifest, in activity you should set parent like this:
android:parentActivityName="com.some.myproject.ParentActivityToThisActivity"
But it can not working with API 14, API 15 should be used or newest.

NullPointerException while creating activity

this is my codes LoginActivity.java file
package com.example.crims;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
public class LoginActivity extends Activity {
TextView screen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
screen = (TextView) findViewById(R.id.link_to_register);
// Listening to register new account link
screen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(i);
}
});
}
}
this is my code for 'activity_login.xml' file
<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=".LoginActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>`
And Finally this is my menifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.crims"
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="com.example.crims.LoginActivity"
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=".RegisterActivity"
android:label="Register New Account">
</activity>
</application>
</manifest>
All of these files are here and i want to find my error as I m new to android app development...
You have NullPointerException(NPE) here:
10-01 21:02:07.580: E/AndroidRuntime(1299):
at com.example.crims.LoginActivity.onCreate(LoginActivity.java:17)
So, check line 17 of LoginActivity.java (you can just double click on this message in the logcat view and you will be navigated to this line).
It seems, line 17 is this:
registerScreen.setOnClickListener(new View.OnClickListener() {
As this is NPE, registerScreen is null. So, you should inspect why it is null. This is because Activity can't find it in line above and it returns null instead:
TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
This can be one of two possibilities: either you do not have widget with id 'link_to_register' in activity_login.xml, or something other is wrong :)
Check this please and show your activity_login.xml file, please.
Listen brother take this code. Change it according to you.
public class MainActivity extends Activity {
TextView screen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen = (TextView) findViewById(R.id.screen);
// Listening to register new account link
screen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), Register.class);
startActivity(i);
}
});
}
}
You need to register your activity in manifest.xml file too in case if you don't know about it.

Categories