Eclipse "Sorry- The application (Appname) has stopped unexpectedly. Please try again" - java

Hey im a begginer in android coding and ive been following this series on youtube. Whenever I run my project as an android application and go to the emulator to select it, it gives me "Sorry- The application (Appname) has stopped unexpectedly. Please try again" Eclipse doesnt give me any error line to where the problem is and so it is very frustrating.. Ive been looking on the internet for answers to this, but I havent been able to fix it. I think it has something to do with my Manifest. Whenever i run my application there are errors on my LogCat.
My application is a small app that when selected will first open a logo screen for 3 seconds then it will take you to an interface where there are 2 buttons to choose from. Both buttons lead to this thing that edits text.
Ive posted all my code below and please guys keep in mind when you answer that I am a begginer and might not understand some terms. Thank you!
Here is my LogCat when i try to start it
07-20 20:19:17.382: D/dalvikvm(254): GC_EXTERNAL_ALLOC freed 663 objects / 51912 bytes in 177ms
07-20 20:19:19.791: D/AndroidRuntime(254): Shutting down VM
07-20 20:19:19.801: W/dalvikvm(254): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-20 20:19:19.811: E/AndroidRuntime(254): FATAL EXCEPTION: main
07-20 20:19:19.811: E/AndroidRuntime(254): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.thepasics/com.example.thepasics.Menu}: java.lang.ClassNotFoundException: com.example.thepasics.Menu in loader dalvik.system.PathClassLoader[/data/app/com.example.thepasics-2.apk]
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.os.Looper.loop(Looper.java:123)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-20 20:19:19.811: E/AndroidRuntime(254): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 20:19:19.811: E/AndroidRuntime(254): at java.lang.reflect.Method.invoke(Method.java:521)
07-20 20:19:19.811: E/AndroidRuntime(254): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-20 20:19:19.811: E/AndroidRuntime(254): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-20 20:19:19.811: E/AndroidRuntime(254): at dalvik.system.NativeStart.main(Native Method)
07-20 20:19:19.811: E/AndroidRuntime(254): Caused by: java.lang.ClassNotFoundException: com.example.thepasics.Menu in loader dalvik.system.PathClassLoader[/data/app/com.example.thepasics-2.apk]
07-20 20:19:19.811: E/AndroidRuntime(254): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
07-20 20:19:19.811: E/AndroidRuntime(254): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
07-20 20:19:19.811: E/AndroidRuntime(254): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-20 20:19:19.811: E/AndroidRuntime(254): ... 11 more
My Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thepasics"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.thepasics.Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thePasics.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TutorialOne"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thePasics.TUTORIALONE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
I have 3 Java files which are Menu.java Main.java TutorialOne.java. This one is the Main one
package com.example.thepasics;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class Main extends Activity {
MediaPlayer logoMusic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
logoMusic = MediaPlayer.create(Main.this, R.raw.music);
logoMusic.start();
Thread logoTimer = new Thread(){
public void run(){
try{
sleep(2000);
Intent menuIntent = new Intent("com.example.thePasics.MENU");
startActivity(menuIntent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
logoMusic.release();
}
}
This One is the menu.java
package com.example.thepasics;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class menu extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Button sound
final MediaPlayer buttonSound = MediaPlayer.create(menu.this, R.raw.buttonsound);
//Setting up button references
Button tut1 = (Button) findViewById(R.id.button1);
Button tut2 = (Button) findViewById(R.id.button2);
tut1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent("com.example.thepasics.TUTORIALONE"));
}
});
tut2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent("com.example.thepasics.TutorialOne"));
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
The third one is the TutorialOne.java
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class TutorialOne extends Activity implements OnCheckedChangeListener{
TextView textOut;
EditText textIn;
RadioGroup gravityG, styleG;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
textOut = (TextView) findViewById(R.id.tvChange);
textIn = (EditText) findViewById(R.id.editText1);
gravityG = (RadioGroup) findViewById(R.id.rgGravity);
gravityG.setOnCheckedChangeListener(this);
styleG = (RadioGroup) findViewById(R.id.rgStyle);
styleG.setOnCheckedChangeListener(this);
Button gen = (Button) findViewById(R.id.bGenerate);
gen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textOut.setText(textIn.getText());
}
});
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.rbLeft:
textOut.setGravity(Gravity.LEFT);
break;
case R.id.rbCenter:
textOut.setGravity(Gravity.CENTER);
break;
case R.id.rbRight:
textOut.setGravity(Gravity.RIGHT);
break;
case R .id.rbNormal:
textOut.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL), Typeface.NORMAL);
break;
case R .id.rbItalic:
textOut.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC), Typeface.ITALIC);
break;
case R .id.rbBold:
textOut.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD), Typeface.BOLD);
break;
}
}
}
My 3 xml files are splash, tutorial1 and main
this one is 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:background="#drawable/backgroundwithoutericapp"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Choose a function" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button1"
android:textSize="25dp"
android:textStyle="bold"
android:id="#+id/button1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button2"
android:textSize="25dp"
android:textStyle="bold"
android:id="#+id/button2"/>
</LinearLayout>
second one is splash.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"
android:background="#drawable/background">
</LinearLayout>
third one is tutorial1.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="#drawable/tutorialonebackground"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="#+id/tvStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:text="Style"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvGravity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:text="Gravity"
android:textSize="25dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<RadioGroup
android:id="#+id/rgStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<RadioButton
android:id="#+id/rbNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Normal" />
<RadioButton
android:id="#+id/rbItalic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Italic" />
<RadioButton
android:id="#+id/rbBold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bold" />
</RadioGroup>
<RadioGroup
android:id="#+id/rgGravity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<RadioButton
android:id="#+id/rbLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left" />
<RadioButton
android:id="#+id/rbCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Center" />
<RadioButton
android:id="#+id/rbRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right" />
</RadioGroup>
</LinearLayout>
<TextView
android:id="#+id/tvChange"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Type in Text and Press the Button Below" />
<Button
android:id="#+id/bGenerate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Generate" />
</LinearLayout>

You have specified wrong class name in manifest.xml
Replace
<activity
android:name="Menu"
android:label="#string/app_name" >
with
<activity
android:name="com.example.thepasics.menu"
android:label="#string/app_name" >

Your Manifest and menu activity don't match. Your manifest has
<activity
android:name="Menu" ...
which first of all isn't a valid name. It could be either android:name=".Menu" to refer to com.com.example.thepasics.Menu or you could use the fully qualified name (as you did for com.example.thepasics.Main).
In addition, your class is named menu, not Menu - remember it is case sensitive. Java convention has class names starting with a capital letter, so it should probably be corrected to Menu.

Your Menu class is actually called "menu" and not "MENU".
public class menu extends Activity{
Try changing "com.example.thePasics.MENU" to "com.example.thePasics.menu" throughout your app.

I only have looked into your Logcat:
07-20 20:19:19.801: W/dalvikvm(254): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-20 20:19:19.811: E/AndroidRuntime(254): FATAL EXCEPTION: main
07-20 20:19:19.811: E/AndroidRuntime(254): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.thepasics/com.example.thepasics.Menu}: java.lang.ClassNotFoundException: com.example.thepasics.Menu in loader dalvik.system.PathClassLoader[/data/app/com.example.thepasics-2.apk]
Your app is crashing becuase your com.example.thepasics.Menu is not being found, and also use android:name=".Menu" instead of android:name="Menu".

Related

App stopped working as soon as i clicked the button

I built a simple project that squares a given number. In that app as soon as i click the button "Calculate" it say's "Unfortunately, SquareCalculator has stopped". What should i do to resolve the problem?
Below is my entire code:-
MainActivity.java
package thenerdimite.squarecalculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void calculate (View v){
EditText input = (EditText) findViewById(R.id.etInput);
TextView output = (TextView) findViewById(R.id.tvOutput);
int base = Integer.valueOf(input.getText().toString());
int result = base * base;
String formattedResult = String.format("%,d", result);
output.setText("Result: " + formattedResult);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="thenerdimite.squarecalculator.MainActivity">
<EditText
android:id="#+id/etInput"
android:layout_width="245dp"
android:layout_height="41dp"
android:layout_marginTop="15dp"
android:ems="10"
android:inputType="number"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="62dp"
android:layout_below="#+id/tvInput"
android:layout_alignStart="#+id/tvInput" />
<Button
android:id="#+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/etInput"
android:layout_below="#+id/etInput"
android:layout_marginTop="20dp"
android:onClick="Calculate"
android:text="Calculate"
android:textSize="18sp" />
<TextView
android:id="#+id/tvOutput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result:"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:layout_below="#+id/btnCalculate"
android:layout_alignStart="#+id/btnCalculate"
android:layout_marginTop="18dp" />
<TextView
android:id="#+id/tvInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter an Integer:"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:layout_marginStart="19dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
logcat
04-25 09:28:05.060 3596-3596/thenerdimite.squarecalculator D/AndroidRuntime: Shutting down VM
04-25 09:28:05.060 3596-3596/thenerdimite.squarecalculator W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa5007678)
04-25 09:28:05.060 3596-3596/thenerdimite.squarecalculator E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not find method Calculate(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btnCalculate'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
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)
04-25 09:28:06.740 3596-3596/? I/Process: Sending signal. PID: 3596 SIG: 9
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="thenerdimite.squarecalculator">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Using the documentation of Button
You see the method need to be public and accept a View
In order for this to work, the method must be public and accept a View as its only parameter.
So it should be
public void Calculate (View v){
Using the xml notation, of course, you should call it calculate and correct the XML
From what I see, the problem is that your button action is android:onClick="Calculate" but should be android:onClick="calculate"
you need to make the method public, protected and private methods are not accessible by xml.
public void calculate (View v){
EditText input = (EditText) findViewById(R.id.etInput);
TextView output = (TextView) findViewById(R.id.tvOutput);
int base = Integer.valueOf(input.getText().toString());
int result = base * base;
String formattedResult = String.format("%,d", result);
output.setText("Result: " + formattedResult);
}
Change android:onClick="Calculate" to android:onClick="calculate".
Also calculate method should be public.
In XML you have "Calculate" with capital "C" while method is called "calculate". Change XML and it should work.
https://developer.android.com/reference/android/widget/Button.html

Android application crashed

I'm doing an app for Android. I have a main activity (not the default MainActivity.java, other activity named HotelPresentacion.java that has 3 buttons for insert/check registers or exit the app).
If I touch the Registrar button, supposedly I can register, but the app stops unexpectedly. If I touch the Registros button I can visualize the registers of my app, but when I touch one register (a short touch or long short) for only visualize or edit the app again stops unexpectedly.
I modified my androidmanifest.xml to set HotelPresentacion.java as my default starting activity.
This is the code of the HotelPresentacion.java
package com.example.lab007;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class HotelPresentacion extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hotel_presentacion);
}
public void onReservar(View v){
Intent i=new Intent(HotelPresentacion.this, ReservacionFormulario.class);
startActivity(i);
}
public void onVer(View v){
Intent i=new Intent(HotelPresentacion.this, MainActivity.class);
startActivity(i);
}
public void onSalir(View v){
finish();
}
}
My androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lab007"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".HotelPresentacion"
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="com.example.lab007.ReservacionFormulario"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.lab007.MainActivity"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
LOGCAT
10-25 11:16:38.880: E/AndroidRuntime(4247): FATAL EXCEPTION: main
10-25 11:16:38.880: E/AndroidRuntime(4247): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lab007/com.example.lab007.ReservacionFormulario}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.os.Looper.loop(Looper.java:130)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.ActivityThread.main(ActivityThread.java:3687)
10-25 11:16:38.880: E/AndroidRuntime(4247): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 11:16:38.880: E/AndroidRuntime(4247): at java.lang.reflect.Method.invoke(Method.java:507)
10-25 11:16:38.880: E/AndroidRuntime(4247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-25 11:16:38.880: E/AndroidRuntime(4247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-25 11:16:38.880: E/AndroidRuntime(4247): at dalvik.system.NativeStart.main(Native Method)
10-25 11:16:38.880: E/AndroidRuntime(4247): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
10-25 11:16:38.880: E/AndroidRuntime(4247): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:212)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.Activity.setContentView(Activity.java:1657)
10-25 11:16:38.880: E/AndroidRuntime(4247): at com.example.lab007.ReservacionFormulario.onCreate(ReservacionFormulario.java:39)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-25 11:16:38.880: E/AndroidRuntime(4247): ... 11 more
The class ReservacionFormulario.java
package com.example.lab007;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;
public class ReservacionFormulario extends ListActivity{
private ComplejoDBAdapter dbAdapterComplejo;
private ComplejoSpinnerAdapter complejoSpinnerAdapter;
private int modo ;
private long id ;
private Reservacion reserva = new Reservacion(this);
private EditText nombre;
private EditText apellidos;
//private DatePicker fechaInicio;
//private DatePicker fechaFin;
private Spinner complejo ;
private Button boton_guardar;
private Button boton_cancelar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reservacion_formulario);
Intent intent = getIntent();
Bundle extra = intent.getExtras();
if (extra == null) return;
nombre = (EditText) findViewById(R.id.nombre);
apellidos = (EditText) findViewById(R.id.apellidos);
//fechaInicio = (DatePicker) findViewById(R.id.);
//fechaInicio = (DatePicker) findViewById(R.id.);
complejo = (Spinner) findViewById(R.id.complejo);
boton_guardar = (Button) findViewById(R.id.boton_guardar);
boton_cancelar = (Button) findViewById(R.id.boton_cancelar);
complejoSpinnerAdapter = new ComplejoSpinnerAdapter(this, Complejo.getAll(this, null));
complejo.setAdapter(complejoSpinnerAdapter);
if (extra.containsKey(ReservacionDBAdapter.C_COL_ID)){
id = extra.getLong(ReservacionDBAdapter.C_COL_ID);
consultar(id);
}
establecerModo(extra.getInt(ReservacionActivity.C_MODO));
boton_guardar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
guardar();
}
});
boton_cancelar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
cancelar();
}
});
}
private void establecerModo(int m)
{
this.modo = m ;
if (modo == ReservacionActivity.C_VISUALIZAR){
this.setTitle(nombre.getText().toString());
this.setEdicion(false);
}
else if (modo == ReservacionActivity.C_CREAR){
this.setTitle("Nuevo reservacion");
this.setEdicion(true);
}
else if (modo == ReservacionActivity.C_EDITAR){
this.setTitle("Editar reservacion");
this.setEdicion(true);
}
}
private void consultar(long id){
reserva = Reservacion.find(this, id);
nombre.setText(reserva.getNombre());
apellidos.setText(reserva.getApellidos());
complejo.setSelection(complejoSpinnerAdapter.getPositionById(reserva.getComplejoId()));
}
private void setEdicion(boolean opcion){
nombre.setEnabled(opcion);
apellidos.setEnabled(opcion);
complejo.setEnabled(opcion);
// Controlamos visibilidad de botonera
LinearLayout v = (LinearLayout) findViewById(R.id.botonera);
if (opcion)
v.setVisibility(View.VISIBLE);
else
v.setVisibility(View.GONE);
}
private void guardar(){
/*verificar si funciona*/
try{
if(nombre.length()<=0){
reserva.setNombre(nombre.getText().toString());
}
}catch(Exception e){
}
reserva.setApellidos(apellidos.getText().toString());
reserva.setComplejoId(complejo.getSelectedItemId());
reserva.save();
if (modo == ReservacionActivity.C_CREAR){
Toast.makeText(ReservacionFormulario.this, "Creado", Toast.LENGTH_SHORT).show();
}
else if (modo == ReservacionActivity.C_EDITAR){
Toast.makeText(ReservacionFormulario.this, "Modificado", Toast.LENGTH_SHORT).show();
}
setResult(RESULT_OK);
finish();
}
private void cancelar(){
setResult(RESULT_CANCELED, null);
finish();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
if (modo == ReservacionActivity.C_VISUALIZAR)
getMenuInflater().inflate(R.menu.reservacion_formulario_ver, menu);
else
getMenuInflater().inflate(R.menu.reservacion_formulario_editar, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()){
case R.id.menu_eliminar:
borrar(id);
return true;
case R.id.menu_cancelar:
cancelar();
return true;
case R.id.menu_guardar:
guardar();
return true;
case R.id.menu_editar:
establecerModo(ReservacionActivity.C_EDITAR);
return true;
}
return super.onMenuItemSelected(featureId, item);
}
private void borrar(final long id){
AlertDialog.Builder dialogEliminar = new AlertDialog.Builder(this);
dialogEliminar.setIcon(android.R.drawable.ic_dialog_alert);
dialogEliminar.setTitle("Eliminar");
dialogEliminar.setMessage("¿Desea eliminar?");
dialogEliminar.setCancelable(false);
dialogEliminar.setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int boton) {
reserva.delete();
Toast.makeText(ReservacionFormulario.this, "Eliminado", Toast.LENGTH_SHORT).show();
setResult(RESULT_OK);
finish();
}
});
dialogEliminar.setNegativeButton(android.R.string.no, null);
dialogEliminar.show();
}
}
The activity_reservacion_formulario.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<!-- Nombre -->
<TextView
android:id="#+id/label_nombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nombre" />
<EditText
android:id="#+id/nombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/label_nombre"
android:maxLength="40"
android:ems="10" />
<!-- Apellidos -->
<TextView
android:id="#+id/label_apellidos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/nombre"
android:maxLength="60"
android:text="Apellidos" />
<EditText
android:id="#+id/apellidos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/label_apellidos"
android:ems="10" />
<!-- Fecha de Inicio -->
<!-- <TextView
android:id="#+id/label_fechainicio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/apellidos"
android:text="Fecha de Inicio" />
<DatePicker
android:id="#+id/datePickerInicio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/label_fechainicio" /> -->
<!-- Fecha de Fin -->
<!-- <TextView
android:id="#+id/label_fechafin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/datePickerInicio"
android:text="Fecha de Fin" />
<DatePicker
android:id="#+id/datePickerFin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/label_fechafin" /> -->
<!-- Spinner -->
<TextView
android:id="#+id/label_complejo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/datePickerFin"
android:text="Complejo" />
<Spinner
android:id="#+id/complejo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/label_complejo" />
<TextView
android:id="#+id/label_ciudad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Complejo"
android:layout_below="#+id/complejo" />
<LinearLayout
android:id="#+id/botonera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:layout_below="#+id/datePickerFin"
android:orientation="horizontal" >
<Button
android:id="#+id/boton_cancelar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancelar" />
<Button
android:id="#+id/boton_guardar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Guardar" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
This is the entire project:
Project
I'll appreciante any help for solve my problem. Thanks
Replace
public class ReservacionFormulario extends ListActivity{
with
public class ReservacionFormulario extends Activity{
As you have extended the ListActivity so Android is searching for the a ListView with Id list, but In your XML there is no any such type of ListView.
From the crash log i guess its.
Change ListActivity to Activity

Unfortunately your "APP" has stopped working [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 8 years ago.
I am new to Android App development and I am having an issue with an app. I have read everything I could find on stackoverflow about the problem and checked my work against the suggestions from the other questions asked.
Thanks for the help.
Unfortunately application has stopped android emulator genymotion
I am trying to run the app on a Samsung Gal 3 tab.
Below are my files
MainActivity.java
package net.androidbootcamp.concerttickets;
import java.text.DecimalFormat;
import android.support.v7.app.ActionBarActivity;
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;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
double costPerTicket = 79.99;
int numberOfTickets;
double totalCost;
String groupChoice;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText tickets=(EditText)findViewById(R.id.txtTicket);
final Spinner group = (Spinner)findViewById(R.id.txtGroup);
Button cost = (Button)findViewById(R.id.btnCost);
cost.setOnClickListener(new OnClickListener() {
final TextView result = ((TextView)findViewById(R.id.txtResult));
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
numberOfTickets = Integer.parseInt(tickets.getText().toString());
totalCost = costPerTicket * numberOfTickets;
DecimalFormat currency = new DecimalFormat("$###,###.##");
groupChoice = group.getSelectedItem().toString();
result.setText("Total Cost for" + groupChoice + "is" + currency.format(totalCost));
}
});
}
#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);
}
}
ActivityMain.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="net.androidbootcamp.concerttickets.MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/txtTitle"
android:textSize="48sp" />
<EditText
android:id="#+id/txtTicket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtGroup"
android:layout_below="#+id/textView1"
android:ems="10"
android:hint="#string/txtTickets"
android:inputType="number"
android:textSize="32sp" >
<requestFocus />
</EditText>
<Spinner
android:id="#+id/txtGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTicket"
android:layout_centerHorizontal="true"
android:entries="#array/txtGroup"
android:prompt="#string/prompt" />
<Button
android:id="#+id/btnCost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtGroup"
android:layout_centerHorizontal="true"
android:text="#string/btnCost"
android:textSize="32sp" />
<TextView
android:id="#+id/txtResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnCost"
android:layout_centerHorizontal="true"
android:textSize="18sp" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtGroup"
android:layout_alignParentBottom="true"
android:layout_marginBottom="19dp"
android:contentDescription="#string/description"
android:src="#drawable/concert" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.androidbootcamp.concerttickets"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<activity
android:name="net.androidbootcamp.concerttickets.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>
</application>
</manifest>
LogCat
08-14 17:19:35.330: D/AndroidRuntime(20330): Shutting down VM
08-14 17:19:35.330: W/dalvikvm(20330): threadid=1: thread exiting with uncaught exception (group=0x41cf0e10)
08-14 17:19:35.340: E/AndroidRuntime(20330): FATAL EXCEPTION: main
08-14 17:19:35.340: E/AndroidRuntime(20330): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.androidbootcamp.concerttickets/net.androidbootcamp.concerttickets.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.app.ActivityThread.access$700(ActivityThread.java:150)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.os.Looper.loop(Looper.java:176)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.app.ActivityThread.main(ActivityThread.java:5279)
08-14 17:19:35.340: E/AndroidRuntime(20330): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 17:19:35.340: E/AndroidRuntime(20330): at java.lang.reflect.Method.invoke(Method.java:511)
08-14 17:19:35.340: E/AndroidRuntime(20330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-14 17:19:35.340: E/AndroidRuntime(20330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-14 17:19:35.340: E/AndroidRuntime(20330): at dalvik.system.NativeStart.main(Native Method)
08-14 17:19:35.340: E/AndroidRuntime(20330): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:110)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99)
08-14 17:19:35.340: E/AndroidRuntime(20330): at net.androidbootcamp.concerttickets.MainActivity.onCreate(MainActivity.java:26)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.app.Activity.performCreate(Activity.java:5267)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
08-14 17:19:35.340: E/AndroidRuntime(20330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
08-14 17:19:35.340: E/AndroidRuntime(20330): ... 11 more
The LogCat output shows that you aren't using the theme correctly. in particular, it wants you to use a compatibility theme. You can do that by altering/including a theme attribute for the activity in your manifest file.
If you're not actually going to use an action bar, your could also just change the parent of your activity from ActionbarActivity to just Activity like this:
public class MainActivity extends Activity {

whenever application is executed it is displayed as blank in android emulator

Below i have pasted copy of my java,xml and logcat of the application.
Here goes my java code
package my.example.myproject;
import my.example.myproject.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*
* #see SystemUiHider
*/
public class FullscreenActivity extends Activity {
/**
* Whether or not the system UI should be auto-hidden after
* {#link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
*/
private static final boolean AUTO_HIDE = true;
/**
* If {#link #AUTO_HIDE} is set, the number of milliseconds to wait after
* user interaction before hiding the system UI.
*/
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
/**
* If set, will toggle the system UI visibility upon interaction. Otherwise,
* will show the system UI visibility upon interaction.
*/
private static final boolean TOGGLE_ON_CLICK = true;
/**
* The flags to pass to {#link SystemUiHider#getInstance}.
*/
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
/**
* The instance of the {#link SystemUiHider} for this activity.
*/
private SystemUiHider mSystemUiHider;
Button iol,help,about;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
final View controlsView = findViewById(R.id.fullscreen_content_controls);
iol = (Button)findViewById(R.id.iolCalculation);
help = (Button)findViewById(R.id.sHelp);
about = (Button)findViewById(R.id.sAbout);
Thread iolthread=new Thread(){
public void run(){
try{
iol.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.selection);
}
});
Intent iolIntent=new Intent(FullscreenActivity.this,Selection.class);
startActivity(iolIntent);
}
finally{
finish();
}
}
};
iolthread.start();
// Set up an instance of SystemUiHider to control the system UI for
// this activity.
mSystemUiHider = SystemUiHider.getInstance(this, controlsView,
HIDER_FLAGS);
mSystemUiHider.setup();
mSystemUiHider
.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
// Cached values.
int mControlsHeight;
int mShortAnimTime;
#Override
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void onVisibilityChange(boolean visible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
// If the ViewPropertyAnimator API is available
// (Honeycomb MR2 and later), use it to animate the
// in-layout UI controls at the bottom of the
// screen.
if (mControlsHeight == 0) {
mControlsHeight = controlsView.getHeight();
}
if (mShortAnimTime == 0) {
mShortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
controlsView
.animate()
.translationY(visible ? 0 : mControlsHeight)
.setDuration(mShortAnimTime);
} else {
// If the ViewPropertyAnimator APIs aren't
// available, simply show or hide the in-layout UI
// controls.
controlsView.setVisibility(visible ? View.VISIBLE
: View.GONE);
}
if (visible && AUTO_HIDE) {
// Schedule a hide().
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
}
});
// Set up the user interaction to manually show or hide the system UI.
controlsView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
} else {
mSystemUiHider.show();
}
}
});
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
delayedHide(100);
}
/**
* Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away
* while interacting with activity UI.
*/
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
#Override
public void run() {
mSystemUiHider.hide();
}
};
/**
* Schedules a call to hide() in [delay] milliseconds, canceling any
* previously scheduled calls.
*/
private void delayedHide(int delayMillis) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
}
XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
tools:context=".FullscreenActivity"
android:id="#+id/frame_layout">
<LinearLayout
android:id="#+id/fullscreen_content_controls"
style="?buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="#color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent" >
</LinearLayout>
<Button
style="#style/ButtonBarButton"
android:layout_width="222dp"
android:layout_height="62dp"
android:layout_marginLeft="45dp"
android:layout_marginTop="75dp"
android:background="#color/black"
android:paddingLeft="20dp"
android:id="#+id/iolCalculation"
android:text="#string/Button"
android:textColor="#color/WhiteSmoke"
android:textStyle="bold" />
<Button
style="#style/ButtonBarButton"
android:layout_width="222dp"
android:layout_height="62dp"
android:layout_marginLeft="45dp"
android:id="#+id/sHelp"
android:layout_marginTop="139dp"
android:background="#color/Black"
android:gravity="center"
android:paddingLeft="10dp"
android:text="#string/Button1"
android:textColor="#color/WhiteSmoke"
android:textStyle="bold" />
<Button
style="#style/ButtonBarButton"
android:layout_width="222dp"
android:layout_height="62dp"
android:id="#+id/sAbout"
android:layout_marginLeft="45dp"
android:layout_marginTop="203dp"
android:background="#color/Black"
android:paddingLeft="10dp"
android:text="#string/Button2"
android:textColor="#color/WhiteSmoke"
android:textStyle="bold" >
</Button>
</FrameLayout>
logcat
12-16 10:48:26.975: E/Trace(767): error opening trace file: No such file or directory (2)
12-16 10:51:38.199: E/AndroidRuntime(767): FATAL EXCEPTION: main
12-16 10:51:38.199: E/AndroidRuntime(767): java.lang.NullPointerException
12-16 10:51:38.199: E/AndroidRuntime(767): at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:168)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.view.View.performClick(View.java:4202)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.view.View$PerformClick.run(View.java:17340)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.os.Handler.handleCallback(Handler.java:725)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.os.Looper.loop(Looper.java:137)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-16 10:51:38.199: E/AndroidRuntime(767): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 10:51:38.199: E/AndroidRuntime(767): at java.lang.reflect.Method.invoke(Method.java:511)
12-16 10:51:38.199: E/AndroidRuntime(767): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-16 10:51:38.199: E/AndroidRuntime(767): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-16 10:51:38.199: E/AndroidRuntime(767): at dalvik.system.NativeStart.main(Native Method)
i have tried every possible solution in stackoverflow but in vain.help me. I think mostly there is a problem in xml to java display content.Also do i need to put in id for every layout in xml and how does the java code detect the layout to be displayed
here goes my selection java code and xml
package com.example.iolcalci;
import android.app.Activity;
import android.os.Bundle;
public class Selection extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.selective);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:orientation="vertical" >
<Spinner
android:id="#+id/formulae"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="#color/LightSkyBlue"
android:prompt="#string/prompt"
android:entries="#array/formulas" />
<TextView
android:id="#+id/k2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/k1"
android:layout_below="#+id/k1"
android:layout_marginTop="38dp"
android:text="#string/K2"
android:textColor="#color/White"
android:textSize="25sp" />
<TextView
android:id="#+id/al_const"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/al"
android:layout_below="#+id/al"
android:layout_marginTop="44dp"
android:text="#string/Rx"
android:textColor="#color/White"
android:textSize="20sp" />
<TextView
android:id="#+id/al"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/k2"
android:layout_below="#+id/k2"
android:layout_marginTop="38dp"
android:text="#string/AL"
android:textColor="#color/White"
android:textSize="25sp" />
<EditText
android:id="#+id/k2_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/al"
android:layout_alignRight="#+id/al_editText"
android:layout_alignTop="#+id/k2"
android:layout_marginLeft="120dp"
android:background="#color/black"
android:ems="10"
android:inputType="numberDecimal"
android:textColorLink="#color/white" />
<EditText
android:id="#+id/al_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/formulae"
android:layout_alignTop="#+id/al"
android:layout_marginLeft="120dp"
android:ems="10"
android:inputType="numberDecimal" />
<EditText
android:id="#+id/al_const_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/al_const"
android:layout_alignRight="#+id/al_editText"
android:layout_marginLeft="120dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="#+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#color/LightSkyBlue"
android:text="#string/Result" />
<EditText
android:id="#+id/k1_editText"
android:layout_width="wrap_content"
android:layout_height="37dp"
android:layout_alignLeft="#+id/k2_editText"
android:layout_alignRight="#+id/formulae"
android:layout_below="#+id/formulae"
android:layout_marginTop="30dp"
android:background="#color/black"
android:ems="10"
android:inputType="numberDecimal"
android:textColorLink="#color/White" />
<TextView
android:id="#+id/k1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/k1_editText"
android:layout_alignParentLeft="true"
android:layout_marginLeft="27dp"
android:text="#string/K1"
android:textColor="#color/white"
android:textSize="25sp" />
</RelativeLayout>
THE NEW LOGCAT
12-21 10:44:50.419: W/Trace(766): Unexpected value from nativeGetEnabledTags: 0
12-21 10:44:50.573: W/Trace(766): Unexpected value from nativeGetEnabledTags: 0
12-21 10:44:50.573: W/Trace(766): Unexpected value from nativeGetEnabledTags: 0
12-21 10:44:50.923: D/AndroidRuntime(766): Shutting down VM
12-21 10:44:50.923: W/dalvikvm(766): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-21 10:44:50.933: E/AndroidRuntime(766): FATAL EXCEPTION: main
12-21 10:44:50.933: E/AndroidRuntime(766): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iolcalci/com.example.iolcalci.MainActivity}: java.lang.NullPointerException
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.os.Looper.loop(Looper.java:137)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-21 10:44:50.933: E/AndroidRuntime(766): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 10:44:50.933: E/AndroidRuntime(766): at java.lang.reflect.Method.invoke(Method.java:511)
12-21 10:44:50.933: E/AndroidRuntime(766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-21 10:44:50.933: E/AndroidRuntime(766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-21 10:44:50.933: E/AndroidRuntime(766): at dalvik.system.NativeStart.main(Native Method)
12-21 10:44:50.933: E/AndroidRuntime(766): Caused by: java.lang.NullPointerException
12-21 10:44:50.933: E/AndroidRuntime(766): at com.example.iolcalci.MainActivity.onCreate(MainActivity.java:23)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.Activity.performCreate(Activity.java:5104)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-21 10:44:50.933: E/AndroidRuntime(766): ... 11 more
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.iolcalci"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.iolcalci.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="Selection"
android:exported="false"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.example.iolcalci.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Judging from your code I'm going to go with the conjecture that this line is causing the error:
setContentView(R.id.selection);
There's no need to change the contentView of the activity from which you're calling the "selection" activity. You should read up on intents. I'm assuming that when the button is clicked, you want the new activity and it's content to pop up. For that, do this :
try{
iol.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent iolIntent=new Intent(FullscreenActivity.this,Selection.class);
startActivity(iolIntent);
}
});
}
Also set your button's layout width and layout height to "wrap_content". Definite measurements won't work well because there are many resolutions possible.
android:layout_height="wrap_content"
android:layout_width="wrap_content"
In case of more errors, post the new logcat, please.
I found this in your selection.java:
setContentView(R.layout.selective);
This means you should have a selective.xml file in your res/layout/ folder.
This selective.xml file should contain a definition of EditText.
Example,
<EditText
android:id="#+id/k1_editText"
android:layout_width="wrap_content"
android:layout_height="37dp"
android:layout_alignLeft="#+id/k2_editText"
android:layout_alignRight="#+id/formulae"
android:layout_below="#+id/formulae"
android:layout_marginTop="30dp"
android:background="#color/black"
android:ems="10"
android:inputType="numberDecimal"
android:textColorLink="#color/White" />
You need to reference this in the activity file with this piece of code:
EditText ed=new EditText findViewById(R.id.k2_editText);
Now the EditText field will be displayed.
If you want to make your EditText object global, declare it in the class and then initialize it after setting content view. Like this,
public class Selection extends Activity {
EditText ed;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.selective);
EditText ed=new EditText findViewById(R.id.k2_editText);
}
Make sure you always initialize a widget object AFTER the creation of the activity and setting content view, because otherwise the compiler is going to have to initialize something which hasn't been referenced yet. The layout file is referenced only after setting content view. Only after setting the layout file can you initialize objects to whatever is present inside the layout file.
If you like the answer, do vote it up.

MapView: Could not find class A referenced from method B

There are 3 buttons on the screen! Start, View Map, Stop
When I click View Map, it should go to a new screen that shows the map! But something goes wrong and the app is getting force closed! I keep getting Could not find class A referenced from method B error. Please please please someone correct it. I've been struck with this for 3 days!!! :'(
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="GPS App"
android:id="#+id/textView1"
android:textSize="40sp"
android:padding="10dp"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<Button
android:text="Start"
android:id="#+id/buttonStart"
android:textSize="30sp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Button
android:text="View Map"
android:id="#+id/buttonMap"
android:textSize="30sp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Button
android:text="Stop"
android:id="#+id/buttonStop"
android:textSize="30sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
map.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mymap"
android:clickable="true"
android:enabled="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="xxxxxxxxxxxxx"
/>
<LinearLayout
android:id="#+id/myzoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Firstdroid.Gps"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<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>
<service android:permission="android.permission.INTERNET"
android:name=".IntentService" android:enabled="true" />
<activity android:name=".MapViewer" android:label="#string/app_name" >
<uses-library android:name="com.google.android.maps" />
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>
MainActivity.java
package Firstdroid.Gps;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* START BUTTON */
Button startButton = (Button) findViewById(R.id.buttonStart);
startButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
Log.d("Firstdroid.Gps", "Starting Exploration..");
// Start Exploration
startService(new Intent(MainActivity.this, GPSexp.class));
}
});
/* MAP BUTTON */
Button mapButton = (Button) findViewById(R.id.buttonMap);
mapButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
Log.d("Firstdroid.Gps", "Loading Map..");
// Loading Google Map View
startService(new Intent(MainActivity.this, MapViewer.class));
}
});
/* STOP BUTTON */
Button stopButton = (Button) findViewById(R.id.buttonStop);
stopButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Log.d("Firstdroid.Gps", "Stopping Exploration..");
// Stop Exploration
stopService(new Intent(MainActivity.this, GPSexp.class));
}
});
}
}/* End of MainActivity */
The start and stop buttons refer to GPSexp.class defined in GPSexp.java which works correctly. Problem is with MapViewer.java
MapViewer.java
package Firstdroid.Gps;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MapViewer extends MapActivity {
MapView myMap;
MyLocationOverlay myLocOverlay;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
initMap();
initMyLocation();
}
private void initMap() {
myMap = (MapView) findViewById(R.id.mymap);
View zoomView = myMap.getZoomControls();
LinearLayout myzoom = (LinearLayout) findViewById(R.id.myzoom);
myzoom.addView(zoomView);
myMap.displayZoomControls(true);
}
/**
* Initialises the MyLocationOverlay and adds it to the overlays of the map
*/
private void initMyLocation() {
myLocOverlay = new MyLocationOverlay(this, myMap);
myLocOverlay.enableMyLocation();
myMap.getOverlays().add(myLocOverlay);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
Logcat
06-30 04:25:07.519: WARN/dalvikvm(357): Unable to resolve superclass of LFirstdroid/Gps/MapViewer; (37)
06-30 04:25:07.519: WARN/dalvikvm(357): Link of class 'LFirstdroid/Gps/MapViewer;' failed
06-30 04:25:07.547: ERROR/dalvikvm(357): Could not find class 'Firstdroid.Gps.MapViewer', referenced from method Firstdroid.Gps.MainActivity$2.onClick
06-30 04:25:07.547: WARN/dalvikvm(357): VFY: unable to resolve const-class 8 (LFirstdroid/Gps/MapViewer;) in LFirstdroid/Gps/MainActivity$2;
06-30 04:25:07.559: DEBUG/dalvikvm(357): VFY: replacing opcode 0x1c at 0x000d
06-30 04:25:07.573: DEBUG/dalvikvm(357): VFY: dead code 0x000f-0015 in LFirstdroid/Gps/MainActivity$2;.onClick (Landroid/view/View;)V
06-30 04:25:07.909: INFO/ActivityManager(58): Displayed activity Firstdroid.Gps/.MainActivity: 1828 ms (total 1828 ms)
06-30 04:25:13.180: DEBUG/dalvikvm(129): GC_EXPLICIT freed 670 objects / 38560 bytes in 169ms
06-30 04:25:13.840: DEBUG/Firstdroid.Gps(357): Loading Map..
06-30 04:25:13.850: DEBUG/AndroidRuntime(357): Shutting down VM
06-30 04:25:13.850: WARN/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): FATAL EXCEPTION: main
06-30 04:25:13.880: ERROR/AndroidRuntime(357): java.lang.NoClassDefFoundError: Firstdroid.Gps.MapViewer
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at Firstdroid.Gps.MainActivity$2.onClick(MainActivity.java:40)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.view.View.performClick(View.java:2408)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.view.View$PerformClick.run(View.java:8816)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.os.Handler.handleCallback(Handler.java:587)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.os.Handler.dispatchMessage(Handler.java:92)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.os.Looper.loop(Looper.java:123)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at java.lang.reflect.Method.invokeNative(Native Method)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at java.lang.reflect.Method.invoke(Method.java:521)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at dalvik.system.NativeStart.main(Native Method)
06-30 04:25:13.900: WARN/ActivityManager(58): Force finishing activity Firstdroid.Gps/.MainActivity
06-30 04:25:14.450: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{44fe90c0 Firstdroid.Gps/.MainActivity}
06-30 04:25:15.750: INFO/Process(357): Sending signal. PID: 357 SIG: 9
06-30 04:25:15.841: INFO/ActivityManager(58): Process Firstdroid.Gps (pid 357) has died.
06-30 04:25:15.880: INFO/WindowManager(58): WIN DEATH: Window{4500c988 Firstdroid.Gps/Firstdroid.Gps.MainActivity paused=false}
You added uses-library as child of the activity element... maybe that's the problem.
This is not a standard package in the
Android library. In order to use it,
you must add the following XML
element, as a child of the
application element, in your AndroidManifest.xml file:
<uses-library android:name="com.google.android.maps" />
You're using startService to start your MapViewer activity. Hope that helps

Categories