Android admob fatal exception - java

I am trying to ad admob ads to my app but I am getting this error in the logcat when it launches. The app ran fine before I added the admob so I think it's something to do with that.
03-16 19:08:00.683: E/AndroidRuntime(529): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.musicbynumbers.scalesads/com.musicbynumbers.scalesads.MainMenu}: java.lang.NullPointerException
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.os.Looper.loop(Looper.java:123)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-16 19:08:00.683: E/AndroidRuntime(529): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 19:08:00.683: E/AndroidRuntime(529): at java.lang.reflect.Method.invoke(Method.java:521)
03-16 19:08:00.683: E/AndroidRuntime(529): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-16 19:08:00.683: E/AndroidRuntime(529): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-16 19:08:00.683: E/AndroidRuntime(529): at dalvik.system.NativeStart.main(Native Method)
03-16 19:08:00.683: E/AndroidRuntime(529): Caused by: java.lang.NullPointerException
03-16 19:08:00.683: E/AndroidRuntime(529): at com.musicbynumbers.scalesads.MainMenu.onCreate(MainMenu.java:69)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-16 19:08:00.683: E/AndroidRuntime(529): ... 11 more
Java of MainMenu:
package com.musicbynumbers.scales;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import com.musicbynumbers.pianoscalesadscopy.R;
public class MainMenu extends LicenseCheckActivity implements View.OnClickListener {
Button majScales, minHarm, minMel, majArp, minArp, chrome, cont, pent,how;
ImageButton mainMenu;
Intent j;
Intent k;
Intent l;
Intent m;
Intent n;
Intent o;
Intent p;
Intent q;
Intent r;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFormat(PixelFormat.RGBA_8888);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
setContentView(R.layout.activity_main_menu);
Toast.makeText(this, "Checking Application License", Toast.LENGTH_SHORT).show();
// Check the license
//checkLicense();
j = new Intent(MainMenu.this, majorScales.class);
k = new Intent(MainMenu.this, minorHarmonic.class);
l = new Intent(MainMenu.this, MajorArpeggios.class);
m = new Intent(MainMenu.this, MinorArpeggios.class);
n = new Intent(MainMenu.this, ChromaticScales.class);
o = new Intent(MainMenu.this, ContraryMotion.class);
p = new Intent(MainMenu.this, PentatonicScales.class);
q = new Intent(MainMenu.this, minorMelodic.class);
r = new Intent(MainMenu.this, HowItWorks.class);
mainMenu = (ImageButton) findViewById(R.id.imagelogo);
majScales = (Button) findViewById(R.id.majorscalesb);
minHarm = (Button) findViewById(R.id.minorharmonicb);
minMel = (Button) findViewById(R.id.minormelodicb);
majArp = (Button) findViewById(R.id.majorarpeggiosb);
minArp = (Button) findViewById(R.id.minorarpeggiosb);
chrome = (Button) findViewById(R.id.chromaticscalesb);
cont = (Button) findViewById(R.id.contraryb);
pent = (Button) findViewById(R.id.pentatonicscaleb);
how = (Button) findViewById(R.id.howitworksb);
majScales.setOnClickListener(this);
mainMenu.setOnClickListener(this);
minHarm.setOnClickListener(this);
minMel.setOnClickListener(this);
majArp.setOnClickListener(this);
minArp.setOnClickListener(this);
chrome.setOnClickListener(this);
cont.setOnClickListener(this);
pent.setOnClickListener(this);
how.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.imagelogo:
Intent i = new Intent(MainMenu.this, MainMenu.class);
startActivity(i);
break;
case R.id.majorscalesb:
startActivity(j);
break;
case R.id.minorharmonicb:
startActivity(k);
break;
case R.id.majorarpeggiosb:
startActivity(l);
break;
case R.id.minorarpeggiosb:
startActivity(m);
break;
case R.id.chromaticscalesb:
startActivity(n);
break;
case R.id.contraryb:
startActivity(o);
break;
case R.id.pentatonicscaleb:
startActivity(p);
break;
case R.id.minormelodicb:
startActivity(q);
break;
case R.id.howitworksb:
startActivity(r);
break;
}
}
}
activity_main_menu xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/musicbynumbersbg"
android:orientation="vertical"
android:weightSum="100"
tools:context=".MainMenu" >
<ImageButton
android:id="#+id/imagelogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="90"
android:background="#drawable/topbanner"
android:gravity="center" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="10" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >
<Button
android:id="#+id/howitworksb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="How to Use the App" />
<Button
android:id="#+id/majorscalesb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Major Scales" />
<Button
android:id="#+id/minormelodicb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Minor Melodic Scales" />
<Button
android:id="#+id/minorharmonicb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Minor Harmonic Scales" />
<Button
android:id="#+id/majorarpeggiosb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Major Arpeggios" />
<Button
android:id="#+id/minorarpeggiosb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Minor Arpeggios" />
<Button
android:id="#+id/chromaticscalesb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Chromatic Scales" />
<Button
android:id="#+id/contraryb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Contrary Motion" />
<Button
android:id="#+id/pentatonicscaleb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Pentatonic Scale" />
</LinearLayout>
</ScrollView>
<com.google.ads.AdView android:id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId=" a15144ab55b99a9"
ads:loadAdOnCreate="true"
ads:adSize="BANNER"
/>
</LinearLayout>
Thanks in advance for trying to help.

I guess you are not setting right layout here.
setContentView(R.layout.activity_main_menu); <<You set the menu layout.
You must set the layout which is in res folder.
If you have layout.xml in res folder then set it like
setContentView(R.layout.layout);
Edit: Clean the project and run it.

Related

NullPointerException using TabHost in Dialog? How to do it right?

I'm trying to use a Tabhostin a Dialog so that the user can toggle inside the Dialog between to layouts. But I always get a NullPointerException. Any advice? or How to do it right?
Dialog Method:
public void dialog(){
final Dialog d = new Dialog(this);
d.setTitle("Dialog);
d.setContentView(R.layout.dialog);
d.setCanceledOnTouchOutside(false);
TabHost tabHost = (TabHost) d.findViewById(android.R.id.tabhost);
Button b_set = (Button) d.findViewById(R.id.b_choose);
Button b_cancel = (Button) d.findViewById(R.id.b_cancel);
tabHost.setup(); //Here is the problem
TabHost.TabSpec tab1 = tabHost.newTabSpec("Favs");
TabHost.TabSpec tab2 = tabHost.newTabSpec("All");
tab1.setIndicator("NEWTAB");
tab1.setContent(new Intent(this,Tab1Activity.class));
tab2.setIndicator("NEWTAB");
tab2.setContent(new Intent(this,Tab2Activity.class));
tabHost.addTab(tab1);
tabHost.addTab(tab2);
d.show();
}
Dialog.xml:
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabHost
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabHost">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Logcat:
11-30 14:48:06.661 7157-7157/com.test.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.test.app.Main.dialog (Main.java:269)
at com.test.app.Main$1.onClick(Main.java:139)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
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:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
TabHost tabHost = (TabHost) d.findViewById(R.id.tabHost);
id is case sensitive

Unable to start another activity with button

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

Android crash in eclipse android SDK

I'm beginner in android developement. And i have a problem with my code. The application is totally crashed. It is Java error in main.
MainActivity.java:
package hu.cgkni6.uniobuda.testapplication;
import hu.cgkni6.uniobuda.testapplication.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
final String username = "diak";
final String pass = "diak";
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
okbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (usrname.getText().toString().equals(username))
{
if (passwd.getText().toString().equals(pass))
{
String text2 = "Success!";
Toast.makeText(MainActivity.this, text2, Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this, "Invalid Password", Toast.LENGTH_LONG).show();
}
}
else {
Toast.makeText(MainActivity.this, "Invalid username", Toast.LENGTH_LONG).show();
}
}
});
cancelbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Activity_main.xml:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#45B7FA" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:text="#string/login_interface" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="#string/username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="#string/username" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="#string/password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="#string/password" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_OK"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="45dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:textSize="20sp"
android:text="#string/ok" />
<Button
android:id="#+id/btn_Cancel"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="45dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:textSize="20sp"
android:text="#string/cancel" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="3dp" >
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="#string/about_us"
/>
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="#string/contact_us" />
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="#string/help" />
</TableRow>
</RelativeLayout>
</LinearLayout>
Catlog:
10-09 09:18:26.244: E/AndroidRuntime(302): FATAL EXCEPTION: main
10-09 09:18:26.244: E/AndroidRuntime(302): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{hu.cgkni6.uniobuda.testapplication/hu.cgkni6.uniobuda.testapplication.MainActivity}: java.lang.NullPointerException
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.os.Looper.loop(Looper.java:123)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.reflect.Method.invoke(Method.java:521)
10-09 09:18:26.244: E/AndroidRuntime(302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-09 09:18:26.244: E/AndroidRuntime(302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-09 09:18:26.244: E/AndroidRuntime(302): at dalvik.system.NativeStart.main(Native Method)
10-09 09:18:26.244: E/AndroidRuntime(302): Caused by: java.lang.NullPointerException
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.Activity.findViewById(Activity.java:1637)
10-09 09:18:26.244: E/AndroidRuntime(302): at hu.cgkni6.uniobuda.testapplication.MainActivity.<init>(MainActivity.java:17)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.Class.newInstanceImpl(Native Method)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.Class.newInstance(Class.java:1429)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
10-09 09:18:26.244: E/AndroidRuntime(302): ... 11 more
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
Put this in the oncreate method after setContentView(R.layout.activity_main);
and you should be good. You cant do it from that location :)
You should initialize your Views after setting the layout in your onCreate() method.
You are getting the error because you are trying to access the views before setting the layout that is why the application is not able to find the view's id.
Change your code as below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
}
You're trying to find views before your layout is setup. Move
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
to your onCreate method. Just after your setContentView call.
First of all you should learn Android life cycle
you should find your all button and edit text in the oncreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
}
Initialize you Button and EditText outside the onCreate method like this
Button okbutton;
Button cancelbutton;
EditText usrname;
EditText passwd;
Then insert this
okbutton = (Button) findViewById(R.id.btn_OK);
cancelbutton = (Button) findViewById(R.id.btn_Cancel);
EditText usrname = (EditText) findViewById(R.id.editText1);
EditText passwd = (EditText) findViewById(R.id.editText2);
inside your onCreate method after the setContentView(R.layout.activity_main);
Hope it helps.

"unable to start activity component info" droid force close issue

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

Android: How do you set up the TabHost

I am trying to use the tabhost when I add it to my xml it doesn't look right, and I believe there is something that needs to be done in java, I am trying to set up three tabs with three different classes is this possible?
Here is my 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:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Browser History:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call Log"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Messages"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvSms"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
</LinearLayout>
And I have three different classes, because I am trying to use each tab to open each activity.
Here are the Classes
package com.johnnydicamillo.spybeta;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Browser;
import android.widget.TabHost;
import android.widget.TextView;
public class AndroidSpybetaActivity extends TabActivity {
/** Called when the activity is first created. */
Resources res;
TabHost tabHost;
Intent intent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
res = getResources();
tabHost = getTabHost();
TabHost.TabSpec spec;
intent = new Intent().setClass(this, Messaging.class);
spec = tabHost.newTabSpec("messaging").setIndicator("Messaging")
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, TestingData.class);
spec = tabHost.newTabSpec("Calls").setIndicator("Calls")
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
TextView view = (TextView) findViewById(R.id.hello);
Cursor mCur = managedQuery(android.provider.Browser.BOOKMARKS_URI,
null, null, null, null);
mCur.moveToFirst();
int index = mCur.getColumnIndex(Browser.BookmarkColumns.TITLE);
while (mCur.isAfterLast() == false) {
view.append(" WebSite " + mCur.getString(index));
mCur.moveToNext();
}
}
}
Second
package com.johnnydicamillo.spybeta;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Messaging extends TabActivity{
static TextView messageBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
messageBox = (TextView) findViewById(R.id.tvSms);
}
public static void updateMessageBox(String msg) {
messageBox.append(msg);
}
}
and third
package com.johnnydicamillo.spybeta;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.widget.TextView;
public class TestingData extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView view = (TextView) findViewById(R.id.call);
String[] projection = new String[] {
Calls.NUMBER
};
Cursor mCur = managedQuery(CallLog.Calls.CONTENT_URI, projection,
Calls.DURATION + "<?", new String[] { "60" }, Calls.DURATION
+ " ASC");
mCur.moveToFirst();
while (mCur.isAfterLast() == false) {
for (int i = 0; i < mCur.getColumnCount(); i++) {
view.append(" Number " + mCur.getString(i));
}
mCur.moveToNext();
}
}
}
Here is my logcat:
08-12 15:19:16.368: D/AndroidRuntime(280): Shutting down VM
08-12 15:19:16.368: W/dalvikvm(280): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-12 15:19:16.628: E/AndroidRuntime(280): FATAL EXCEPTION: main
08-12 15:19:16.628: E/AndroidRuntime(280): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.johnnydicamillo.spybeta/com.johnnydicamillo.spybeta.AndroidSpybetaActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.johnnydicamillo.spybeta/com.johnnydicamillo.spybeta.Messaging}; have you declared this activity in your AndroidManifest.xml?
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.os.Looper.loop(Looper.java:123)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-12 15:19:16.628: E/AndroidRuntime(280): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 15:19:16.628: E/AndroidRuntime(280): at java.lang.reflect.Method.invoke(Method.java:521)
08-12 15:19:16.628: E/AndroidRuntime(280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-12 15:19:16.628: E/AndroidRuntime(280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-12 15:19:16.628: E/AndroidRuntime(280): at dalvik.system.NativeStart.main(Native Method)
08-12 15:19:16.628: E/AndroidRuntime(280): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.johnnydicamillo.spybeta/com.johnnydicamillo.spybeta.Messaging}; have you declared this activity in your AndroidManifest.xml?
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.resolveActivityInfo(ActivityThread.java:2473)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:277)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.widget.TabHost.setCurrentTab(TabHost.java:323)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.widget.TabHost.addTab(TabHost.java:213)
08-12 15:19:16.628: E/AndroidRuntime(280): at com.johnnydicamillo.spybeta.AndroidSpybetaActivity.onCreate(AndroidSpybetaActivity.java:31)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-12 15:19:16.628: E/AndroidRuntime(280): ... 11 more
08-12 15:19:21.929: I/Process(280): Sending signal. PID: 280 SIG: 9
Yes, it is possible.
You can specify each activity (Start an intent) for each tabs in the following manner
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
res = getResources();
tabHost = getTabHost();
TabHost.TabSpec spec;
intent = new Intent().setClass(this, CalendarActivity.class);
spec = tabHost.newTabSpec("calendar").setIndicator("Calendar", res.getDrawable(R.drawable.ic_tab_calendar)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ProfileActivity.class);
spec = tabHost.newTabSpec("profile").setIndicator("Profile", res.getDrawable(R.drawable.ic_tab_profile)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
Each activity will have their own content layout views, therefore no need to worry about that in the main layout.
Your main XML layout will be small and simple as
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
I think this is what you want.
It is not possible to have a tab correspond to an activity. The purpose of tabs is to break up one activity into how ever many views if there is too much info display in one view(not to be confused with activity). Here however is how you set up TabHost:
First start with an xml:
<?xml version="1.0" encoding="UTF-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
-
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
-
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<AnalogClock
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="A semi-random button" />
</FrameLayout>
</LinearLayout>
</TabHost>
Sorry that's hard to read but if you copy it into eclipse you should be fine. And then the Java:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
public class TabDemo extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TabHost tabs=(TabHost)findViewById(R.id.tabhost); //Id of tab host
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("tag1");//make a new tab
spec.setContent(R.id.tab1); //What is in the tab (not an activity but rather a view)
spec.setIndicator("Clock"); //Name of tab
tabs.addTab(spec); //Add it
spec=tabs.newTabSpec("tag2"); //Same thing here
spec.setContent(R.id.tab2);
spec.setIndicator("Button");
tabs.addTab(spec);
}
}
This is relatively simple an I am sure there is support in the android development website and of course just searching on Google. This code was copied out of Beginning Android 4 by Grant Allen and the book explains this topic in much greater detail. Good luck!

Categories

Resources