I am not totally new to working with the eclipse android plugin but I am new at writing a project from the ground up. So I went to the Android developers website to follow the 'Hello World' tutorial.
When I run my program the emulator puts up a screen that says
unfortunately Hello, Android Has stopped working.
my code is:
HelloAndroid.Java
package daniel.android.projects;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Object o = null;
o.toString();
setContentView(R.layout.main);
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/hello"/>
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello, Android! I am a string resource!</string>
<string name="app_name">Hello, Android</string>
</resources>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="daniel.android.projects"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".AndroidTesterActivity"
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 have a null pointer exception, you assign null to o and then call toString() on it:
Object o = null;
o.toString();
It seems like it serves nothing in your application anyway, it shouldn't be there.
Also, looking at your code, you create HelloAndroid class, but in themanifest you declare
android:name=".AndroidTesterActivity"
it should be
android:name=".HelloAndroid"
Related
In the following SSCCE I am trying to invoke an explicit intent from preferences.xml, to open an Activity which is located in the one and only package in the app, in which all Activity's are located.
But I get the following exception:
android.content.ActivityNotFoundException: No Activity found to handle Intent { }
I have seen this question but it is about starting an Activity in another package, and somebody said in that question that their app to open Activity in default package works fine.
Following are the relevant parts of code.
NOTE: Since SecondActivity is in the same package as the MainActivity, I initially tried to use only one android:targetClass attribute to the <intent> in the preferences.xml, but then after the exception I added the android:targetPackage too, but that did not solve the problem.
MainActivty.java:
package practice.preferences_practice;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class MainActivity extends PreferenceActivity {
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<Preference android:key="#+id/preferences_preference_preferenceContainingIntent"
android:title="#string/preferences_preference_preferenceContainingIntent_title"
android:summary="#string/preferences_preference_preferenceContainingIntent_summary" >
<intent android:targetPackage="practice.preferences_practice"
android:targetClass="practice.preferences_practice.SecondActivity" />
</Preference>
</PreferenceScreen>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="practice.preferences_practice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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=".SecondActivity"
android:label="#string/title_activity_second" >
</activity>
</application>
</manifest>
NOTE: I have Not used an <intent-filter> in Manifest for the SecondActivity because it lies in the same default package as the MainActivity, which is practice.preferences_practice.
NOTE: If you think I should post all the other code files as well, please let me know.
EDIT:
res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Preferences Practice</string>
<string name="hello_world">Hello world!</string>
<string name="preferences_preference_preferenceContainingIntent_title">Preferece Title</string>
<string name="preferences_preference_preferenceContainingIntent_summary">Opens another activity because this preference contains and invokes an Intent.</string>
<string name="title_activity_second">SecondActivity</string>
</resources>
res/layout/activity_second.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="#F4A460"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
SecondActivity.java:
package practice.preferences_practice;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
You code is as it should be. It's working pretty fine as I also tested it. Try to clean build the project and reinstall the apk.
Use startActivity(new Intent(MainActivity.this, SecondActivity.class)); to start second activity.
I'm new to Android programming and i'm doing tutorial from layouts from http://developer.android.com/resources/tutorials/views/index.html . Relative layout tutorial exactly. I did everything they say but when I try to start app in android emulator there are no buttons, textfields etc. Only name of app shows at the top. What is wrong? Is this a problem with emulator? I'm using Eclipse Version: 3.7.1.
EDIT:
package pchot.tutorial;
import android.app.Activity;
import android.os.Bundle;
public class Tutorial1Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pchot.tutorial"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Tutorial1Activity"
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>
Layout is copy/paste from http://developer.android.com/resources/tutorials/views/hello-relativelayout.html
EDIT:
Ok, problem solved. I needed to delete "Hello World, Tutorial1Activity!" from string.xml file which was autocreated during creation of project in Eclipse.
Have you modified you manifest.xml file? Maybe you haven't modify it to make it aware that the launched main activity is the activity you've just created.
You didn't declare anything in your onCreate() method.
For Button declare Button btn = (Button)findViewById(R.id.btnSubmit)
Implement onClickListener if you want the button to listen to clicks.
For TextView, declare TextView tv = (TextView)findViewById(R.id.textview)
I've made a very simple app that just displays a Google map. However, it only seems to be displaying gray squares.
I've troubleshooted and have done virtually everything I could. I checked my code 5+ times over, I've compared it even to other tutorials. I've re-made my API key 3 times - none of them work.
The phone I'm using is connected to wireless. Even the emulator won't work. I have the library and permission established in the manifest. I think I've done just about everything, but it still doesn't work... any suggestions?
Here's my code:
MapsActivity.java:
package net.learn2develop.GoogleMaps;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import android.os.Bundle;
public class MapsActivity extends MapActivity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="#+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0duU2_tgS67qkUZIpmLVIo0IDvJDh4Ew1Mzh9Pg"
/>
</RelativeLayout>
GoogleMapsManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.GoogleMaps"
android:versionCode="1"
android:versionName="1.0.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".MapsActivity"
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>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
You need to add
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
into you manifest
Note: If you are using both NETWORK_PROVIDER and GPS_PROVIDER, then you need to request only the ACCESS_FINE_LOCATION permission, because it includes permission for both providers. (Permission for ACCESS_COARSE_LOCATION includes permission only for NETWORK_PROVIDER.)
The problem was that Mike was using an API key generated with the release keystore instead of the debug keystore that eclipse uses when compiling the apk.
I had the same error ! I tried everything, so a try this tutorial below and it worked!!!!
TUTORIAL
Thanks in advance for your efforts.
I have an Application that revolves around a ListActivity extended class. Before the app starts, I want to check whether the user is registered, and if not tell him to and get some info from him. So, I tried to call StartActivity in the OnCreate() method. When that loaded, I got a big black screen.
I thought that it may be related to being run in the OnCreate so I let the Activity start as usual, and I tried to run it in an OnClick event, and I got the same result.
In both cases, if I press escape, that Window goes away and the main app window comes back.
Here are the lines from where I call the new Activity
Intent emailIntent = new Intent(this, EmailAddressGetter.class);
this.startActivity(emailIntent);
Here is the code of the class
/**
*
*/
package com.kosherapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
/**
* #author Josh
*
*/
public class EmailAddressGetter extends Activity {
public void OnCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emailinput);
}
}
Here is the emailinput xml contents
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
android:textColor="#000000"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:text="hello"
>
</TextView>
And, here is the manifest contents
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kosherapp"
android:versionCode="1"
android:versionName="1.0"
>
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.CALL_PHONE" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
>
<activity
android:name=".KosherApp"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar"
>
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".EmailAddressGetter"
android:label="email Address Getter"
>
</activity>
<activity
android:name=".GoogleMaps"
android:label="Google Maps"
>
</activity>
<uses-library
android:name="com.google.android.maps" />
</application>
</manifest>
Let me know if there's any other info you may need. Oh, I'm running this with the Google API 2.1-update
Thanks
Change OnCreate to onCreate in EmailAddressGetter.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emailinput);
}
You need to use EditText instead. TextView is for static text which is why nothing is displaying. You should also include the EditText in another layout like Linear or Relative so it looks cleaner.
You are using an intent to call EmailAddressGetter but it doesn't have an intent filter in the manifest. Throw in something like Log.e("EmailAddressGetter", "It worked") in your onCreate and use ddms to see if it is actually opening that activity.
Also, in the updated code in your comment to Robby Pond, remove the xmlns line from EditText
Currently I am trying to add a preference activity into my application
but found out that I couldn't make it works.
Every time, I tried to start the preference activity but it just crash before showing anything.
Here is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="keysquare.android"
android:versionCode="1"
android:versionName="0.9">
<application android:icon="#drawable/icon" android:label="#string/ime_name" android:debuggable="true">
<service android:name="KeysquareAndroid" android:permission="android.permission.BIND_INPUT_METHOD">
<intent-filter>
<action android:name="android.view.InputMethod" />
</intent-filter>
<meta-data android:name="android.view.im" android:resource="#xml/method" />
</service>
<activity android:label="KeysquareAndroidSettings" android:name="KeysquareAndroidSettings" android:exported="true" android:enabled="true"></activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
And the preference xml, which I have tried my best to trim it down.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
</PreferenceScreen>
And finally the preference activity class, which also looks normal to me...
package keysquare.android;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class KeysquareAndroidSettings extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Thanks in advance if anyone can help.
Jeanno
Thanks Mathias.
Finally the problem was solved.
It seems that I shouldn't call activities from a service.
Also, in the method.xml, I should reference to a full package instead of using a
local reference.
Jeanno