I decided to learn android and I bought a book Hello, Android Introducing Google's Mobile Development Platform, Third Edition. I started to do my first project Sudoku from the book and I have managed to do it until when I need it create class Prefs extends PreferenceActivity. The method addPreferencesFromResource(R.XML.Settings) do not work any more with PreferenceActivity. I am trying to fix this problem for two day without success for.
What I have so far! I read that I could manage it via PerformanceFragment and I try to follow this link: http://developer.android.com/reference/android/preference/PreferenceFragment.html and I did following:
public class Prefs extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
It appears an error and Eclipse recommend to add #TargetApi(Build.VERSION_CODES.HONEYCOMB)
when I did it, I could compile it and run my emulator but when I press menu and settings I have received message Unfortunately, Sudoku has stopped.
Other codes in mainActivity I have:
package org.example.sudoku;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.MenuInflater;
public class MainActivitySudoku extends ActionBarActivity implements OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_sudoku);
//setting up click listener for all buttons
View continueButton = findViewById(R.id.continu_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
// other codes...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.settings:
startActivity(new Intent(this, Prefs.class));
return true;
}
return false;
}
inside Sudoku/res/xml/settings.xml I have xml codes:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="music"
android:title="#string/music_title"
android:summary="#string/music_summary"
android:defaultValue="true" />
<CheckBoxPreference
android:key="hints"
android:title="#string/hints_title"
android:summary="#string/hints_sammary"
android:defaultValue="true" />
</PreferenceScreen>
Last thing to mention is AndroidManifest Sudoku/bin/AndroidManifest.xml I have following codes:
$ <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.sudoku"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivitySudoku"
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/about_title"
android:theme="#android:style/Theme.Dialog" >
</activity>
<activity android:name=".Prefs"
android:label="#string/settings_title"
</activity>
</application>
</manifest>
Well, could anyone please help me how to fix this problem. I have looked at on some examples but I could not figure it out. Also if anybody knows a book that would be better to follow exercises it would be great! Thank you David
The method addPreferencesFromResource(R.XML.Settings) do not work any more with PreferenceActivity
It should. It is deprecated, but it should still work.
I have received message Unfortunately, Sudoku has stopped
I'm pretty sure that Ed covers LogCat in his book, which is how you learn more about problems like this.
In this case, you are probably getting a ClassCastException. Your Intent for startActivity() is:
new Intent(this, Prefs.class)
However, Prefs is a PreferenceFragment, not an Activity. You need to have your <activity> element point to an Activity, presumably a PreferenceActivity, one that uses your PreferenceFragment.
Also if anybody knows a book that would be better to follow exercises it would be great!
Asking for off-site resources is considered off-topic for Stack Overflow. That being said, I recommend you pick a book that was published this year. The book that you are using was published four years ago, and that is a long time in Android development.
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 have a few questions about Crosswalk, I tried searching on Google, but since I don't know the exact names of some functions, I can't find anything.
Is it possible to use Crosswalk to render a online website?
If the answer to question 1 is no, Is it possible to write a HTML5+JS app, that renders an iframe, with in there a online site?
If question 1 is yes, can you post a link to a documentation or a tutorial?
Yes it is possible to do both action.
here is some help on the official web site. and here is a code that might work for you too
import android.app.Activity;
import android.os.Bundle;
import java.util.logging.Logger;
import org.xwalk.core.XWalkView;
public class MainActivity extends Activity {
/**
* Called when the activity is first created.
*/
Logger logger = Logger.getLogger("xwl");
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
XWalkView view = new XWalkView(this, this);
logger.severe("loadeding ...");
view.load("https://www.google.com", null);
logger.severe("loaded");
setContentView(view);
}
}
Here is the manifest file
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wxl"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:label="#string/app_name"
android:icon="#drawable/ic_launcher" android:hardwareAccelerated="true"
android:largeHeap="true">
<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>
</application>
</manifest>
The two permissions are needed.
The only problem I found is that it renders blurry :( on android.
Good luck.
I'm in an early process of developping a game for Android and I'm trying to incorporate an ad banner using AdMob. I have downloaded the sample straight from the tutorial on the official site, so I guess that whatever I'm doing wrong here has to be basic since it crushes after a few seconds when debugging on my Galaxy S2 device. Pleae help.
package com.google.example.ads.fundamentals;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.LinearLayout;
/**
* A simple {#link Activity} that embeds an AdView.
*/
public class BannerSample extends Activity {
private AdView adView;
private final TelephonyManager tm =
(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
private final String AD_MOB_ID = "my AdMob ID goes here";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create an ad.
adView = new AdView(this, AdSize.BANNER, AD_MOB_ID);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.addView(adView);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(tm.getDeviceId());
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
/** Called before the activity is destroyed. */
#Override
public void onDestroy() {
// Destroy the AdView.
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
The Logcat data in a screen shot is here
Edit: Also adding the Manifest.XML, which I suspect is what causeing the problems - amazingly the one that came with the example from the official site had an error in it (according to Eclipse), so I had to modify it a bit:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="3"
android:targetSdkVersion="13" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity
android:name=".HelloAdMobActivity"
android:label="#string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:screenOrientation="landscape" >
</activity>
</application>
</manifest>
Logcat information would be super useful, but I'm betting on one of these two reasons:
You referenced the SDK as an external Jar, and didn't add it into your libs/ folder. You have two options here to fix this: add it to your libs/ folder, or go to Properties -> Java Build Path -> Order and Export and check the AdMob jar.
You don't have a LinearLayout in your XML with android:id="linearLayout". This is less likely as the sample project should include this.
Alright, so it turns out the problem is with tm.getDeviceId(), which for whatever reason caused the program to crush.
You need to add the following permission.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
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)
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