App crash on setContentView() of second activity - java

I have the launch activity called MainActivity that will eventually start another called SearchResultsActivity.
They both extend ActionBarActivity, and the second is yet to be written, for now it is just a stub with:
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.search_results);
}
The layout XML is as follows, just a ListView in a white background:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_products"
android:layout_gravity="center_horizontal" />
</LinearLayout>
The app is crashing on setContentView() with the following logcat:
17348-17348/com.example.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.SearchResultsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1666)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1682)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:940)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3714)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
at com.example.myapp.SearchResultsActivity.onCreate(SearchResultsActivity.java:42) <-- this is setContentView()
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1630)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1682)
            at android.app.ActivityThread.access$1500(ActivityThread.java:121)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:940)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3714)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
            at dalvik.system.NativeStart.main(Native Method)
Here's the manifest excerpt:
<activity
android:name="com.example.myapp.MainActivity"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Styled">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myapp.SearchResultsActivity"
android:label="#string/app_name"
android:theme="#style/Theme.Styled"
android:configChanges="orientation|keyboardHidden">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myapp.MainActivity" />
</activity>
I thought it could be some issue with the layout, but then I decide to replace R.layout.search_results with R.layout.home, which is the same XML used in MainActivity, therefore should work. But it crashes in the very same line.
Any ideas on what could be causing this?

As others have pointed out, your error is likely in your onCreate code for your second activity. If you read the documentation here you'll see they start their activity off with onCreate and an immediate call back to super.onCreate(...)
Your code should be as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //added this line
setContentView(R.layout.search_results);
}
If you chose to have your IDE automatically generate an Activity stub, it will always place the call to super.onCreate in your activity. It initializes the activity, see this question for more of an explanation.
If this doesn't fix your problem and you have more code in your second activity, it might be useful to post code for the entire activity.

Related

Error inflating class fragment in simple Google Maps project [duplicate]

This question already has answers here:
Error Inflating class fragment in activity maps xml
(3 answers)
Closed 6 years ago.
I just wanted to run a simple map fragment but I keep getting InflateException. I also asked a similar question but I made a new Google Maps project for simplicity.
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: Process: com.example.ali.myapplication, PID: 1785
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ali.myapplication/com.example.ali.myapplication.MapsActivity}: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
MapsActivity.java:
package com.example.ali.myapplication;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
activity_maps.xml:
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#619ec9"
xmlns:android="http://schemas.android.com/apk/res/android">
>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map" tools:context=".MapsActivity"
class="com.google.android.gms.maps.SupportMapFragment"
tools:layout="#layout/activity_maps"
android:layout_width="362dp"
android:layout_height="323dp"
/>
</LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ali.myapplication" >
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Fragment should be added to a frame layour, it cant be directly used.
Update your code and let me know if works

Getting error "java.lang.RuntimeException: Unable to start activity" in my app

My application worked yesterday, I cant figure out why it stopped working. The only thing I tried doing was to create a signed apk. to do this I had to create a new Google maps key and place it into the manifest file. I done this following the Google developers tutorial. But now when I try to run the app it start up, I click a button and it force closes.
This is the error log:
03-20 23:31:33.636 10036-10036/project.sharethefare E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: project.sharethefare, PID: 10036
java.lang.RuntimeException: Unable to start activity ComponentInfo{project.sharethefare/project.sharethefare.CurrentLocation}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5834)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428)
at android.app.Activity.setContentView(Activity.java:2241)
at project.sharethefare.CurrentLocation.onCreate(CurrentLocation.java:24)
at android.app.Activity.performCreate(Activity.java:6221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5834)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: java.lang.RuntimeException: The API key can only be specified once. It is recommended that you use the meta-data tag with the name: com.google.android.maps.v2.API_KEY in the <application> element of AndroidManifest.xml
at com.google.maps.api.android.lib6.gmm6.c.g.a(Unknown Source)
at com.google.maps.api.android.lib6.c.i.a(Unknown Source)
at com.google.maps.api.android.lib6.c.el.a(Unknown Source)
at com.google.maps.api.android.lib6.c.ab.a(Unknown Source)
at com.google.maps.api.android.lib6.c.aa.a(Unknown Source)
at com.google.android.gms.maps.internal.x.onTransact(SourceFile:107)
at android.os.Binder.transact(Binder.java:380)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
at com.google.android.gms.dynamic.a$4.b(Unknown Source)
at com.google.android.gms.dynamic.a.a(Unknown Source)
at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:920)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1206)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2159)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428)
            at android.app.Activity.setContentView(Activity.java:2241)
            at project.sharethefare.CurrentLocation.onCreate(CurrentLocation.java:24)
            at android.app.Activity.performCreate(Activity.java:6221)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5834)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
This is the CurrentLocation class:
package project.sharethefare;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import android.location.Location;
import android.view.View;
public class CurrentLocation extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
//________________________________________________________________________________________
#Override
protected void onCreate(Bundle savedInstanceState) { //auto generated
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current_location);
setUpMapIfNeeded(); //part of google maps api
mMap.setMyLocationEnabled(true); //creates a new HomeScreen
}
#Override
protected void onResume() { //Auto Generated
super.onResume();
setUpMapIfNeeded(); //set up map if not already created
}
/**
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
* installed) and the map has not already been instantiated.. This will ensure that we only ever
* call {#link #setUpMap()} once when {#link #mMap} is not null.
* <p/>
* If it isn't installed {#link SupportMapFragment} (and
* {#link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
* install/update the Google Play services APK on their device.
* <p/>
* A user can return to this FragmentActivity after following the prompt and correctly
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not
* have been completely destroyed during this process (it is likely that it would only be
* stopped or paused), {#link #onCreate(Bundle)} may not be called again so we should call this
* method in {#link #onResume()} to guarantee that it will be called.
*/
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.curLocMap))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p/>
* This should only be called once and when we are sure that {#link #mMap} is not null.
*/
//call the method to continuously check current location
private void setUpMap() {
mMap.setOnMyLocationChangeListener(myLocationChangeListener);
}
//called above. Used to constantly update the users position on the map
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
//create a new latitude and longitude point
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
//make global variables on home screen == to current location
HomeScreen.curLat = location.getLatitude();
HomeScreen.curLong = location.getLongitude();
HomeScreen.curLocSet = true;
//animate the camera to zoom in on position when found
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
}
};
public void backToHome(View view){ // called when button clicked. returns to homeScreen activity
Intent intent = new Intent(CurrentLocation.this,HomeScreen.class);
startActivity(intent);
}
}
This is the corresponding xml file:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#000">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginTop="60dp"
android:layout_marginBottom="50dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/curLocMap"
tools:context="project.sharethefare.CurrentLocation"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
<Button
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="70dp"
android:text="#string/set_Cur_Loc"
android:textSize="20sp"
android:onClick="backToHome"
android:background="#drawable/button_design"
android:textStyle="bold"/>
</LinearLayout>
And this is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="project.sharethefare" >
<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-feature
android:glEsVersion="0x00020000"
android:required="true" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".HomeScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="" />
<activity
android:name=".CurrentLocation"
android:label="#string/title_activity_current_location" >
</activity>
<activity
android:name=".Destination"
android:label="#string/title_activity_maps" >
</activity>
<activity
android:name=".Share"
android:label="#string/title_activity_share" >
</activity>
<activity
android:name=".SplashScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value=""/>
</application>
</manifest>
Caused by: java.lang.RuntimeException: The API key can only be specified once. It is recommended that you use the meta-data tag with the name: com.google.android.maps.v2.API_KEY in the <application> element of AndroidManifest.xml
Based upon the error message, delete this from your manifest:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyCgOCmQbNnBUd_6HeB5DmTfHXPaxmmJJtk"/>
If you are using both Maps & Places Api in your application then you only need to specify geo api key
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyCgOCmQbNnBUd_6HeB5DmTfHXPaxmmJJtk"/>
Both apis will work properly.You can not use both meta data tags for google apis.
In my case I have Google Map & Place API (PlaceLikelihoodBuffer).
Manifest file does not support two meta data for Google API key.
I have commented Google map API key for Map. Looks like below:
<!-- <meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBqXxRIxL4nK7BarmN6qQB48kkAbdp1Kbk" />\ -->
<meta-data
android:name="com.crashlytics.ApiKey"
android:value="5b03ad6e904c61e5d9bfe6e499e749671d9eaea4" />
Fortunately Google Map & Place API (PlaceLikelihoodBuffer) both worked for me.
Strange but true.
Hope this will help you.

Eclipse AVD gives error of 'Unfortunately, (app) has stopped'

I am a beginning in trying to create applications for android using Eclipse (also why i didn't do pictures). I started to learn to program by following Site (link). I tried this 3 times and each times it creates 2 projects. First one always called 'appcompat_v7' and the second one what ever i called it. In this instance I called it 'FirstApp'. In the project FirstApp, I could not find /res/layout containing anything so i created it, calling it fragment_main.xml. Containing:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
</LinearLayout>
I edited my 'androidmanifest.xml' to say:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="20" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
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>
And my strings.xml says:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">FirstApp</string>
<string name="edit_message">Enter a message</string>
</resources>
When running on my Samsung Galaxy S4 or on a AVD manager:
(settings are platform: 4.4W , API Lvl: 20 , and CPU/ABI: Android Wear
ARM (armeabi-v7a) , Device: Nexus 5)
Both give me the same error. "Unfortunately, (app name) has stopped."
And my LogCat gives me this log:
~Not late-enabling CheckJNI (already on)
~Shutting down VM
~threadid=1: thread exiting with uncaught exception (group=0xb2aa9d70)
~FATAL EXCEPTION: main
~Process: com.example.firstapp, PID: 745
~java.lang.RuntimeException: Unable to instantiate activity ComponentI
nfo{com.example.firstapp/com.example.firstapp.MainActivity}: java.lan
g.ClassNotFoundException: Didn't find class "com.example.firstapp.Mai
nActivity" on path: DexPathList[[zip file "/data/app/com.example.firs
tapp-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.firs
tapp-2, /system/lib]]
~at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2123)
~at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
~at android.app.ActivityThread.access$800(ActivityThread.java:138)
~at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
~at android.os.Handler.dispatchMessage(Handler.java:102)
~at android.os.Looper.loop(Looper.java:136)
~at android.app.ActivityThread.main(ActivityThread.java:5026)
~at java.lang.reflect.Method.invokeNative(Native Method)
~at java.lang.reflect.Method.invoke(Method.java:515)
~at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
~at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
~at dalvik.system.NativeStart.main(Native Method)
~Caused by: java.lang.ClassNotFoundException: Didn't find class "com.e
xample.firstapp.MainActivity" on path: DexPathList[[zip file "/data/a
pp/com.example.firstapp-2.apk"],nativeLibraryDirectories=[/data/app-l
ib/com.example.firstapp-2, /system/lib]]
~at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
~at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
~at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
~at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
~at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
~ ... 11 more
~Sending signal. PID: 745 SIG: 9
I have not edited anything else, but those xml files (strings.xml, fragment_main.xml, and AndroidManifest.xml)
Here is a pic of my packages:
Please help to remove this error. Thanks!
First of all, the reason that your app is creating two projects is for compatibility with devices running below API 14. I would recommend (and this is just a personal preference) that, as a beginner, when you create your projects you should set the minimum API level to 14.
When setting up a new project, check the check box that says 'Create activity', right under 'Create custom launcher icon':
Among the automatically created files, you should find a MainActivity.java file (in src > com.example.YourAppName) and an activity_main.xml layout file (in res > layout). If they are not created, this points to issues with your Eclipse/ADT/SDK setup.
So my advice would be to create a new project (above API 14) and run it without editing/creating any files. If it works, then you're good to go.

(WebView)java.lang.runtimeexception unable to start activity

I am creating android application which is open a webpage using webview but on run time I got an error . This is mobile js page in which is I want to open webview.
Log-cat :
06-19 00:44:25.756: I/Process(1130): Sending signal. PID: 1130 SIG: 9
06-19 00:46:45.136: D/AndroidRuntime(1156): Shutting down VM
06-19 00:46:45.136: W/dalvikvm(1156): threadid=1: thread exiting with uncaught exception (group=0xb4a48ba8)
06-19 00:46:45.156: E/AndroidRuntime(1156): FATAL EXCEPTION: main
06-19 00:46:45.156: E/AndroidRuntime(1156): Process: com.example.dynakodetechnologypvt.ltd, PID: 1156
06-19 00:46:45.156: E/AndroidRuntime(1156): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dynakodetechnologypvt.ltd/com.example.dynakodetechnologypvt.ltd.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.os.Handler.dispatchMessage(Handler.java:102)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.os.Looper.loop(Looper.java:136)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-19 00:46:45.156: E/AndroidRuntime(1156): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 00:46:45.156: E/AndroidRuntime(1156): at java.lang.reflect.Method.invoke(Method.java:515)
06-19 00:46:45.156: E/AndroidRuntime(1156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-19 00:46:45.156: E/AndroidRuntime(1156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-19 00:46:45.156: E/AndroidRuntime(1156): at dalvik.system.NativeStart.main(Native Method)
06-19 00:46:45.156: E/AndroidRuntime(1156): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
06-19 00:46:45.156: E/AndroidRuntime(1156): at com.example.dynakodetechnologypvt.ltd.MainActivity.onCreate(MainActivity.java:19)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.app.Activity.performCreate(Activity.java:5231)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-19 00:46:45.156: E/AndroidRuntime(1156): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-19 00:46:45.156: E/AndroidRuntime(1156): ... 11 more
Main Activity
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
String url = "http://www.google.com";
WebView view = (WebView) this.findViewById(R.id.webView1);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
Android manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dynakodetechnologypvt.ltd"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<activity
android:name="com.example.dynakodetechnologypvt.ltd.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>
How can I solve this please help
ActionBarActivity requires a theme that extends from Theme.AppCompat, otherwise your app will crash with this error:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
So in Manifest file use the new theme with MainActivity: as
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dynakodetechnologypvt.ltd"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<activity
android:name="com.example.dynakodetechnologypvt.ltd.MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.AppCompat"> //Add here
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
This will add Actionbar in MainActivity
But usually you need to customize you Actionbar. To do this you need to create two styles with Theme.AppCompat parent, for example, "#style/Theme.AppCompat.Light". First one will be for api 11>= (versions of android with build in android actionbar), second one for api 7-10 (no build in actionbar).
Let's look at first style. It will be located in res/values-v11/styles.xml . It will look like this:
<style name="Theme.Styled" parent="#style/Theme.AppCompat.Light">
<!-- Setting values in the android namespace affects API levels 11+ -->
<item name="android:actionBarStyle">#style/Widget.Styled.ActionBar</item>
</style>
<style name="Widget.Styled.ActionBar" parent="#style/Widget.AppCompat.Light.ActionBar">
<!-- Setting values in the android namespace affects API levels 11+ -->
<item name="android:background">#drawable/ab_custom_solid_styled</item>
<item name="android:backgroundStacked"
>#drawable/ab_custom_stacked_solid_styled</item>
<item name="android:backgroundSplit"
>#drawable/ab_custom_bottom_solid_styled</item>
</style>
And you need to have same style for api 7-10. It will be located in res/values/styles.xml, BUT because that api levels don't yet know about original android actionbar style items, we should use one, provided by support library. ActionBar Compat items are defined just like original android, but without "android:" part in the front:
<style name="Theme.Styled" parent="#style/Theme.AppCompat.Light">
<!-- Setting values in the default namespace affects API levels 7-11 -->
<item name="actionBarStyle">#style/Widget.Styled.ActionBar</item>
</style>
<style name="Widget.Styled.ActionBar" parent="#style/Widget.AppCompat.Light.ActionBar">
<!-- Setting values in the default namespace affects API levels 7-11 -->
<item name="background">#drawable/ab_custom_solid_styled</item>
<item name="backgroundStacked">#drawable/ab_custom_stacked_solid_styled</item>
<item name="backgroundSplit">#drawable/ab_custom_bottom_solid_styled</item>
</style>
For more info see ActionBarCompat and I/O 2013 App Source

Android App Activity Issue

I'm having trouble opening an activity. In my first activity (activity_main), I have 3 buttons.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFF"
android:id="#+id/root"
>
<TableRow android:layout_margin="0dip"
android:id="#+id/row1">
<Button android:id="#+id/button01"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_weight="1"
android:hint="#string/buttonText1"
android:layout_marginTop="60dip"
android:layout_marginBottom="15dip"
android:padding="15dip" />
</TableRow>
<TableRow android:layout_margin="0dip"
android:id="#+id/row2">
<Button android:id="#+id/button02"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_weight="1"
android:hint="#string/buttonText2"
android:padding="15dip" />
</TableRow>
<TableRow android:layout_margin="0dip"
android:id="#+id/row3">
<Button android:id="#+id/button03"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_marginTop="15dip"
android:hint="#string/buttonText3"
android:onClick="browseExercises"
android:padding="15dip" />
</TableRow>
</TableLayout>
</LinearLayout>
I click on the 3rd button, and my second activity opens, which consists of two additional buttons.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFF"
android:id="#+id/root"
>
<TableRow android:layout_margin="0dip"
android:id="#+id/row1">
<Button android:id="#+id/button01"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_weight="1"
android:hint="#string/alphabetical"
android:layout_marginTop="60dip"
android:layout_marginBottom="15dip"
android:onClick="Alphabetical"
android:padding="15dip" />
</TableRow>
<TableRow android:layout_margin="0dip"
android:id="#+id/row2">
<Button android:id="#+id/button99999"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_weight="1"
android:hint="#string/group"
android:onClick="muscleGroups"
android:padding="15dip" />
</TableRow>
</TableLayout>
</LinearLayout>
I click on either of these buttons, and I get an error telling me that "Unfortunately, Test_Project has stopped."
I already have an activity created for each of these two buttons, and everything for these two buttons are coded the exact same way as when I created my second activity, which opens just fine, and has buttons I can click on without causing any errors. I think I'm missing something fundamental here, it's like if I create too many activities, they aren't able to look back at my MainActivity.java file for the method that is supposed to open the next activity. Here is my MainActivity.java code:
package com.example.test_project;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void browseExercises(View view){
Intent intent = new Intent(this, BrowseExercises.class);
startActivity(intent);
}
public void Alphabetical(View view){
Intent intent = new Intent(this, Alphabetical.class);
startActivity(intent);
}
public void muscleGroups(View view){
Intent intent = new Intent(this, MuscleGroups.class);
startActivity(intent);
}
#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;
}
}
Here is my Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test_project"
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.test_project.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.test_project.BrowseExercises"
android:label="#string/title_activity_browse_exercises"
>
</activity>
<activity
android:name="com.example.test_project.MuscleGroups"
android:label="#string/title_activity_muscle_groups" >
</activity>
<activity
android:name="com.example.test_project.Alphabetical"
android:label="#string/title_activity_alphabetical" >
</activity>
</application>
</manifest>
And my logcat file:
04-20 13:46:57.632: W/dalvikvm(26747): threadid=1: thread exiting with uncaught exception (group=0x40dc42a0)
04-20 13:46:57.639: E/AndroidRuntime(26747): FATAL EXCEPTION: main
04-20 13:46:57.639: E/AndroidRuntime(26747): java.lang.IllegalStateException: Could not find a method muscleGroups(View) in the activity class com.example.test_project.BrowseExercises for onClick handler on view class android.widget.Button with id 'button99999'
04-20 13:46:57.639: E/AndroidRuntime(26747): at android.view.View$1.onClick(View.java:3587)
04-20 13:46:57.639: E/AndroidRuntime(26747): at android.view.View.performClick(View.java:4106)
04-20 13:46:57.639: E/AndroidRuntime(26747): at android.view.View$PerformClick.run(View.java:17052)
04-20 13:46:57.639: E/AndroidRuntime(26747): at android.os.Handler.handleCallback(Handler.java:615)
04-20 13:46:57.639: E/AndroidRuntime(26747): at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 13:46:57.639: E/AndroidRuntime(26747): at android.os.Looper.loop(Looper.java:137)
04-20 13:46:57.639: E/AndroidRuntime(26747): at android.app.ActivityThread.main(ActivityThread.java:5059)
04-20 13:46:57.639: E/AndroidRuntime(26747): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 13:46:57.639: E/AndroidRuntime(26747): at java.lang.reflect.Method.invoke(Method.java:511)
04-20 13:46:57.639: E/AndroidRuntime(26747): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
04-20 13:46:57.639: E/AndroidRuntime(26747): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
04-20 13:46:57.639: E/AndroidRuntime(26747): at dalvik.system.NativeStart.main(Native Method)
04-20 13:46:57.639: E/AndroidRuntime(26747): Caused by: java.lang.NoSuchMethodException: muscleGroups [class android.view.View]
04-20 13:46:57.639: E/AndroidRuntime(26747): at java.lang.Class.getConstructorOrMethod(Class.java:460)
04-20 13:46:57.639: E/AndroidRuntime(26747): at java.lang.Class.getMethod(Class.java:915)
04-20 13:46:57.639: E/AndroidRuntime(26747): at android.view.View$1.onClick(View.java:3580)
04-20 13:46:57.639: E/AndroidRuntime(26747): ... 11 more
04-20 13:46:59.202: I/Process(26747): Sending signal. PID: 26747 SIG: 9
Probably something pretty simple I'm just not aware of yet, but any help would be appreciated!!!!
By looking at your comment I guess that you have not created JAVA files for other Activities.
For e.g. you must create BrowseExercise.java file in the directory where your main Activity lies. And then you need to define this Activity in the AndroidManifest.xml file.
An easy way to do is by just clicking +(or something like that) on left TOP corner , then Android , New Activity . Then you just have to select which type of activity you want to use .
Have you made Java files for all your activity? Like if you are opening other activity it should fulfill following requirements:
It should be mention in manifest file with proper action
There should be one java file corresponding to that activity
Layout file should be defined or you should create layout via code
Intent should be properly declared.
If still problem exists please add more code of your intent calling and java files.

Categories