Here's the code of my main activty:
package com.manparvesh.Soil;
import com.manparvesh.soilclassification.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class myMainScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.classificationsystems);
Button aashtob = (Button) findViewById(R.id.aashto);
aashtob.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v1){
startActivity(new Intent(getApplicationContext(),AASHTO_Open.class));
}
});
Button uscsb = (Button) findViewById(R.id.uscs);
uscsb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v2) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(),USCS_Open.class));
}
});
Button usdab = (Button) findViewById(R.id.usda);
usdab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v3) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(),USDA_Open.class));
}
});
}
}
Here's the code of the class that I am trying to open:
package com.manparvesh.Soil;
import com.manparvesh.soilclassification.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.*;
public class AASHTO_Open extends Activity {
Button classify1,clear1,menu1;
EditText S10,S40,S200,ll,pi;
TextView CC,CdCd;
int c;
String C,Cd;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.aashto);
classify1=(Button)findViewById(R.id.classify1);
clear1=(Button)findViewById(R.id.clear1);
menu1=(Button)findViewById(R.id.menu1);
S10 = (EditText)findViewById(R.id.T11);
S40 = (EditText)findViewById(R.id.T12);
S200 = (EditText)findViewById(R.id.T13);
ll = (EditText)findViewById(R.id.T14);
pi = (EditText)findViewById(R.id.T15);
int s10 = Integer.parseInt(S10.getText().toString());
int s40 = Integer.parseInt(S40.getText().toString());
int s200 = Integer.parseInt(S200.getText().toString());
int LL = Integer.parseInt(ll.getText().toString());
int PI = Integer.parseInt(pi.getText().toString());
if (s10<=50){
if (s40<=30){
if (s200<=15 && PI<=6){
c=1;
}
}else if(s40<=50){
if (s200<=25){
c=2;
}
}else{
if (s200<=10){
c=3;
}else if (s200<=35){
if (LL<=40){
if (PI<=10){
c=4;
}else{
c=5;
}
}else{
if (PI<=10){
c=6;
}else{
c=7;
}
}
}else{
if (LL<=40){
if (PI<=10){
c=8;
}else{
c=9;
}
}else{
if (PI<=10){
c=10;
}else{
c=11;
}
}
}
}
}
if (("".equals(S10))|| ("".equals(S200)) || ("".equals(S40)) || ("".equals(LL)) || ("".equals(PI))){
throw new NumberFormatException();
}
switch(c){
case 1:
C="A-1-a";
Cd="stone fragments, gravel and sand";
break;
case 2:
C="A-1-b";
Cd="stone fragments, gravel and sand";
break;
case 3:
C="A-3";
Cd="fine sand";
break;
case 4:
C="A-2-4";
Cd="silty or clayey gravel and sand";
break;
case 5:
C="A-2-6";
Cd="silty or clayey gravel and sand";
break;
case 6:
C="A-2-5";
Cd="silty or clayey gravel and sand";
break;
case 7:
C="A-2-7";
Cd="silty or clayey gravel and sand";
break;
case 8:
C="A-4";
Cd="silty soils";
break;
case 9:
C="A-6";
Cd="clayey soils";
break;
case 10:
C="A-5";
Cd="silty soils";
break;
case 11:
C="A-7";
Cd="clayey soils";
break;
}
classify1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v1) {
CC.setText(C);
CdCd.setText(Cd);
}
});
menu1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v1) {
startActivity(new Intent("com.tutorial.CLEARSCREEN"));
}
});
clear1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v1) {
S10.setText("");
S40.setText("");
S200.setText("");
ll.setText("");
pi.setText("");
CC.setText("");
CdCd.setText("");
}
});
}
}`
The XML file of the layout I'm trying to open:
<?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="#drawable/bg3"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="105dp"
android:textColor="#FFFFFF"
android:text="AASHTO"
android:textSize="55dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_marginTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:textColor="#FFFFFF"
android:id="#+id/textView2"
android:layout_width="159dp"
android:layout_height="wrap_content"
android:text="Sieve no.10 (% passing)" />
<EditText
android:inputType="phone"
android:id="#+id/T11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:background="#99FFFFFF">
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_marginTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:textColor="#FFFFFF"
android:id="#+id/textView4"
android:layout_width="159dp"
android:layout_height="wrap_content"
android:text="Sieve no.40 (% passing)" />
<EditText
android:inputType="phone"
android:background="#99FFFFFF"
android:id="#+id/T12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:textColor="#FFFFFF"
android:id="#+id/aegg"
android:layout_width="159dp"
android:layout_height="wrap_content"
android:text="Sieve no.200 (% passing)" />
<EditText
android:inputType="phone"
android:background="#99FFFFFF"
android:id="#+id/T13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:textColor="#FFFFFF"
android:id="#+id/textView6"
android:layout_width="159dp"
android:layout_height="wrap_content"
android:text="Liquid Limit" />
<EditText
android:inputType="phone"
android:background="#99FFFFFF"
android:id="#+id/T14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:textColor="#FFFFFF"
android:id="#+id/textView3"
android:layout_width="159dp"
android:layout_height="wrap_content"
android:text="Plasticity Index (=LL-PL)" />
<EditText
android:inputType="phone"
android:background="#99FFFFFF"
android:id="#+id/T15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="5dp">
<Button
android:textColor="#FFFFFF"
android:id="#+id/menu1"
android:layout_width="105dp"
android:layout_height="wrap_content"
android:text="Menu"
android:gravity="center"
android:layout_marginRight="0.75dp"
android:background="#drawable/button_wood"
/>
<Button
android:textColor="#FFFFFF"
android:id="#+id/clear1"
android:layout_width="105dp"
android:layout_height="wrap_content"
android:text="Clear"
android:layout_marginRight="0.75dp"
android:gravity="center"
android:background="#drawable/button_wood"
/>
<Button
android:textColor="#FFFFFF"
android:id="#+id/classify1"
android:layout_width="105dp"
android:layout_height="wrap_content"
android:text="Classify"
android:gravity="center"
android:background="#drawable/button_wood"
/>
</LinearLayout>
</LinearLayout>
The Manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.manparvesh.soilclassification"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19"
android:maxSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.manparvesh.Soil.MainActivity"
android:label="#layout/splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.manparvesh.Soil.myMainScreen" android:screenOrientation="portrait" android:theme="#style/AppTheme" android:label="#layout/classificationsystems"></activity>
<activity android:name="com.manparvesh.Soil.AASHTO_Open" android:theme="#style/AppTheme" android:description="#layout/aashto"></activity>
<activity android:name="com.manparvesh.Soil.USCS_Open" android:label="#layout/uscs"></activity>
<activity android:name="com.manparvesh.Soil.USDA_Open" android:theme="#style/AppTheme" android:label="#layout/usda"></activity>
</application>
</manifest>
Here's what the LogCat shows:
07-09 18:27:15.803: E/AndroidRuntime(686): FATAL EXCEPTION: main
07-09 18:27:15.803: E/AndroidRuntime(686): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.manparvesh.soilclassification/com.manparvesh.Soil.USCS_Open}: java.lang.NumberFormatException: Invalid double: ""
07-09 18:27:15.803: E/AndroidRuntime(686): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-09 18:27:15.803: E/AndroidRuntime(686): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-09 18:27:15.803: E/AndroidRuntime(686): at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-09 18:27:15.803: E/AndroidRuntime(686): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-09 18:27:15.803: E/AndroidRuntime(686): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 18:27:15.803: E/AndroidRuntime(686): at android.os.Looper.loop(Looper.java:137)
07-09 18:27:15.803: E/AndroidRuntime(686): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-09 18:27:15.803: E/AndroidRuntime(686): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 18:27:15.803: E/AndroidRuntime(686): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 18:27:15.803: E/AndroidRuntime(686): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-09 18:27:15.803: E/AndroidRuntime(686): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-09 18:27:15.803: E/AndroidRuntime(686): at dalvik.system.NativeStart.main(Native Method)
07-09 18:27:15.803: E/AndroidRuntime(686): Caused by: java.lang.NumberFormatException: Invalid double: ""
07-09 18:27:15.803: E/AndroidRuntime(686): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
07-09 18:27:15.803: E/AndroidRuntime(686): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
07-09 18:27:15.803: E/AndroidRuntime(686): at java.lang.Double.parseDouble(Double.java:295)
07-09 18:27:15.803: E/AndroidRuntime(686): at com.manparvesh.Soil.USCS_Open.onCreate(USCS_Open.java:42)
07-09 18:27:15.803: E/AndroidRuntime(686): at android.app.Activity.performCreate(Activity.java:5008)
07-09 18:27:15.803: E/AndroidRuntime(686): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-09 18:27:15.803: E/AndroidRuntime(686): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-09 18:27:15.803: E/AndroidRuntime(686): ... 11 more
I am not able to find the problem here.
Check your following code -
int s10 = Integer.parseInt(S10.getText().toString());
int s40 = Integer.parseInt(S40.getText().toString());
int s200 = Integer.parseInt(S200.getText().toString());
int LL = Integer.parseInt(ll.getText().toString());
int PI = Integer.parseInt(pi.getText().toString());
Exception is thrown because one of the field value (out of s10, s40, s200, LL, PI) is blank. And you are doing Integer.parseInt(""); on that field value.
NumberFormatException
if string cannot be parsed as an integer value.
Solution 1-
Put the block inside try-catch -
int s10 = 0, s40 = 0, s200 = 0, LL = 0, PI = 0;
try{
s10 = Integer.parseInt(S10.getText().toString());
s40 = Integer.parseInt(S40.getText().toString());
s200 = Integer.parseInt(S200.getText().toString());
LL = Integer.parseInt(ll.getText().toString());
PI = Integer.parseInt(pi.getText().toString());
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
Solution 2-
Put this if block just after EditText findView declared and then do something with
your blank values.
if (("".equals(S10.getText().toString()))||
("".equals(S200.getText().toString())) ||
("".equals(S40.getText().toString())) ||
("".equals(LL.getText().toString())) ||
("".equals(PI.getText().toString())))
{
//EditText field value is blank Do something
}
You can assign a valid integer value for blank field so that it will not give NumberFormatException. This is recommended solution.
You are getting a NumberFormatException when you try to do
int s10 = Integer.parseInt(S10.getText().toString());
S10 is currently empty as you haven't typed while it is being created so you try to do
Integer.parseInt("");
and as a result you get NumberFormatException
Try this...
package com.manparvesh.Soil;
import com.manparvesh.soilclassification.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class myMainScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.classificationsystems);
Button aashtob = (Button) findViewById(R.id.aashto);
aashtob.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v1){
Intent i = new Intent(myMainScreen.this,AASHTO_Open.class);
startActivity(i);
}
});
Button uscsb = (Button) findViewById(R.id.uscs);
uscsb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v2) {
// TODO Auto-generated method stub
Intent i1 = new Intent(myMainScreen.this,USCS_Open.class);
startActivity(i1);
}
});
Button usdab = (Button) findViewById(R.id.usda);
usdab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v3) {
// TODO Auto-generated method stub
Intent i2 = new Intent(myMainScreen.this,USDA_Open.class);
startActivity(i2);
}
});
}
}
You need to pass the context of your Activity whenever starting a new Activity. Passing applicationContext() will increase the scope and may cause memory leaks in the future. So in order to avoid that change this :
startActivity(new Intent(myMainScreen.this,AASHTO_Open.class));
This will reduce the scope to your Activity.
You are getting a NumberFormatException because when you are trying to read from your TextView it's empty but you are expecting a double value. So you must always enforce a check for that and avoid crashes. You must do something like this :
if(S10.getText().toString() != null && S10.getText().toString().trim().length() > 0){
int s10 = Integer.parseInt(S10.getText().toString());
}
Also have a look at the Java Coding Standards before proceeding to code.
Java Coding Style
Related
I have a problem in this app, it show me "Unfortunately, app has stopped"
MainActivity.class
package ahmedchtn.stockmanager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText username;
EditText password;
Button loginbutton;
Button bpasschange;
int counter = 3;
String username_string ;
String password1 = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText) findViewById(R.id.identifer);
password = (EditText) findViewById(R.id.password);
loginbutton = (Button) findViewById(R.id.bLogin);
bpasschange = (Button) findViewById(R.id.bCh);
username_string = "admin";
if (password1 == "") {
password1 = "admin";
} else {
Intent intent4 = getIntent();
password1 = intent4.getStringExtra("oldpassw");
}
bpasschange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intentoldpass = new Intent(MainActivity.this, PasswordChange.class);
intentoldpass.putExtra("oldpassw", password1);
Intent intentpasschan = new Intent(getApplicationContext(), PasswordChange.class);
startActivity(intentpasschan);
}
});
loginbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (username.getText().toString().equals(username_string) &&
password.getText().toString().equals(password1)) {
Toast.makeText(getApplicationContext(),
"Redirecting...", Toast.LENGTH_SHORT).show();
Intent iop = new Intent(getApplicationContext(), ManagementPage.class);
startActivity(iop);
} else {
Toast.makeText(getApplicationContext(),
"Wrong Entries", Toast.LENGTH_SHORT).show();
counter--;
if (counter == 0) {
loginbutton.setEnabled(false);
}
}
}
}
);
}
}
PasswordChange.class
package ahmedchtn.stockmanager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class PasswordChange extends AppCompatActivity {
Button bchange;
EditText oldpassword;
EditText newpassword;
String oldpass;
String newpass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password_change);
bchange = (Button) findViewById(R.id.bChange2);
oldpassword = (EditText) findViewById(R.id.old_password);
newpassword = (EditText) findViewById(R.id.new_password);
bchange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent ioldpass = getIntent();
oldpass = ioldpass.getStringExtra("oldpassw");
if (oldpass == (oldpassword.getText().toString())) {
newpass = newpassword.getText().toString();
Intent intentnew = new Intent(PasswordChange.this, MainActivity.class);
intentnew.putExtra("passnew", newpass);
startActivity(intentnew);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
}
});
}
}
I think that the problem is with the intents..
Thanks in advance for your help !
Is there any other solutions
The logcat
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at ahmedchtn.stockmanager.MainActivity$2.onClick(MainActivity.java:46)
at android.view.View.performClick(View.java:4439)
at android.widget.Button.performClick(Button.java:139)
at android.view.View$PerformClick.run(View.java:18395)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Application terminated.
Xml Codes
activity_main.xml
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="ahmedchtn.stockmanager.MainActivity">
<EditText
android:id="#+id/identifer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:hint="#string/enter_your_id" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:hint="#string/enter_your_password"
android:inputType="textPassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/bLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#string/login" />
<Button
android:id="#+id/bCh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="changer passe" />
</LinearLayout>
</LinearLayout>
activity_password_change.xml
<?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:id="#+id/activity_password_change"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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="ahmedchtn.stockmanager.PasswordChange">
<EditText
android:id="#+id/old_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:hint="#string/enter_your_old_password" />
<EditText
android:id="#+id/new_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:hint="#string/enter_your_new_password" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/activity_vertical_margin"
android:weightSum="1">
<Button
android:id="#+id/bChange2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:layout_weight="0.5"
android:text="#string/confirm" />
<Button
android:id="#+id/main_page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:layout_weight="0.5"
android:text="#string/main_page" />
</LinearLayout>
</LinearLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ahmedchtn.stockmanager">
<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=".ManagementPage"
android:parentActivityName=".MainActivity" />
<activity android:name=".ProductsSearch" />
<activity android:name=".ProductsCommand" />
<activity
android:name=".Editor"
android:parentActivityName=".ProductsList" />
<activity android:name=".ProductsList" />
<provider
android:name=".data.ProductProvider"
android:authorities="ahmedchtn.stockmanager"
android:exported="false" />
<activity android:name=".PasswordChange"></activity>
</application>
</manifest>
I have updated my code
From the information you've provided so far (NPE on line 28 of PasswordChange.class), it looks like this line is causing the issue:
bchange = (Button) findViewById(R.id.bChange);
bchange.setOnClickListener(new View.OnClickListener() {
...
You have a button named bChange in both of your layout xml files - change one of them into something unique. I highly recommend that all components should be uniquely named through the app, as that stops the possibility of an activity accidentally referencing the wrong resource.
Example:
activity_password_change.xml
...
<Button
android:id="#+id/bPasswordChange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:layout_weight="0.5"
android:text="#string/confirm" />
...
PasswordChange.java
...
bchange = (Button) findViewById(R.id.bPasswordChange);
...
For the second issue that's arisen in MainActivity.java:
Intent intentbool = getIntent();
bmain = intentbool.getBooleanExtra("bool", bmain);
However, your problem here lies with line 20:
Boolean bmain;
Because you've declared a Boolean object rather than a boolean primitive (note the capital B), it is null by default as you've not initialised it. Change it to a primitive:
boolean bmain;
Make sure you have defined PasswordChange.class in your Manifest.xml file.
An example would be <activity android:name=".PasswordChange"/>. That's the most common reason for an app to crash when going to a new activity.
I am new to android. Here am trying to pass the radiobutton and Checkbox values from one activity to another activity. Its not showing any error but when I run it simply gets closed. Any help would be appreciated.Here I have posted the complete code..
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
public class MainActivity extends Activity {
Intent obj = new Intent(this, SecondActivity.class);
Bundle extras = new Bundle();
RadioButton male, female, single, married;
CheckBox twelveth, bE, mE;
String s1="+2";
String s2="B.E.";
String s3="M.E.";
StringBuilder s;
String str;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
male = (RadioButton) findViewById(R.id.male);
female = (RadioButton) findViewById(R.id.female);
single = (RadioButton) findViewById(R.id.single);
married = (RadioButton) findViewById(R.id.married);
twelveth = (CheckBox) findViewById(R.id.twelveth);
bE = (CheckBox) findViewById(R.id.BE);
mE = (CheckBox) findViewById(R.id.ME);
str=s.toString();
extras.putString("Education",str);
}
public void onGenderClicked(View v) {
boolean checked = ((RadioButton) v).isChecked();
switch (v.getId()) {
case R.id.male:
if (checked)
extras.putString("Gender", "Male");
break;
case R.id.female:
if (checked)
extras.putString("Gender", "Female");
break;
}
}
public void onStatusClicked(View v) {
boolean checked = ((RadioButton) v).isChecked();
switch (v.getId()) {
case R.id.single:
if (checked)
extras.putString("Status", "Single");
break;
case R.id.married:
if (checked)
extras.putString("Status", "Married");
break;
}
}
public void ButtonClick(View v) {
obj.putExtras(extras);
startActivity(obj);
}
public void onCheckBoxClicked(View v)
{
boolean checked=((CheckBox)v).isChecked();
switch(v.getId())
{
case R.id.twelveth:
if(checked)
{
s.append("\n"+s1);
}
break;
case R.id.BE:
if(checked)
{
s.append("\n"+s2);
}
break;
case R.id.ME:
if(checked)
{
s.append("\n"+s3);
}
break;
}
str=s.toString();
extras.putString("Education",str);
}
}
SecondActivity
package com.example.aravindpraveen.buttonsandboxes;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity {
TextView tvGender,tvStatus,tvEducation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
tvGender=(TextView)findViewById(R.id.tvGender);
tvStatus=(TextView)findViewById(R.id.tvStatus);
tvEducation=(TextView)findViewById(R.id.tvEducation);
Intent obj1=getIntent();
Bundle extras=obj1.getExtras();
String gender=extras.getString("Gender");
String status=extras.getString("Starus");
String education=extras.getString("Education");
tvGender.setText(gender);
tvStatus.setText(status);
tvEducation.setText(education);
}
}
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aravindpraveen.buttonsandboxes" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
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"
tools:context=".MainActivity"
android:background="#6457d962"
android:orientation="vertical">
<TextView android:text="#string/b_b"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="40sp"
android:background="#f62dd93c"
android:textColor="#c34c27e6"
android:gravity="center"/>
<Space
android:layout_width="match_parent"
android:layout_height="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/gender"
android:textSize="40sp"
android:gravity="center"
android:textColor="#000000"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rg_gender">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/male"
android:text="#string/male"
android:textSize="30sp"
android:checked="true"
android:onClick="onGenderClicked"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/female"
android:text="#string/female"
android:textSize="30sp"
android:onClick="onGenderClicked"/>
</RadioGroup>
<Space
android:layout_width="match_parent"
android:layout_height="15dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="#string/M_status"
android:textColor="#000000"
android:gravity="center"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rg_status">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/single"
android:text="#string/single"
android:textSize="30sp"
android:checked="true"
android:onClick="onStatusClicked"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/married"
android:text="#string/married"
android:textSize="30sp"
android:onClick="onStatusClicked"/>
</RadioGroup>
<Space
android:layout_width="match_parent"
android:layout_height="15dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="#string/education"
android:textColor="#000000"
android:gravity="center"
/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/twelveth"
android:textSize="30sp"
android:id="#+id/twelveth"
android:onClick="onCheckBoxClicked"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/bE"
android:textSize="30sp"
android:id="#+id/BE"
android:onClick="onCheckBoxClicked"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/mE"
android:textSize="30sp"
android:id="#+id/ME"
android:onClick="onCheckBoxClicked"/>
<Button
android:layout_width="wrap_content"
android:layout_height="50dp"
android:id="#+id/submit"
android:text="#string/submit"
android:layout_gravity="center"
android:onClick="ButtonClick"/>
</LinearLayout>
second.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="#f4dbde"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:background="#fba1a1"
android:text="#string/heading"
android:gravity="center"
android:textColor="#ffffff"/>
<Space
android:layout_width="match_parent"
android:layout_height="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/gender"
android:textSize="40sp"
android:gravity="center"
android:textColor="#ffffff"
android:background="#f7beae"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:paddingTop="20dp"
android:gravity="center"
android:id="#+id/tvGender"/>
<Space
android:layout_width="match_parent"
android:layout_height="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/M_status"
android:textSize="40sp"
android:gravity="center"
android:textColor="#ffffff"
android:background="#f7beae"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:paddingTop="20dp"
android:gravity="center"
android:id="#+id/tvStatus"/>
<Space
android:layout_width="match_parent"
android:layout_height="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/education"
android:textSize="40sp"
android:gravity="center"
android:textColor="#ffffff"
android:background="#f7beae"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:paddingTop="20dp"
android:gravity="center"
android:id="#+id/tvEducation"/>
</LinearLayout>
LogCat
02-03 11:39:42.859 1135-1135/com.example.aravindpraveen.buttonsandboxes D/AndroidRuntime﹕ Shutting down VM
02-03 11:39:42.863 1135-1135/com.example.aravindpraveen.buttonsandboxes W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4c2c648)
02-03 11:39:42.899 1135-1135/com.example.aravindpraveen.buttonsandboxes E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.aravindpraveen.buttonsandboxes/com.example.aravindpraveen.buttonsandboxes.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
at android.content.ComponentName.<init>(ComponentName.java:75)
at android.content.Intent.<init>(Intent.java:3662)
at com.example.aravindpraveen.buttonsandboxes.MainActivity.<init>(MainActivity.java:15)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1130)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
from the second activity declare in Manifest, remove tags
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
i think starting activity as MAIN while it's not MAIN may cause this problem
also in documentaion
Activity Action: Start as a main entry point, does not expect to receive data.
EDIT:
also move
str=s.toString();
extras.putString("Education",str);
inside onClick that starts the 2nd activity
Hi I have an app written that goes from a login page, to another activity with a progress bar, then to another activity with a tab host. If I get rid of the loading page and go from login to the tab host works fine, but if I try to go from login to loading the app stops and says 'Unfortunately application has stopped.' I have checked the logcat and see that there is a null pointer error for the Loading page, but I can't see why. In the intent to go from login to loading I call loading correctly, and the set content view in Loading matches its xml loading_main. Here is my code below, thannks:
Main login activity:
package com.example.loginscreen;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText username=null;
private EditText password=null;
private TextView attempts;
private Button login;
int counter = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
attempts = (TextView)findViewById(R.id.textView5);
attempts.setText(Integer.toString(counter));
login = (Button)findViewById(R.id.button1);
}
public void login(View view){
if(username.getText().toString().equals("mara") &&
password.getText().toString().equals("mara")){
Toast.makeText(getApplicationContext(), "Login Successful!",
Toast.LENGTH_LONG).show();
startActivity(new Intent(MainActivity.this,Loading.class));
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",
Toast.LENGTH_SHORT).show();
attempts.setBackgroundColor(Color.RED);
counter--;
attempts.setText(Integer.toString(counter));
if(counter==0){
login.setEnabled(false);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Loading activity:
package com.example.loginscreen;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class Loading extends Activity {
Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
private long fileSize = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_main);
addListenerOnButton();
}
public void addListenerOnButton() {
btnStartProgress = (Button) findViewById(R.id.button1);
btnStartProgress.setOnClickListener( // <== This is line 33
new OnClickListener() {
#Override
public void onClick(View v) {
// prepare for a progress bar dialog
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("Searching for Driver Card...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
//reset progress bar status
progressBarStatus = 0;
//reset filesize
fileSize = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
// process some tasks
progressBarStatus = doSomeTasks();
// your computer is too fast, sleep 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
// driver card is found
if (progressBarStatus >= 100) {
// sleep 2 seconds, so that you can see the 100%
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// close the progress bar dialog
progressBar.dismiss();
}
}
});
startActivity(new Intent(Loading.this,LinkTabs.class));
}
});
}
// file download simulator
public int doSomeTasks() {
while (fileSize <= 1000000) {
fileSize++;
if (fileSize == 100000) {
return 10;
} else if (fileSize == 200000) {
return 20;
} else if (fileSize == 300000) {
return 30;
}
}
return 100;
}
}
Activity LinkTabs to link all tabs in tab host:
package com.example.loginscreen;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
#SuppressWarnings("deprecation")
public class LinkTabs extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.link_main);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
// First tab
Intent intentAndroid = new Intent().setClass(this, HomePage.class);
TabSpec tabSpecHomePage = tabHost
.newTabSpec("HomePage")
.setIndicator("", ressources.getDrawable(R.drawable.overview))
.setContent(intentAndroid);
// Second tab
Intent intentApple = new Intent().setClass(this, HomePage2.class);
TabSpec tabSpecHomePage2 = tabHost
.newTabSpec("HomePage2")
.setIndicator("", ressources.getDrawable(R.drawable.card_summary))
.setContent(intentApple);
// Third tab
Intent intentWindows = new Intent().setClass(this, HomePage3.class);
TabSpec tabSpecHomePage3 = tabHost
.newTabSpec("HomePage3")
.setIndicator("", ressources.getDrawable(R.drawable.details))
.setContent(intentWindows);
// add all tabs
tabHost.addTab(tabSpecHomePage);
tabHost.addTab(tabSpecHomePage2);
tabHost.addTab(tabSpecHomePage3);
//set Windows tab as default (zero based)
tabHost.setCurrentTab(2);
}
}
Tab activities:
package com.example.loginscreen;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HomePage extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("Overview");
setContentView(textview);
}
}
package com.example.loginscreen;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HomePage2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("Card Summary");
setContentView(textview);
}
}
package com.example.loginscreen;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HomePage3 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("Details");
setContentView(textview);
}
}
Here are the xml files:
//activity_main.xml
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_marginLeft="32dp"
android:layout_toRightOf="#+id/textView2"
android:ems="10"
tools:ignore="TextFields" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView3"
android:layout_alignLeft="#+id/editText1"
android:ems="10"
android:inputType="textPassword" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_marginTop="40dp"
android:layout_toLeftOf="#+id/editText1"
android:hint="Password"
android:text="#string/password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView4"
android:layout_alignBottom="#+id/textView4"
android:layout_alignLeft="#+id/button1"
android:text="TextView"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_alignParentTop="true"
android:layout_marginTop="155dp"
android:hint="Username"
android:text="#string/username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView3"
android:layout_alignLeft="#+id/textView2"
android:layout_alignParentTop="true"
android:src="#drawable/tranzlogo1" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_marginTop="26dp"
android:layout_toLeftOf="#+id/textView5"
android:text="#string/attempts"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_alignRight="#+id/editText2"
android:layout_below="#+id/textView4" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp"
android:onClick="login"
android:text="#string/Login" />
</RelativeLayout>
//fragment_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: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.loginscreen.MainActivity$PlaceholderFragment" >
<ImageView
android:id="#+id/tranzlogo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp"
android:src="#drawable/overview"
tools:ignore="ContentDescription" />
</RelativeLayout>
//link_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: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.loginscreen.MainActivity$PlaceholderFragment" >
<ImageView
android:id="#+id/tranzlogo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp"
android:src="#drawable/overview"
tools:ignore="ContentDescription" />
</RelativeLayout>
//loading_main.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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="413dp"
android:layout_height="382dp" />
</LinearLayout>
Here is loading_main.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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="413dp"
android:layout_height="382dp" />
</LinearLayout>
Here is the mainfest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.loginscreen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.loginscreen.MainActivity"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HomePage" />
<activity android:name=".HomePage2" />
<activity android:name=".HomePage3" />
<activity android:name=".Loading"/>
<activity android:name=".LinkTabs"/>
</application>
</manifest>
And the logcat output:
06-20 10:22:15.289: W/dalvikvm(29524): threadid=1: thread exiting with uncaught exception (group=0x40cca318)
06-20 10:22:15.299: E/AndroidRuntime(29524): FATAL EXCEPTION: main
06-20 10:22:15.299: E/AndroidRuntime(29524): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loginscreen/com.example.loginscreen.Loading}: java.lang.NullPointerException
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2063)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread.access$600(ActivityThread.java:134)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.os.Looper.loop(Looper.java:137)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread.main(ActivityThread.java:4744)
06-20 10:22:15.299: E/AndroidRuntime(29524): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 10:22:15.299: E/AndroidRuntime(29524): at java.lang.reflect.Method.invoke(Method.java:511)
06-20 10:22:15.299: E/AndroidRuntime(29524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-20 10:22:15.299: E/AndroidRuntime(29524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-20 10:22:15.299: E/AndroidRuntime(29524): at dalvik.system.NativeStart.main(Native Method)
06-20 10:22:15.299: E/AndroidRuntime(29524): Caused by: java.lang.NullPointerException
06-20 10:22:15.299: E/AndroidRuntime(29524): at com.example.loginscreen.Loading.addListenerOnButton(Loading.java:33)
06-20 10:22:15.299: E/AndroidRuntime(29524): at com.example.loginscreen.Loading.onCreate(Loading.java:26)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.Activity.performCreate(Activity.java:5008)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2027)
06-20 10:22:15.299: E/AndroidRuntime(29524): ... 11 more
You have referenced in Your LoadingMain activity the following layout with setContentView:
setContentView(R.layout.loading_main);
But this Layout does not have any button1. Instead, You are trying to reference a button from another layout, from activity_main.xml
btnStartProgress = (Button) findViewById(R.id.button1);
This button1 is inside the activity_main.xml and You cannot reference a button in a layout that is not attached per setContentView. So What You have to do is, add a button to the loading_main.xml, BUT, don´t give it the same id like in the activity main "button1". Sure, it works, to give same ids as long as the right layout is referenced, but id´s should be unique. If the button in the activity_layout should remain, give the id for your button inside loading_main.xml another name, maybe android:id="#+id/loading_main_button_1".
Clearly the error is in xml, you have not added the button here:
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="413dp"
android:layout_height="382dp" />
</LinearLayout>
Try adding the following in your loading_main.xml
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
The complete thing should look like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="413dp"
android:layout_height="382dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
NullPointerExcception
it causes because using null object or not defining value to object.
In Loading.java you have used
setContentView(R.layout.loading_main);
that don't have button1
add button1 in loading_main.xml it or Change layout in setContentView(R.layout.other_layout);
I run my app on the emulator and when I click on the button for Clubs it just crashes and gives the message "Unfortunately app has stopped in Android Emulator". I've looked at the logcat and it says to check its declared in the manifest file which it is. So I'm just not sure what do. I'll put up my code for the layout file, the java file and the manifest file. Any help would be greatly appreciated.
<?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/background"
android:orientation="vertical" >
<ImageView
android:id="#+id/school_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:contentDescription="#string/logo_desc"
android:src="#drawable/logo" />
<TextView
android:id="#+id/title_socities"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="20dp"
android:textSize="20sp"
android:textColor="#CC0000"
android:text="#string/socities_info" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/ace"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/ace_icon"
android:padding="20dp"
android:text="Ace Society"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/ace_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ace" />
<ImageButton
android:id="#+id/ace_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ace"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/anime_manga"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/anime_manga_icon"
android:padding="20dp"
android:text="Anime Manga"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/anime_manga_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/anime_manga" />
<ImageButton
android:id="#+id/anime_manga_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/anime_manga"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/best_buddies"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/best_buddies_icon"
android:padding="20dp"
android:text="Best Buddies"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/best_buddies_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/best_buddies" />
<ImageButton
android:id="#+id/best_buddies_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/best_buddies"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/computer_society"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/computer_society_icon"
android:padding="20dp"
android:text="Computer Society"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/computer_society_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/computer_socity" />
<ImageButton
android:id="#+id/computer_society_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_toRightOf="#+id/computer_society"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/cumann_gaelach"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/cumann_gaelach_icon"
android:padding="20dp"
android:text="Cumann Gaelach"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/cumann_gaelach_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/cumann_gaelach" />
<ImageButton
android:id="#+id/cumann_gaelach_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/cumann_gaelach"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/dj_society"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/dj_society_icon"
android:padding="20dp"
android:text="DJ Society"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/dj_society_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/dj_society" />
<ImageButton
android:id="#+id/dj_society_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/dj_society"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/eng_society"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/eng_society_icon"
android:padding="20dp"
android:text="Engineering Society"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/eng_society_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/eng_society" />
<ImageButton
android:id="#+id/eng_society_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/eng_society"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/equestrain"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/equestrain_icon"
android:padding="20dp"
android:text="Equestrain"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/equestrain_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/equestrian" />
<ImageButton
android:id="#+id/equestrain_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/equestrain"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/fianna_fail"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/fianna_fail_icon"
android:padding="20dp"
android:text="Fianna Fail"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/fianna_fail_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/fianna_fail" />
<ImageButton
android:id="#+id/fianna_fail_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/fianna_fail"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/film_society"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/film_society_icon"
android:padding="20dp"
android:text="Film Society"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/film_society_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/film_society" />
<ImageButton
android:id="#+id/film_society_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/film_society"
android:src="#drawable/facebook" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Java file
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Clubs extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.socities);
Button aces = (Button) findViewById(R.id.ace_fb);
Button anime_manga = (Button) findViewById(R.id.anime_manga_fb);
Button best_buddies = (Button) findViewById(R.id.best_buddies_fb);
Button computer_society = (Button) findViewById(R.id.computer_society_fb);
Button cumann_gaelach = (Button) findViewById(R.id.cumann_gaelach_fb);
Button dj_society = (Button) findViewById(R.id.dj_society_fb);
Button eng_society = (Button) findViewById(R.id.eng_society_fb);
Button equestrian = (Button) findViewById(R.id.equestrain_fb);
Button fianna_fail = (Button) findViewById(R.id.fianna_fail_fb);
Button film_society = (Button) findViewById(R.id.film_society_fb);
Button foiroige = (Button) findViewById(R.id.foiroige_fb);
Button games = (Button) findViewById(R.id.games_fb);
Button wish_makers_on_campus = (Button) findViewById(R.id.wish_makers_on_campus_fb);
Button heritage = (Button) findViewById(R.id.heritage_fb);
Button internationl_society = (Button) findViewById(R.id.internationl_society_fb);
Button letterfrack = (Button) findViewById(R.id.letterfrack_fb);
Button lgbt = (Button) findViewById(R.id.lgbt_fb);
Button living_history = (Button) findViewById(R.id.living_history_fb);
Button mature_students = (Button) findViewById(R.id.mature_students_fb);
Button photography = (Button) findViewById(R.id.photography_fb);
Button robotics = (Button) findViewById(R.id.robotics_fb);
Button science = (Button) findViewById(R.id.science_fb);
Button sinnfein = (Button) findViewById(R.id.sinnfein_fb);
aces.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/aceofclubsgmit?fref=ts"));
startActivity(myWebLink);
}
});
anime_manga.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/gmitanimemangasoc"));
startActivity(myWebLink);
}
});
best_buddies.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/pages/Best-Buddies-GMIT/305305566158422?fref=ts"));
startActivity(myWebLink);
}
});
computer_society.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/GMITComputerSociety"));
startActivity(myWebLink);
}
});
cumann_gaelach.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/gmitcumannnagaeilge?fref=ts"));
startActivity(myWebLink);
}
});
dj_society.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/GmitDjSociety"));
startActivity(myWebLink);
}
});
eng_society.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/pages/EngSoc-GMIT/196642613742557"));
startActivity(myWebLink);
}
});
equestrian.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/gmit.ridingclub"));
startActivity(myWebLink);
}
});
fianna_fail.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/sean.mulroycumann"));
startActivity(myWebLink);
}
});
film_society.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/pages/GMIT-Film-Making-Society/1376699195883960?fref=ts"));
startActivity(myWebLink);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
============================================================================
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ie.gmit.pleasework"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="ie.gmit.pleasework.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="ie.gmit.pleasework.Sponsors"
android:label="#string/airsoft" >
</activity>
<activity
android:name="ie.gmit.pleasework.Societies"
android:label="#string/airsoft" >
</activity>
<activity
android:name="ie.gmit.pleasework.Clubs"
android:label="#string/airsoft" >
</activity>
</application>
</manifest>
=================================================================================
logcat
04-29 11:50:28.660: E/AndroidRuntime(1642): FATAL EXCEPTION: main
04-29 11:50:28.660: E/AndroidRuntime(1642): Process: ie.gmit.pleasework, PID: 1642
04-29 11:50:28.660: E/AndroidRuntime(1642): android.content.ActivityNotFoundException: Unable to find explicit activity class {ie.gmit.pleasework/ie.gmit.pleasework.Societies}; have you declared this activity in your AndroidManifest.xml?
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.app.Activity.startActivityForResult(Activity.java:3424)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.app.Activity.startActivityForResult(Activity.java:3385)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.app.Activity.startActivity(Activity.java:3627)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.app.Activity.startActivity(Activity.java:3595)
04-29 11:50:28.660: E/AndroidRuntime(1642): at ie.gmit.pleasework.MainActivity$8.onClick(MainActivity.java:101)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.view.View.performClick(View.java:4438)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.view.View$PerformClick.run(View.java:18422)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.os.Handler.handleCallback(Handler.java:733)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.os.Handler.dispatchMessage(Handler.java:95)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.os.Looper.loop(Looper.java:136)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-29 11:50:28.660: E/AndroidRuntime(1642): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 11:50:28.660: E/AndroidRuntime(1642): at java.lang.reflect.Method.invoke(Method.java:515)
04-29 11:50:28.660: E/AndroidRuntime(1642): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-29 11:50:28.660: E/AndroidRuntime(1642): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-29 11:50:28.660: E/AndroidRuntime(1642): at dalvik.system.NativeStart.main(Native Method)
Main Activity
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button contact = (Button) findViewById(R.id.btnContact);
Button fixturesAndResults = (Button) findViewById(R.id.btnFixtureandResults);
Button newsFeed = (Button) findViewById(R.id.btnNewsFeed);
Button sponsors = (Button) findViewById(R.id.btnSponsors);
Button scholarships = (Button) findViewById(R.id.btnScholarships);
Button facilites = (Button) findViewById(R.id.btnFacilities);
Button clubs = (Button) findViewById(R.id.btnClubs);
Button societies = (Button) findViewById(R.id.btnSocieties);
Button about = (Button) findViewById(R.id.btnAbout);
contact.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), Contact.class);
startActivity(i);
}
});
fixturesAndResults.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), FixturesAndResults.class);
startActivity(i);
}
});
newsFeed.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), NewsFeed.class);
startActivity(i);
}
});
sponsors.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), Sponsors.class);
startActivity(i);
}
});
scholarships.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), Scholarships.class);
startActivity(i);
}
});
facilites.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), Facilites.class);
startActivity(i);
}
});
clubs.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), Clubs.class);
startActivity(i);
}
});
societies.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), Societies.class);
startActivity(i);
}
});
about.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), About.class);
startActivity(i);
}
});
}
}
You declared your Activities incorrectly in your manifest.
You wrote, for instance
<activity
android:name="ie.gmit.pleasework.Societies"
android:label="#string/airsoft" >
</activity>
and should have put
<activity
android:name=".Societies"
android:label="#string/airsoft" >
</activity>
The names of the classes are taken to be from the root of your application (ie.gmit.pleasework). By writing your complete package twice, you're asking Dalvik to find the class ie.gmit.pleasework/ie.gmit.pleasework.Societies
Here's the java code for the class
package com.warriorsoft.mtgupanddown;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
int guy1;
int guy2;
MediaPlayer sound;
Button plus1g1, plus1g2, min1g1, min1g2, plus5g1, plus5g2, min5g1, min5g2,
GG, RESET;
TextView g1, g2;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
guy1 = 20;
guy2 = 20;
min5g1 = (Button) findViewById(R.id.button1); // x
plus5g1 = (Button) findViewById(R.id.button2); //x
plus1g1 = (Button) findViewById(R.id.button3); // x
min1g1 = (Button) findViewById(R.id.button4); // x
min5g2 = (Button) findViewById(R.id.button5); // x
plus5g2 = (Button) findViewById(R.id.button6); //x
min1g2 = (Button) findViewById(R.id.button7); // x
plus1g2 = (Button) findViewById(R.id.button8); //x
RESET = (Button) findViewById(R.id.RESET);
GG = (Button) findViewById(R.id.GG);
g1 = (TextView) findViewById(R.id.tvPlayer1HP);
g2 = (TextView) findViewById(R.id.tvPlayer2HP);
min5g1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
guy1 -= 5;
g1.setText(guy1);
}
});
min5g2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy2 -= 5;
g2.setText(guy2);
}
});
min1g1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy1 -= 1;
g1.setText(guy1);
}
});
min1g2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy2 -= 1;
g2.setText(guy2);
}
});
plus5g2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy2 += 5;
g2.setText(guy2);
}
});
plus5g1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy1 += 5;
g1.setText(guy1);
}
});
plus1g1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy1 += 1;
g1.setText(guy1);
}
});
plus1g2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy2 += 1;
g2.setText(guy2);
}
});
RESET.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0){
guy1 = 20;
guy2 = 20;
g2.setText(guy2);
g1.setText(guy1);
}
});
GG.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0){
sound = MediaPlayer.create(MainActivity.this, R.raw.gg );
}
});
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Here's the layout 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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/tvPlayer2HP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="36dp"
android:text="20"
android:textSize="35dp" />
<TextView
android:id="#+id/tvPlayer1HP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="36dp"
android:text="20"
android:textSize="35dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_marginLeft="18dp"
android:layout_toRightOf="#+id/button1"
android:text="+5" />
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button3"
android:layout_alignBottom="#+id/button3"
android:layout_alignLeft="#+id/button2"
android:text="-1" />
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_alignRight="#+id/button1"
android:layout_marginBottom="28dp"
android:text="+1" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_alignParentRight="true"
android:text="+5" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_marginRight="14dp"
android:layout_toLeftOf="#+id/button6"
android:text="-5" />
<Button
android:id="#+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/button5"
android:layout_alignTop="#+id/button4"
android:text="+1" />
<Button
android:id="#+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button7"
android:layout_alignBottom="#+id/button7"
android:layout_toRightOf="#+id/button5"
android:text="-1" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_alignLeft="#+id/button5"
android:ems="10"
android:hint="Guy 1" >
</EditText>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/button2"
android:layout_below="#+id/button1"
android:layout_marginTop="36dp"
android:ems="10"
android:hint="Guy 2" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvPlayer1HP"
android:layout_marginTop="74dp"
android:text="-5" />
<ImageView
android:id="#+id/GG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvPlayer2HP"
android:layout_centerHorizontal="true"
android:minHeight="140dp"
android:minWidth="120dp"
android:src="#drawable/gg" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:text="Optimal sideways"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/RESET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="RESET" />
</RelativeLayout>
And here's the logcat
01-16 14:01:51.292: W/dalvikvm(699): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-16 14:01:51.302: E/AndroidRuntime(699): FATAL EXCEPTION: main
01-16 14:01:51.302: E/AndroidRuntime(699): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.warriorsoft.mtgupanddown/com.warriorsoft.mtgupanddown.MainActivity}: java.lang.ClassCastException: android.widget.ImageView
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.os.Looper.loop(Looper.java:123)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-16 14:01:51.302: E/AndroidRuntime(699): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 14:01:51.302: E/AndroidRuntime(699): at java.lang.reflect.Method.invoke(Method.java:507)
01-16 14:01:51.302: E/AndroidRuntime(699): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-16 14:01:51.302: E/AndroidRuntime(699): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-16 14:01:51.302: E/AndroidRuntime(699): at dalvik.system.NativeStart.main(Native Method)
01-16 14:01:51.302: E/AndroidRuntime(699): Caused by: java.lang.ClassCastException: android.widget.ImageView
01-16 14:01:51.302: E/AndroidRuntime(699): at com.warriorsoft.mtgupanddown.MainActivity.onCreate(MainActivity.java:33)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-16 14:01:51.302: E/AndroidRuntime(699): ... 11 more
01-16 14:01:53.712: I/Process(699): Sending signal. PID: 699 SIG: 9
The problem is running the code on a phone.
It tells me that the program broke in some unexpected way and forces me to force close.
If anyone could please help me understand what i've done wrong i'll be eternally grateful
I am still new to programming and can barely understand the logcats
please go easy
Thankyou for trying
You are finding the ImageView and casting it to Button.
check this line of code.
GG = (Button) findViewById(R.id.GG);
Change the type of GG as ImageView and typecast to ImageView as below:
ImageView GG;
GG = (ImageView) findViewById(R.id.GG);
Change this to this
GG = (Button) findViewById(R.id.GG);
to
GG = (ImageView) findViewById(R.id.GG);
you cannot cast an ImageView to a Button. Which is what you are doing