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
Related
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
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 {
I am creating an android app for an University. I am trying to display a Location map in one activity by clicking a button in other activity. I have referred some links and worked, but I am getting error. Here is my code snippet and the Logcat. Please anybody help me in making this work.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.university"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<permission
android:name="com.example.university.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.university.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:glEsVersion="0x00000000" android:required="true" />
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDGPcb88FGrYilWxe41RbWog38pSgNfp5o" />
<application android:allowBackup="true" android:icon="#drawable/launcher"
android:theme="#style/Theme.AppCompat.Light" >
<activity android:name=".MainActivity" android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".About" android:label="#string/title_activity_about" >
</activity>
<activity android:name=".Chancellor" android:label="#string/title_activity_chancellor" >
</activity>
<activity android:name=".Prochancellor" android:label="#string/title_activity_prochancellor" >
</activity>
<activity android:name=".Programs" android:label="#string/title_activity_programs" >
</activity>
<activity android:name=".Contact" android:label="#string/title_activity_contact" >
</activity>
<activity android:name=".SplashActivity" android:label="#string/title_activity_splash" >
</activity>
<activity android:name=".Admissions" android:label="#string/title_activity_admissions" >
</activity>
<activity android:name=".Map" android:label="#string/title_activity_map" >
</activity>
</application>
</manifest>
THIS .JAVA AND .XML FILES FOR BUTTON AND ONCLICK FUNCTION TO WORK
Contact.java
package com.example.university;
import android.support.v7.app.ActionBarActivity;
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.view.View.OnClickListener;
public class Contact extends ActionBarActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_contact.xml
setContentView(R.layout.activity_contact);
// Locate the button in activity_contact.xml
button = (Button)findViewById(R.id.button1);
// Capture button clicks
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(Contact.this,
Map.class);
startActivity(myIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.contact, 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.
switch (item.getItemId()) {
case R.id.about:
Intent a = new Intent(getApplicationContext(), About.class);
startActivity(a);
return true;
case R.id.chancellor:
Intent b = new Intent(getApplicationContext(), Chancellor.class);
startActivity(b);
return true;
case R.id.prochancellor:
Intent c = new Intent(getApplicationContext(), Prochancellor.class);
startActivity(c);
return true;
case R.id.programs:
Intent d = new Intent(getApplicationContext(), Programs.class);
startActivity(d);
return true;
case R.id.admissions:
Intent e = new Intent(getApplicationContext(), Admissions.class);
startActivity(e);
return true;
case R.id.contact:
Intent f = new Intent(getApplicationContext(), Contact.class);
startActivity(f);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
activity_contact.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:background="#drawable/tiny_grid"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.university.Contact" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#drawable/campus1" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView1"
android:layout_marginLeft="26dp"
android:layout_toRightOf="#+id/imageView1"
android:text="100 Feet Ring Road, BSK III Stage, Bangalore-560085"
android:textSize="11dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:text="+91 80 26721983"
android:textSize="11dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:text="+91 80 26722108"
android:textSize="11dp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/textView3"
android:autoLink="email"
android:text="admissions#pes.edu"
android:textSize="11dp" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_marginTop="20dp"
android:layout_toLeftOf="#+id/textView1"
android:src="#drawable/campus2" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_alignTop="#+id/imageView2"
android:text=" Hosur Road Campus (1 Km before Electronic City), Bangalore - 560 100"
android:textSize="11dp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView5"
android:layout_below="#+id/textView5"
android:text="+91 80 66186610"
android:textSize="11dp" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/textView6"
android:text="+91 80 66186611"
android:textSize="11dp" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView7"
android:layout_below="#+id/textView7"
android:autoLink="email"
android:text="admissions#pes.edu"
android:textSize="11dp" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView2"
android:layout_marginTop="25dp"
android:layout_toLeftOf="#+id/textView1"
android:src="#drawable/campus3" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView8"
android:layout_alignTop="#+id/imageView3"
android:text="National Highway 219, Kuppam, Andhra Pradesh - 517 425"
android:textSize="11dp" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView9"
android:layout_below="#+id/textView9"
android:text="085 70 256736"
android:textSize="11dp" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView10"
android:layout_below="#+id/textView10"
android:autoLink="email"
android:text="admissions#pes.edu"
android:textSize="11dp" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView3"
android:layout_marginTop="20dp"
android:layout_toLeftOf="#+id/textView1"
android:src="#drawable/campus4" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView11"
android:layout_alignTop="#+id/imageView4"
android:text="100 Feet Ring Road, BSK III Stage, Bangalore-560085"
android:textSize="11dp" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView12"
android:layout_below="#+id/textView12"
android:text="+91 80 26721983"
android:textSize="11dp" />
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView13"
android:layout_below="#+id/textView13"
android:text="+91 80 26722108"
android:textSize="11dp" />
<TextView
android:id="#+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView14"
android:layout_below="#+id/textView14"
android:autoLink="email"
android:text="admissions#pes.edu"
android:textSize="11dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView4"
android:layout_alignParentBottom="true"
android:layout_marginLeft="16dp"
android:text="Button" />
</RelativeLayout>
THIS .JAVA NAD .XML FILES TO DISPLAY MAP
package com.example.university;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.widget.Toast;
public class Map extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS",
Toast.LENGTH_LONG).show();
}else{
}
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS",
Toast.LENGTH_LONG).show();
Log.v(">>>>>>>>>>>>>.", "successs");
}else{
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map, 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.
switch (item.getItemId()) {
case R.id.about:
Intent a = new Intent(getApplicationContext(), About.class);
startActivity(a);
return true;
case R.id.chancellor:
Intent b = new Intent(getApplicationContext(), Chancellor.class);
startActivity(b);
return true;
case R.id.prochancellor:
Intent c = new Intent(getApplicationContext(), Prochancellor.class);
startActivity(c);
return true;
case R.id.programs:
Intent d = new Intent(getApplicationContext(), Programs.class);
startActivity(d);
return true;
case R.id.admissions:
Intent e = new Intent(getApplicationContext(), Admissions.class);
startActivity(e);
return true;
case R.id.contact:
Intent f = new Intent(getApplicationContext(), Contact.class);
startActivity(f);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Aactivity_map.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.university.Map" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Here is the Logcat when i click on the button in activity_contact.xml
07-26 02:22:16.012: I/Process(2244): Sending signal. PID: 2244 SIG: 9
07-26 02:22:20.062: D/dalvikvm(2268): GC_FOR_ALLOC freed 86K, 7% free 2998K/3200K, paused 221ms, total 269ms
07-26 02:22:20.292: I/dalvikvm-heap(2268): Grow heap (frag case) to 6.783MB for 3932172-byte allocation
07-26 02:22:20.372: D/dalvikvm(2268): GC_FOR_ALLOC freed 2K, 3% free 6835K/7044K, paused 79ms, total 79ms
07-26 02:22:21.552: D/gralloc_goldfish(2268): Emulator without GPU emulation detected.
07-26 03:10:59.626: D/dalvikvm(2376): GC_FOR_ALLOC freed 90K, 7% free 2997K/3204K, paused 191ms, total 202ms
07-26 03:10:59.876: I/dalvikvm-heap(2376): Grow heap (frag case) to 6.782MB for 3932172-byte allocation
07-26 03:11:00.126: D/dalvikvm(2376): GC_FOR_ALLOC freed 2K, 4% free 6835K/7048K, paused 248ms, total 248ms
07-26 03:11:07.206: D/gralloc_goldfish(2376): Emulator without GPU emulation detected.
07-26 03:11:09.626: D/dalvikvm(2376): GC_FOR_ALLOC freed 57K, 3% free 7068K/7260K, paused 31ms, total 36ms
07-26 03:11:09.896: I/Choreographer(2376): Skipped 58 frames! The application may be doing too much work on its main thread.
07-26 03:11:10.356: I/Choreographer(2376): Skipped 38 frames! The application may be doing too much work on its main thread.
07-26 03:11:21.527: D/dalvikvm(2376): GC_FOR_ALLOC freed 1266K, 17% free 7055K/8436K, paused 173ms, total 176ms
07-26 03:11:25.347: D/AndroidRuntime(2376): Shutting down VM
07-26 03:11:25.357: W/dalvikvm(2376): threadid=1: thread exiting with uncaught exception (group=0xb0cefb20)
07-26 03:11:25.397: E/AndroidRuntime(2376): FATAL EXCEPTION: main
07-26 03:11:25.397: E/AndroidRuntime(2376): Process: com.example.university, PID: 2376
07-26 03:11:25.397: E/AndroidRuntime(2376): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.university/com.example.university.Map}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.os.Handler.dispatchMessage(Handler.java:102)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.os.Looper.loop(Looper.java:136)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-26 03:11:25.397: E/AndroidRuntime(2376): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 03:11:25.397: E/AndroidRuntime(2376): at java.lang.reflect.Method.invoke(Method.java:515)
07-26 03:11:25.397: E/AndroidRuntime(2376): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-26 03:11:25.397: E/AndroidRuntime(2376): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-26 03:11:25.397: E/AndroidRuntime(2376): at dalvik.system.NativeStart.main(Native Method)
07-26 03:11:25.397: E/AndroidRuntime(2376): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-26 03:11:25.397: E/AndroidRuntime(2376): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.Activity.setContentView(Activity.java:1929)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:217)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:77)
07-26 03:11:25.397: E/AndroidRuntime(2376): at com.example.university.Map.onCreate(Map.java:20)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.Activity.performCreate(Activity.java:5231)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-26 03:11:25.397: E/AndroidRuntime(2376): ... 11 more
07-26 03:11:25.397: E/AndroidRuntime(2376): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.Fragment.instantiate(Fragment.java:597)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.Fragment.instantiate(Fragment.java:561)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.Activity.onCreateView(Activity.java:4778)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
07-26 03:11:25.397: E/AndroidRuntime(2376): ... 24 more
07-26 03:11:25.397: E/AndroidRuntime(2376): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.SupportMapFragment" on path: DexPathList[[zip file "/data/app/com.example.university-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.university-2, /system/lib]]
07-26 03:11:25.397: E/AndroidRuntime(2376): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
07-26 03:11:25.397: E/AndroidRuntime(2376): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
07-26 03:11:25.397: E/AndroidRuntime(2376): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.Fragment.instantiate(Fragment.java:583)
07-26 03:11:25.397: E/AndroidRuntime(2376): ... 27 more
You are doing a few things wrong as you probably didn't follow all the steps in using Google Maps on your application:
Google Maps API key is supposed to be inside of the application tags in your manifest.
You are missing the play services meta tag (also inside the application tags):
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
Also make sure that you are compiling the play services with your project. If you're using Android-Studio, then the bottom of your build.gradle file should look like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:20.+'
compile 'com.google.android.gms:play-services:5.0.77'
}
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".
need some help with creating my main menu. I have successfully made one on click listener that switches to a different activity but im having troubles getting the second one to work. The application runs fine in the simulation but when i go to click the weight gin button nothing happens. I just cant seem to find the problem
package com.example.bmiworking;
import android.app.Activity;
import android.content.DialogInterface;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainMenu extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button btn,btn1,btn2,btn3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
btn = (Button) findViewById(R.id.buttonBMI);
btn.setOnClickListener(this);
btn1 = (Button) findViewById(R.id.buttonGain);
btn1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.buttonBMI) {
startActivity(new Intent(this, MainActivity.class));
}
}
public void onClick1(View d) {
if (d.getId() == R.id.buttonGain) {
startActivity(new Intent(this, weightgain.class));
}
}
}
package com.example.bmiworking;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class weightgain extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weightgain);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".StartingPoint"
android:background="#drawable/grunge">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Main Menu"
android:textSize="45dp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay"/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="BMI Goal"
android:layout_gravity="center"
android:textSize="20dp"
android:id="#+id/bSub"/>
<Button
android:id="#+id/buttonBMI"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="BMI Calculator"
android:textSize="20dp" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Muscle Loss Workouts"
android:layout_gravity="center"
android:textSize="20dp"
android:id="#+id/bSub"/>
<Button
android:id="#+id/buttonGain"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Muscle Gain Workouts"
android:textSize="20dp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bmiworking"
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.bmiworking.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="com.example.bmiworking.MainMenu"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.bmiworking.weightgain"
android:label="#string/app_name" >
</activity>
</application>
Edit with stack trace:
01-24 02:34:34.739: D/dalvikvm(808): newInstance failed: p0 i0 [0 a1
01-24 02:34:34.754: D/AndroidRuntime(808): Shutting down VM
01-24 02:34:34.754: W/dalvikvm(808): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
01-24 02:34:34.819: E/AndroidRuntime(808): FATAL EXCEPTION: main
01-24 02:34:34.819: E/AndroidRuntime(808): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.bmiworking/com.example.bmiworking.MainMenu}: java.lang.InstantiationException: can't instantiate class com.example.bmiworking.MainMenu
01-24 02:34:34.819: E/AndroidRuntime(808): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
01-24 02:34:34.819: E/AndroidRuntime(808): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-24 02:34:34.819: E/AndroidRuntime(808): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-24 02:34:34.819: E/AndroidRuntime(808): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-24 02:34:34.819: E/AndroidRuntime(808): at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 02:34:34.819: E/AndroidRuntime(808): at android.os.Looper.loop(Looper.java:137)
01-24 02:34:34.819: E/AndroidRuntime(808): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-24 02:34:34.819: E/AndroidRuntime(808): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 02:34:34.819: E/AndroidRuntime(808): at java.lang.reflect.Method.invoke(Method.java:511)
01-24 02:34:34.819: E/AndroidRuntime(808): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-24 02:34:34.819: E/AndroidRuntime(808): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-24 02:34:34.819: E/AndroidRuntime(808): at dalvik.system.NativeStart.main(Native Method)
01-24 02:34:34.819: E/AndroidRuntime(808): Caused by: java.lang.InstantiationException: can't instantiate class com.example.bmiworking.MainMenu
01-24 02:34:34.819: E/AndroidRuntime(808): at java.lang.Class.newInstanceImpl(Native Method)
01-24 02:34:34.819: E/AndroidRuntime(808): at java.lang.Class.newInstance(Class.java:1319)
01-24 02:34:34.819: E/AndroidRuntime(808): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
01-24 02:34:34.819: E/AndroidRuntime(808): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
01-24 02:34:34.819: E/AndroidRuntime(808): ... 11 more
change onClick code as using switch case instead of if-else :
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.buttonBMI:
startActivity(new Intent(this, MainActivity.class));
break;
case R.id.buttonGain:
startActivity(new Intent(this, weightgain.class));
break;
}
}
Change it so it is like this:
#Override
public void onClick(View v) {
if (v.getId() == R.id.buttonBMI) {
startActivity(new Intent(this, MainActivity.class));
}
if (v.getId() == R.id.buttonGain) {
startActivity(new Intent(this, weightgain.class));
}
}
You only really override onClick(View v) and so, both button presses make onClick(View v) called (onClick1() never gets used). Then since you have an if, the second button doesn't get to start anything. Of course, if you have a lot of buttons, consider using a switch statement (like ρяσѕρєяK outlined). However, the idea is the same, you only use 1 method.
I think you missed something like the correct implement about the Intent and the OnClick method. Please try this on replacinging your code into mine and you will get a good result;
#Override
public void onClick(View v) {
if (v.equals(btn)) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
} else if (v.equals(btn1)) {
Intent intent = new Intent(this, weightgain.class);
startActivity(intent);
}
}
In above, "btn" and "btn1" are your buttons.
Please let me know your result.
First Change onClick code as,
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.buttonBMI:
startActivity(new Intent(MainMenu.this, MainActivity.class));
break;
case R.id.buttonGain:
startActivity(new Intent(MainMenu.this, weightgain.class));
break;
}
}
Dont pass this when creating new instance, pass it as ActivityName.this
You Can Try This Code
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.buttonBMI:
startActivity(new Intent(MainMenu.this, MainActivity.class));
break;
case R.id.buttonGain:
startActivity(new Intent(MainMenu.this, weightgain.class));
break;
}
}
Or
Try This 2nd Approach
public void buttonBMIClick(View view){
startActivity(new Intent(MainMenu.this, MainActivity.class));
}
public void buttonGainClick(View view){
startActivity(new Intent(MainMenu.this, weightgain.class));
}
and make you layout XML
<Button
android:id="#+id/buttonBMI"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="BMI Calculator"
android:onClick="buttonBMIClick"
android:textSize="20dp" />
<Button
android:id="#+id/buttonGain"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Muscle Gain Workouts"
android:onClick="buttonGainClick"
android:textSize="20dp" />