I made an app and a part of the app is to get the longitude and latitude add these into one string and use that string to get an xml file from the weather servers so you can see what weather, temperature, humidity... it is at your place.
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/instruction_nexttobutton_english" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1"
android:onClick="getPositionAndGetWheater"
android:text="#string/getweather_button_english" />
<WebView
android:id="#+id/webview"
android:layout_width="wrap_content"
android:layout_height="115dp"
android:layout_alignLeft="#+id/button2"
android:layout_alignRight="#+id/button2"
android:layout_below="#+id/button2" />
</RelativeLayout>
MainActivity.java:
package com.example.thelexapp;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.webkit.WebView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public void getPositionAndGetWheater(View view) {
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
String longitudestring = String.valueOf(longitude);
String latitudestring = String.valueOf(latitude);
String URLforweather = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=" + longitudestring + "," + latitudestring + "&format=xml&num_of_days=1&key=IHAVEAAPIKEYBUTI'MNOTPOSTINGITONTHEFORUMFORSECURITYREASONS";
WebView webview=(WebView)findViewById(R.id.webview);
webview.loadUrl(URLforweather);
}
}
"appname" Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thelexapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.thelexapp.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 WINDOWS:
02-26 15:00:26.710: D/dalvikvm(977): Not late-enabling CheckJNI (already on)
02-26 15:00:29.730: V/WebViewChromium(977): Binding Chromium to the background looperLooper{b1dceb80}
02-26 15:00:29.750: I/chromium(977): [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
02-26 15:00:29.770: I/BrowserProcessMain(977): Initializing chromium process, renderers=0
02-26 15:00:29.960: E/chromium(977): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found.
02-26 15:00:29.960: E/chromium(977): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed.
02-26 15:00:29.960: E/chromium(977): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found.
02-26 15:00:29.960: E/chromium(977): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed.
02-26 15:00:29.970: E/chromium(977): [ERROR:gpu_info_collector.cc(86)] gfx::GLSurface::InitializeOneOff() failed
02-26 15:00:30.000: W/chromium(977): [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
02-26 15:00:30.150: D/dalvikvm(977): GC_FOR_ALLOC freed 88K, 5% free 3235K/3392K, paused 48ms, total 50ms
02-26 15:00:30.160: I/dalvikvm-heap(977): Grow heap (frag case) to 4.297MB for 1127536-byte allocation
02-26 15:00:30.230: D/dalvikvm(977): GC_FOR_ALLOC freed 3K, 4% free 4333K/4496K, paused 67ms, total 67ms
02-26 15:00:30.800: D/gralloc_goldfish(977): Emulator without GPU emulation detected.
02-26 15:00:30.880: W/AwContents(977): nativeOnDraw failed; clearing to background color.
02-26 15:00:31.870: W/AwContents(977): nativeOnDraw failed; clearing to background color.
02-26 15:00:31.930: W/AwContents(977): nativeOnDraw failed; clearing to background color.
02-26 15:00:32.000: W/AwContents(977): nativeOnDraw failed; clearing to background color.
02-26 15:00:32.020: W/AwContents(977): nativeOnDraw failed; clearing to background color.
02-26 15:00:32.090: W/AwContents(977): nativeOnDraw failed; clearing to background color.
02-26 15:00:32.130: W/AwContents(977): nativeOnDraw failed; clearing to background color.
02-26 15:00:31.817: D/AndroidRuntime(977): Shutting down VM
02-26 15:00:31.817: W/dalvikvm(977): threadid=1: thread exiting with uncaught exception (group=0xb1af5b90)
02-26 15:00:31.827: E/AndroidRuntime(977): FATAL EXCEPTION: main
02-26 15:00:31.827: E/AndroidRuntime(977): Process: com.example.thelexapp, PID: 977
02-26 15:00:31.827: E/AndroidRuntime(977): java.lang.IllegalStateException: Could not execute method of the activity
02-26 15:00:31.827: E/AndroidRuntime(977): at android.view.View$1.onClick(View.java:3814)
02-26 15:00:31.827: E/AndroidRuntime(977): at android.view.View.performClick(View.java:4424)
02-26 15:00:31.827: E/AndroidRuntime(977): at android.view.View$PerformClick.run(View.java:18383)
02-26 15:00:31.827: E/AndroidRuntime(977): at android.os.Handler.handleCallback(Handler.java:733)
02-26 15:00:31.827: E/AndroidRuntime(977): at android.os.Handler.dispatchMessage(Handler.java:95)
02-26 15:00:31.827: E/AndroidRuntime(977): at android.os.Looper.loop(Looper.java:137)
02-26 15:00:31.827: E/AndroidRuntime(977): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-26 15:00:31.827: E/AndroidRuntime(977): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 15:00:31.827: E/AndroidRuntime(977): at java.lang.reflect.Method.invoke(Method.java:515)
02-26 15:00:31.827: E/AndroidRuntime(977): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-26 15:00:31.827: E/AndroidRuntime(977): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-26 15:00:31.827: E/AndroidRuntime(977): at dalvik.system.NativeStart.main(Native Method)
02-26 15:00:31.827: E/AndroidRuntime(977): Caused by: java.lang.reflect.InvocationTargetException
02-26 15:00:31.827: E/AndroidRuntime(977): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 15:00:31.827: E/AndroidRuntime(977): at java.lang.reflect.Method.invoke(Method.java:515)
02-26 15:00:31.827: E/AndroidRuntime(977): at android.view.View$1.onClick(View.java:3809)
02-26 15:00:31.827: E/AndroidRuntime(977): ... 11 more
02-26 15:00:31.827: E/AndroidRuntime(977): Caused by: java.lang.NullPointerException
02-26 15:00:31.827: E/AndroidRuntime(977): at com.example.thelexapp.MainActivity.getPositionAndGetWheater(MainActivity.java:33)
02-26 15:00:31.827: E/AndroidRuntime(977): ... 14 more
02-26 15:00:37.167: I/Process(977): Sending signal. PID: 977 SIG: 9
Are you testing it on emulator or device? There is a posibility that your Location location is null. Please make sure you have corresponding permission in your manifest. To handle such situations, you should check your location for null:
public void getPositionAndGetWheater(View view) {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
double longitude = location.getLongitude();
double latitude = location.getLatitude();
String longitudestring = String.valueOf(longitude);
String latitudestring = String.valueOf(latitude);
String URLforweather = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=" + longitudestring + "," + latitudestring + "&format=xml&num_of_days=1&key=IHAVEAAPIKEYBUTI'MNOTPOSTINGITONTHEFORUMFORSECURITYREASONS";
WebView webview = (WebView) findViewById(R.id.webview);
webview.loadUrl(URLforweather);
} else {
//show error
}
}
Related
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'
}
I'm trying to pass a string between 2 classes in Android . The first class is "MainActivity" and the second one is "abc" .
Here's my MainActivity code :
package com.example.passstrings;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends ActionBarActivity {
String pass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pass = "abcabc";
Intent abc = new Intent(this , abc.class);
abc.putExtra("key", pass);
startActivity(abc);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#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;
}
}
}
and here's my abc class code :
package com.example.passstrings;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class abc extends Activity{
TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
String pass= intent.getExtras().getString("key");
if(pass!= null){
tv1.setText(pass);
}
}
}
Here's my fragment_main.xml code :
<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.passstrings.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_marginLeft="69dp"
android:layout_marginTop="72dp"
android:layout_toRightOf="#+id/textView2"
android:text="TextView" />
</RelativeLayout>
and here's my LogCat :
04-04 05:34:38.545: I/dalvikvm(734): Could not find method android.content.pm.PackageManager.getActivityLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 05:34:38.545: W/dalvikvm(734): VFY: unable to resolve virtual method 320: Landroid/content/pm/PackageManager;.getActivityLogo (Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
04-04 05:34:38.545: D/dalvikvm(734): VFY: replacing opcode 0x6e at 0x008b
04-04 05:34:38.545: I/dalvikvm(734): Could not find method android.content.pm.ApplicationInfo.loadLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 05:34:38.545: W/dalvikvm(734): VFY: unable to resolve virtual method 316: Landroid/content/pm/ApplicationInfo;.loadLogo (Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
04-04 05:34:38.545: D/dalvikvm(734): VFY: replacing opcode 0x6e at 0x0099
04-04 05:34:38.555: D/dalvikvm(734): VFY: dead code 0x008e-0092 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 05:34:38.555: D/dalvikvm(734): VFY: dead code 0x009c-00a0 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 05:34:38.675: D/AndroidRuntime(734): Shutting down VM
04-04 05:34:38.675: W/dalvikvm(734): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-04 05:34:38.685: E/AndroidRuntime(734): FATAL EXCEPTION: main
04-04 05:34:38.685: E/AndroidRuntime(734): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.passstrings/com.example.passstrings.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.passstrings/com.example.passstrings.abc}; have you declared this activity in your AndroidManifest.xml?
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.os.Looper.loop(Looper.java:123)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-04 05:34:38.685: E/AndroidRuntime(734): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 05:34:38.685: E/AndroidRuntime(734): at java.lang.reflect.Method.invoke(Method.java:521)
04-04 05:34:38.685: E/AndroidRuntime(734): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-04 05:34:38.685: E/AndroidRuntime(734): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-04 05:34:38.685: E/AndroidRuntime(734): at dalvik.system.NativeStart.main(Native Method)
04-04 05:34:38.685: E/AndroidRuntime(734): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.passstrings/com.example.passstrings.abc}; have you declared this activity in your AndroidManifest.xml?
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Activity.startActivityForResult(Activity.java:2817)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:839)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Activity.startActivity(Activity.java:2923)
04-04 05:34:38.685: E/AndroidRuntime(734): at com.example.passstrings.MainActivity.onCreate(MainActivity.java:25)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-04 05:34:38.685: E/AndroidRuntime(734): ... 11 more
and here's my manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.passstrings"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.passstrings.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>
If I remove "startActivity(abc);" the application doesn't crash, also if use try&catch the application doesn't crash but the same time it doesn't perform the action . So what could be the reason ? and thanks in advance
My LogCat after adding the activity to manifest :
04-04 05:47:16.555: I/dalvikvm(883): Could not find method android.content.pm.PackageManager.getActivityLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 05:47:16.555: W/dalvikvm(883): VFY: unable to resolve virtual method 320: Landroid/content/pm/PackageManager;.getActivityLogo (Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
04-04 05:47:16.555: D/dalvikvm(883): VFY: replacing opcode 0x6e at 0x008b
04-04 05:47:16.555: I/dalvikvm(883): Could not find method android.content.pm.ApplicationInfo.loadLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 05:47:16.565: W/dalvikvm(883): VFY: unable to resolve virtual method 316: Landroid/content/pm/ApplicationInfo;.loadLogo (Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
04-04 05:47:16.565: D/dalvikvm(883): VFY: replacing opcode 0x6e at 0x0099
04-04 05:47:16.565: D/dalvikvm(883): VFY: dead code 0x008e-0092 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 05:47:16.565: D/dalvikvm(883): VFY: dead code 0x009c-00a0 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 05:47:16.775: D/AndroidRuntime(883): Shutting down VM
04-04 05:47:16.775: W/dalvikvm(883): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-04 05:47:16.786: E/AndroidRuntime(883): FATAL EXCEPTION: main
04-04 05:47:16.786: E/AndroidRuntime(883): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.passstrings/com.example.passstrings.abc}: java.lang.NullPointerException
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.os.Looper.loop(Looper.java:123)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-04 05:47:16.786: E/AndroidRuntime(883): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 05:47:16.786: E/AndroidRuntime(883): at java.lang.reflect.Method.invoke(Method.java:521)
04-04 05:47:16.786: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-04 05:47:16.786: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-04 05:47:16.786: E/AndroidRuntime(883): at dalvik.system.NativeStart.main(Native Method)
04-04 05:47:16.786: E/AndroidRuntime(883): Caused by: java.lang.NullPointerException
04-04 05:47:16.786: E/AndroidRuntime(883): at com.example.passstrings.abc.onCreate(abc.java:20)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-04 05:47:16.786: E/AndroidRuntime(883): ... 11 more
and my Manifest after the update :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.passstrings"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.passstrings.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.passstrings.abc" />
</application>
</manifest>
You need to add your second Activity to your Androidmanifest.xml file.
<activity android:name="com.example.passstrings.abc" />
have you declared this activity in your AndroidManifest.xml?
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
it clearly say's that you have not declared your abc activity in manifest file. have you declared this activity in your AndroidManifest.xml?
so just decalre your abc activity in manifest file.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.passstrings.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=".abc"
</activity>
</application>
add this line
<activity
android:name=".abc"
</activity>
and also change this in your abc.java
setContentView(R.layout.activity_main);
you have reference of activity_main only you have to set abc.XML or whatever your XML filename for abc. So change it with
setContentView(R.layout.abc);
Edit
public class abc extends Activity{
TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
tv1=(TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
String pass= intent.getExtras().getString("key");
if(pass!= null){
tv1.setText(pass);
}
}
It is simply ActivityNotFoundException which mean you forgot to declare abc activity in your AndroidManifest.xml file.
Just add following line in your manifest.xml in application tag.
<activity android:name="com.example.passstrings.abc" />
For your new NullPointerException here is the solution
You have defined following .xml in your Activity file activity_main while you have declare TextViews in fragment_main.xml, that's why it is giving NullPointerException.
I suggest you to change the xml from following line
setContentView(R.layout.activity_main);
to
setContentView(R.layout.fragment_main.xml);
I posted my code below my application shows no errors int eh debug menu, when I run it logcat shows one error I posted it below. When I try to run the app on my phone it closes and stops responding and im not sure why. I was following an online tutorial and did it exactly how it said. Please help.
10-07 14:50:02.901: I/Process(11653): Sending signal. PID: 11653 SIG: 9
10-07 14:50:06.124: W/dalvikvm(11801): VFY: unable to resolve static field 1346 (MapAttrs) in Lcom/google/android/gms/R$styleable;
10-07 14:50:06.124: D/dalvikvm(11801): VFY: replacing opcode 0x62 at 0x000e
10-07 14:50:06.134: D/AndroidRuntime(11801): Shutting down VM
10-07 14:50:06.134: W/dalvikvm(11801): threadid=1: thread exiting with uncaught exception (group=0x4129bac8)
10-07 14:50:06.134: E/AndroidRuntime(11801): FATAL EXCEPTION: main
10-07 14:50:06.134: E/AndroidRuntime(11801): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
10-07 14:50:06.134: E/AndroidRuntime(11801): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
10-07 14:50:06.134: E/AndroidRuntime(11801): at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:284)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
10-07 14:50:06.134: E/AndroidRuntime(11801): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.app.Activity.setContentView(Activity.java:1928)
10-07 14:50:06.134: E/AndroidRuntime(11801): at com.example.gloc.MainActivity.onCreate(MainActivity.java:30)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.app.Activity.performCreate(Activity.java:5250)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.app.ActivityThread.access$700(ActivityThread.java:152)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.os.Looper.loop(Looper.java:137)
10-07 14:50:06.134: E/AndroidRuntime(11801): at android.app.ActivityThread.main(ActivityThread.java:5328)
10-07 14:50:06.134: E/AndroidRuntime(11801): at java.lang.reflect.Method.invokeNative(Native Method)
10-07 14:50:06.134: E/AndroidRuntime(11801): at java.lang.reflect.Method.invoke(Method.java:511)
10-07 14:50:06.134: E/AndroidRuntime(11801): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
10-07 14:50:06.134: E/AndroidRuntime(11801): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
10-07 14:50:06.134: E/AndroidRuntime(11801): at dalvik.system.NativeStart.main(Native Method)
MainActivity.java
package com.example.gloc;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
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;
public class MainActivity extends FragmentActivity implements LocationListener {
GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
// Showing status
if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available
int requestCode = 10;
//Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
//dialog.show();
}else { // Google Play Services are available
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
}
}
#Override
public void onLocationChanged(Location location) {
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// Setting latitude and longitude in the TextView tv_location
tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude );
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#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;
}
}
Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/tv_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/tv_location"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gloc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<permission
android:name="in.wptrafficanalyzer.locationingooglemapv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="in.wptrafficanalyzer.locationingooglemapv2.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<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="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="IEARASEDTHSITOHIDEIT"/>
<activity
android:name="com.example.gloc.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>
You can't change the map options in onCreate, as it has not yet been instantiated. Try doing all of the stuff in OnLocationChanged to your GoogleMap object in onResume() instead of onCreate().
I'm trying to learn to build Android apps. This is my very first attempt. Everything was okay and when I would get to this activity (this is when someone would select rock) I could have it display Hello World. I tried to develop rules for winning and losing but whenever I select rock my app just crashes. Please take a look.
package ro.pa.sc;
import java.util.Random;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class Rockvs extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
randombox();
setContentView(R.layout.activity_rockvs);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.rockvs, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up- vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void randombox()
{
Random r = new Random();
int pickedNumber=r.nextInt(2);
if (pickedNumber==1) {
win();
} else if (pickedNumber==2) {
lose();
} else if (pickedNumber==0){
tie();
}
}
public void win() {
String message = "Computer picked Scissors! You Won!";
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
public void lose() {
String message = "Computer picked Paper! You Lose!";
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
public void tie() {
String message = "Computer picked Rock! It's a Tie!";
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
}
Logcat output:
05-05 19:22:46.571: D/ActivityThread(15646): setTargetHeapUtilization:0.25
05-05 19:22:46.571: D/ActivityThread(15646): setTargetHeapIdealFree:8388608
05-05 19:22:46.581: D/ActivityThread(15646): setTargetHeapConcurrentStart:2097152
05-05 19:22:46.722: I/Adreno200-EGL(15646): <qeglDrvAPI_eglInitialize:269>: EGL 1.4 QUALCOMM build: (CL2803123)
05-05 19:22:46.722: I/Adreno200-EGL(15646): Build Date: 11/28/12 Wed
05-05 19:22:46.722: I/Adreno200-EGL(15646): Local Branch: D1L_JB_1736J_Disable_CPUMempool
05-05 19:22:46.722: I/Adreno200-EGL(15646): Remote Branch:
05-05 19:22:46.722: I/Adreno200-EGL(15646): Local Patches:
05-05 19:22:46.722: I/Adreno200-EGL(15646): Reconstruct Branch:
05-05 19:22:48.233: W/dalvikvm(15646): threadid=1: thread exiting with uncaught exception (group=0x4126c438)
05-05 19:22:48.253: E/AndroidRuntime(15646): FATAL EXCEPTION: main
05-05 19:22:48.253: E/AndroidRuntime(15646): java.lang.RuntimeException: Unable to start activity ComponentInfo{ro.pa.sc/ro.pa.sc.Rockvs}: android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2080)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.app.ActivityThread.access$600(ActivityThread.java:137)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.os.Looper.loop(Looper.java:137)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.app.ActivityThread.main(ActivityThread.java:4904)
05-05 19:22:48.253: E/AndroidRuntime(15646): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 19:22:48.253: E/AndroidRuntime(15646): at java.lang.reflect.Method.invoke(Method.java:511)
05-05 19:22:48.253: E/AndroidRuntime(15646): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-05 19:22:48.253: E/AndroidRuntime(15646): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-05 19:22:48.253: E/AndroidRuntime(15646): at dalvik.system.NativeStart.main(Native Method)
05-05 19:22:48.253: E/AndroidRuntime(15646): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
05-05 19:22:48.253: E/AndroidRuntime(15646): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-05 19:22:48.253: E/AndroidRuntime(15646): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:321)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.app.Activity.setContentView(Activity.java:2087)
05-05 19:22:48.253: E/AndroidRuntime(15646): at ro.pa.sc.Rockvs.onCreate(Rockvs.java:20)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.app.Activity.performCreate(Activity.java:5232)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2044)
05-05 19:22:48.253: E/AndroidRuntime(15646): ... 11 more
05-05 19:22:48.253: E/AndroidRuntime(15646): Caused by: java.lang.reflect.InvocationTargetException
05-05 19:22:48.253: E/AndroidRuntime(15646): at java.lang.reflect.Constructor.constructNative(Native Method)
05-05 19:22:48.253: E/AndroidRuntime(15646): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
05-05 19:22:48.253: E/AndroidRuntime(15646): ... 24 more
05-05 19:22:48.253: E/AndroidRuntime(15646): Caused by: android.content.res.Resources$NotFoundException: File You picked Rock! I hope you win! from drawable resource ID #0x7f050009: .xml extension required
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.content.res.Resources.loadColorStateList(Resources.java:2094)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.content.res.TypedArray.getColorStateList(TypedArray.java:342)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.widget.TextView.<init>(TextView.java:820)
05-05 19:22:48.253: E/AndroidRuntime(15646): at android.widget.TextView.<init>(TextView.java:489)
05-05 19:22:48.253: E/AndroidRuntime(15646): ... 27 more
XML File :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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=".Rockvs" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/pickedrock"
android:textColor="#string/pickedrock"
android:textSize="25sp" />
</RelativeLayout>
In your XML, the line:
android:textColor="#string/pickedrock"
is not correct - it should point to a color resource, not a string.
I've been reading up examples on how to do this, trying to piece together a functioning example from posts on this site and others. I'm sure I'm missing something small, but as a novice to Java, I could use some assistance.
I'm merely trying to create a small example, derived from the automated hello world generated when creating a new project in Eclipse. All I want to do is be able to store a few global variables in a subclass, then reference those values in my main activity. Unfortunately, every time I try to run the app, it crashes "Unfortunately, GeneralTest1 has stopped", and the log cat error is not very helpful.
Quick overview:
GlobalVars class extends Application
In the manifest, android:name has been added to reference the additional GlobalVars class
Within my main activity, I'm initializing the global vars class with getApplicationContext()
Here is everything I've got; any help would be much appreciated!
MainActivity.java
package com.example.generaltest1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
GlobalVars myVars = ((GlobalVars)getApplicationContext());
TextView myText = (TextView) findViewById(R.id.myText);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText.setText(myVars.getMyString());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
GlobalVars.java
package com.example.generaltest1;
import android.app.Application;
public class GlobalVars extends Application {
String myString = "Some Text";
public String getMyString() {
return myString;
}
public String setMyString(String string) {
this.myString = string;
return myString;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
strings.xml
<resources>
<string name="app_name">GeneralTest1</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
GeneralTest1 Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.generaltest1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" android:name="GlobalVars">
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Log Cat
08-14 09:53:05.546: E/Trace(620): error opening trace file: No such file or directory (2)
08-14 09:53:05.656: D/AndroidRuntime(620): Shutting down VM
08-14 09:53:05.656: W/dalvikvm(620): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-14 09:53:05.676: E/AndroidRuntime(620): FATAL EXCEPTION: main
08-14 09:53:05.676: E/AndroidRuntime(620): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.generaltest1/com.example.generaltest1.MainActivity}: java.lang.NullPointerException
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.os.Looper.loop(Looper.java:137)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-14 09:53:05.676: E/AndroidRuntime(620): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620): at java.lang.reflect.Method.invoke(Method.java:511)
08-14 09:53:05.676: E/AndroidRuntime(620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-14 09:53:05.676: E/AndroidRuntime(620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-14 09:53:05.676: E/AndroidRuntime(620): at dalvik.system.NativeStart.main(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620): Caused by: java.lang.NullPointerException
08-14 09:53:05.676: E/AndroidRuntime(620): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
08-14 09:53:05.676: E/AndroidRuntime(620): at com.example.generaltest1.MainActivity.<init>(MainActivity.java:10)
08-14 09:53:05.676: E/AndroidRuntime(620): at java.lang.Class.newInstanceImpl(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620): at java.lang.Class.newInstance(Class.java:1319)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
08-14 09:53:05.676: E/AndroidRuntime(620): ... 11 more
You are calling getApplicationContext() in your class definition, which is too early in the activity lifetime and results in a nullpointer exception. Move the assignments to your onCreate() function and it should work as expected.
Please check this article about activities and their lifecycle :
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Your activity should look like this:
public class MainActivity extends Activity {
GlobalVars myVars;
TextView myText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVars = ((GlobalVars)getApplication());
myText = (TextView) findViewById(R.id.myText);
myText.setText(myVars.getMyString());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Add these lines in oncreate()..
GlobalVars myVars = ((GlobalVars)getApplicationContext());
TextView myText = (TextView) findViewById(R.id.myText);