Setting button for Live Wallpaper doesn't appear - android

I created a preference files for my slide show Live Wallpaper.
Most of the source are copied from several samples. I think I copied them correctly, and the eclipse shows no warnings or errors for my source.
My problem is that the "Settings" button doesn't appear when I choose the Live Wallpaper.
I checked the very close question "Can't get settings button to display for live wallpaper", but it didn't solve my problem.
I suspect something is wrong with my Manifest, or preference sources, but I couldn't find the point.
Sorry for my bad English.
My sources are follows:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest package="sample.slide_wallpaper"
android:versionCode="1"
android:versionName="1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="#drawable/icon"
android:label="#string/app_name">
<!-- =========================================================== -->
<!-- Launcher -->
<activity android:name=".Launcher" android:label="#string/app_name"
android:theme="#style/Theme.HalfTrans">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- ==============================================================-->
<!-- Live Wallpaper -->
<service
android:enabled="true"
android:permission="android.permission.BIND_WALLPAPER"
android:label="#string/app_name"
android:name="SlideWallpaper"
android:icon="#drawable/thumbnail">
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService"></action>
</intent-filter>
<meta-data
android:name="android.service.wallpaper"
android:resource="#xml/wallpaper">
</meta-data>
</service>
<!-- ================================================================= -->
<!-- Preferences -->
<activity
android:name="sample.slide_wallpaper.Prefs"
android:label="#string/wallpaper_settings"
android:theme="#android:style/Theme.WallpaperSettings"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.PREFERENCE" />
</intent-filter>
</activity>
</application>
</manifest>
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="#string/wallpaper_settings"
>
<ListPreference
android:key="timer_key"
android:title="#string/list_title"
android:summary="#string/list_summary"
android:entries="#array/timer_pref"
android:entryValues="#array/timer_pref_values"
android:defaultValue="5000"
/>
</PreferenceScreen>
**wallpaper.xml**
<?xml version="1.0" encoding="utf-8" ?>
<wallpaper xmlns:android="http://shemas.android.com/apk/res/android"
android:thumbnail="#drawable/thumbnail"
android:description="#string/description"
android:settingsActivity="sample.slidewallpaper.Prefs"
/>
**Prefs.java**
package sample.slide_wallpaper;
import sample.slide_wallpaper.R;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.util.Log;
public class Prefs extends PreferenceActivity
implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
Log.w("Prefs", "prefs onCreate");
addPreferencesFromResource(R.xml.settings);
}
#Override
protected void onDestroy()
{
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
}
}
SlideWallpaper.java
package sample.slide_wallpaper;
import java.util.Random;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.WindowManager;
import android.content.SharedPreferences;
import android.content.res.Resources;
//WallpaperService
public class SlideWallpaper extends WallpaperService {
// Path for Prefs
public static final String SHARED_PREFS_NAME = "sample.slide_wallpaper.Prefs";
#Override
public void onCreate() {
.....
super.onCreate();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public WallpaperService.Engine onCreateEngine() {
return new WallpaperEngine(getResources());
}
//*********************************************************
//Wallpaper Engine
//*********************************************************
public class WallpaperEngine extends Engine
implements SharedPreferences.OnSharedPreferenceChangeListener {
private final Handler handler=new Handler();
SharedPreferences prefs;
public Bitmap img;
private Bitmap images[] = new Bitmap[5];
private Bitmap eximg;
private final Random randGen = new Random();
private static final String TAG = "WallpaperEngine";
//timer
private long timer = 5000;//interval(sec)×1000
private long startTime = 0;
private int currentAlpha;
private final Paint imagePaint;
private final Runnable drawThread=new Runnable() {
public void run() {
Log.d(TAG,"runnable");
drawFrame();
}
};
public WallpaperEngine(Resources r) {
prefs=SlideWallpaper.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
prefs.registerOnSharedPreferenceChangeListener(this);
images[0]=BitmapFactory.decodeResource(r,R.drawable.imgA);
images[1]=BitmapFactory.decodeResource(r,R.drawable.imgB);
images[2]=BitmapFactory.decodeResource(r,R.drawable.imgC);
images[3]=BitmapFactory.decodeResource(r,R.drawable.imgD);
images[4]=BitmapFactory.decodeResource(r,R.drawable.imgE);
eximg = BitmapFactory.decodeResource(r,R.drawable.bg1);
img = eximg;
imagePaint = new Paint();
imagePaint.setAlpha(255);
}
//=============================
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key){
Log.w(TAG, "onPreferenceChanged");//this Log doesn't show up
String listString = prefs.getString("timer_key", "5000");
int listInt = Integer.parseInt(listString);
timer = listInt;
}
//=================================
#Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
Log.d(TAG, "onCreate");
drawFrame();
}
//=====================================
#Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(fadeAnimate);
handler.removeCallbacks(drawThread);
}
//==================================
#Override
public void onVisibilityChanged(boolean visible) {
Log.d(TAG, "Visibility changed");
if (visible) {
if(nowTime() - startTime + 100 < timer){
if (img.isRecycled()) {
img = eximg;
}
drawBitmap(img);
handler.postDelayed(drawThread, timer - (nowTime() - startTime));
}else {
drawFrame();
}
} else {
handler.removeCallbacks(fadeAnimate);
handler.removeCallbacks(drawThread);
}
}
//===================================================
//draws bitmap
private void drawBitmap(Bitmap b) {
............
}
//=========================
#Override
public void onSurfaceCreated(SurfaceHolder holder) {
super.onSurfaceCreated(holder);
}
//============================
#Override
public void onSurfaceChanged(SurfaceHolder holder,
int format,int width,int height) {
super.onSurfaceChanged(holder,format,width,height);
drawFrame();
}
//==============================
#Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
handler.removeCallbacks(fadeAnimate);
handler.removeCallbacks(drawThread);
}
//=================================
#Override
public void onOffsetsChanged(float xOffset,float yOffset,
float xStep,float yStep,int xPixels,int yPixels) {
drawBitmap(img);
}
//=================================================
//Changes image==================================
protected void drawFrame() {
.....
}
private final Runnable fadeAnimate = new Runnable() {
public void run() {
fadeTransition(img, currentAlpha);
}
};
private void fadeTransition(Bitmap b, int alpha) {
.....
}//END fadeTransition
private long nowTime() {
return System.nanoTime() / 1000000;
}
}//Engine
}//END
I really need your help!!
Also, the thumbnail image doesn't show up on the Live Wallpaper list. But this is a small problem.

Your wallpaper.xml has a minor error. The package name is incorrect. It should be:
<?xml version="1.0" encoding="utf-8" ?>
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
android:thumbnail="#drawable/thumbnail"
android:description="#string/description"
android:settingsActivity="sample.slide_wallpaper.Prefs"
/>

I solved the problem by myself.
Thanks for Rajesh, I rewrote the whole code of "wallpaper.xml", and it worked correctly.
Actually, I couldn't find the difference, but my revised version is this:
<?xml version="1.0" encoding="utf-8" ?>
<wallpaper
xmlns:android="http://schemas.android.com/apk/res/android"
android:thumbnail="#drawable/thumbnail"
android:description="#string/description"
android:settingsActivity="sample.slide_wallpaper.Prefs"
/>
By revising the source, the thumbnail error was shown up, too!

Related

How to get the sumary from preferences

One question:
I have a EditTextPreference to let the user type in a default path for the app.
How can I manage that the new value is to be seen in the preferencefragment I use?
After the user clicked ok in the preference window to complete the new settings I want to write the new path as a summary below the title.
I have tried already experiencing with the onSharedPreferenceChanged but it did not work. I do not know how to get access to the edit field from the popup window where the users text is in.
Hope to find some help
Andreas!
Edit:
Here is the source code for the whole PreferenceFragment I used and modified with the suggestions of Rustam. Unfortunately it does not work.
package com.example.wbsettings;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
public class PreferenceFrag extends PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener{
public static final String KEYVAL = "startpath";
SharedPreferences sp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
EditTextPreference prefUrl = (EditTextPreference) findPreference(KEYVAL);
prefUrl.getEditText().setHint("default path");
//>> Here the app crashed when I debug it
------------------------------------
prefUrl.setSummary(sp.getString(KEYVAL, ""));
}//onCreate
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
Preference pref = findPreference(key);
if (pref instanceof EditTextPreference) {
EditTextPreference etp = (EditTextPreference) pref;
pref.setSummary(etp.getText());
}
}//onSharedPreferenceChanged
}
What I also do not understand in your example: Where is the preferences to register with the OnSharedPreferenceChangeListener? I mean as of now I never reach the onSharedPreferenceChanged procedure because of the crash. But when I make the statement a comment and the app is running and finished I have never been stopped at the breakpoint I set within the onSharedPreferenceChanged procedure.
Another question:
What is the best practice here to answer a comment if the answer becomes too long for another comment? I tried to open an answer to my own question. But a pop up informed me that this is not the way it should be. So what to do?
Regards Andreas!
your menu_setting.xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/menu_settings"
android:icon="#android:drawable/ic_menu_preferences"/>
</menu>
your preference.xml :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:dialogTitle="Enter path"
android:key="prefPath"
android:summary=""
android:title="Path Setting" />
</PreferenceScreen>
your PreferenceFrag should look like this :
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
public class PreferenceFrag extends PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener{
public static final String KEYVAL = "prefPath";
SharedPreferences sp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preference);
EditTextPreference prefPath = (EditTextPreference) findPreference(KEYVAL);
prefPath.getEditText().setHint("sdcard/path");
// Here the app crashed when I debug it
SharedPreferences sp = getPreferenceScreen().getSharedPreferences();
prefPath.setSummary(sp.getString(KEYVAL, ""));
}//onCreate
#Override
public void onResume() {
super.onResume();
// Set up a listener whenever a key changes
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onPause() {
super.onPause();
// Unregister the listener whenever a key changes
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
Preference pref = findPreference(key);
if (pref instanceof EditTextPreference) {
EditTextPreference etp = (EditTextPreference) pref;
pref.setSummary(etp.getText());
}
}//onSharedPreferenceChanged
}
your PreferenceActivity :
import android.app.Activity;
import android.os.Bundle;
public class PreferenceActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new PreferenceFrag()).commit();
}
}
your MainActivity :
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview=(TextView)findViewById(R.id.textview);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_setting, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
Intent i = new Intent(this, PreferenceActivity.class);
startActivity(i);
break;
}
return true;
}
}
finally AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.preferencetest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.preferencetest.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.preferencetest.PreferenceActivity"/>
</application>
</manifest>

Saving state of activity on orientation change using different Layouts?

I am using two different Layouts for same Activity, On orientation change the activity state is not maintained. Please suggest how to do that?
You can easily store your "activity state" using onSavedInstanceState. For example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(saveInstanceState != null) {
if(savedInstanceState.getBoolean("running") == true) {
// previously is running
} else {
// previously not running
}
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if(condition) {
outState.putBoolean("running", true);
}
}
You seem to do some work in an AsyncTask and then you're changing the orientation.
Move the task you're doing from AsyncTask in an IntentService. Once the job is executed, send a broadcast with the result. In your activity, register a BroadcastReceiver in onCreate and de-register it in onDestroy.
That receiver will handle the result of your operation. You could also send from IntentService some intermediary data/progress update that can be received and processed by the same Receiver.
EDIT: to show some code.
The first search result on Google for IntentService tutorial. But I would strongly recommend reading more about Services. Also Vogella has a nice article about this.
About BroadcastReceivers.
If you want something to get you started here's a sample project I built in 15 mins:
The activity:
package com.adip.droid.sampleandroid;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class FrontActivity extends FragmentActivity {
private TextView labelData;
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WorkerService.WORK_PROGRESS_ACTION.equals(action)) {
publishProgress(intent.getStringExtra(WorkerService.WORK_KEY_PROGRESS));
} else if (WorkerService.WORK_END_ACTION.equals(action)) {
workInProgress = false;
publishResult(intent.getStringExtra(WorkerService.WORK_KEY_RESULT));
}
}
};
protected void publishProgress(String data) {
showMessage(data);
}
protected void publishResult(String data) {
showMessage(data);
}
private void showMessage(String data) {
labelData.setText(data);
}
/**
* Maybe you need this in order to not start the service once you have an ongoing job ...
* */
private boolean workInProgress;
#Override
protected void onCreate(Bundle instanceState) {
super.onCreate(instanceState);
setContentView(R.layout.front_layout);
if (instanceState != null) {
workInProgress = instanceState.getBoolean("WORK_IN_PROGRESS", false);
}
labelData = (TextView) findViewById(R.id.labelData);
findViewById(R.id.btnWorker).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
doTheJob();
}
});
IntentFilter filter = new IntentFilter(WorkerService.WORK_END_ACTION);
filter.addAction(WorkerService.WORK_PROGRESS_ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);
}
protected void doTheJob() {
if (workInProgress) {
return;
}
Intent serviceIntent = new Intent(this, WorkerService.class);
serviceIntent.setAction(WorkerService.WORK_START_ACTION);
startService(serviceIntent);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("WORK_IN_PROGRESS", workInProgress);
}
#Override
protected void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
}
}
its layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnWorker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Start the work" />
<TextView
android:id="#+id/labelData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btnWorker"
android:layout_centerHorizontal="true"
android:layout_marginBottom="35dp"
android:freezesText="true"
android:text="No data yet"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
and the IntentService:
package com.adip.droid.sampleandroid;
import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
public class WorkerService extends IntentService {
public static final String WORK_START_ACTION = "WORK_START_ACTION";
public static final String WORK_END_ACTION = "WORK_END_ACTION";
public static final String WORK_KEY_RESULT = "WORK_KEY_RESULT";
public static final String WORK_PROGRESS_ACTION = "WORK_PROGRESS_ACTION";
public static final String WORK_KEY_PROGRESS = "WORK_KEY_PROGRESS";
public WorkerService() {
super("WorkerService");
}
#Override
protected void onHandleIntent(Intent intent) {
if (WORK_START_ACTION.equals(intent.getAction())) {
startProgress();
}
}
private void startProgress() {
publishProgress("Starting the job");
synchronized (this) {
try {
wait(2500);
} catch (InterruptedException ignored) {
}
}
publishProgress("Progress Point A");
synchronized (this) {
try {
wait(2500);
} catch (InterruptedException ignored) {
}
}
publishJobDone();
}
public void publishProgress(String data) {
Intent progressIntent = new Intent(WORK_PROGRESS_ACTION);
progressIntent.putExtra(WORK_KEY_PROGRESS, data);
LocalBroadcastManager.getInstance(this).sendBroadcast(progressIntent);
}
public void publishJobDone() {
Intent progressIntent = new Intent(WORK_END_ACTION);
progressIntent.putExtra(WORK_KEY_RESULT, "Job done!");
LocalBroadcastManager.getInstance(this).sendBroadcast(progressIntent);
}
}
Also don't forget to update your manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.adip.droid.sampleandroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".FrontActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".WorkerService"
android:exported="false" >
</service>
</application>
</manifest>

Android: Preference activity not running?

I have been playing around with this preference activity code for most of the day now and with some help in my last post I got it so it would compile / install on AVD but it wont run and does not show up in the app menu. Here is the code:
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.DialogPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;
public class volman extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dhd);
DialogPreference dp = (DialogPreference) findPreference("mediavolume");
dp.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference,
Object newValue) {
SeekBar volumeBar = (SeekBar) findViewById(R.id.seekBar);
final AudioManager manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeBar.setMax(manager
.getStreamMaxVolume(AudioManager.STREAM_SYSTEM));
volumeBar.setProgress(manager
.getStreamVolume(AudioManager.STREAM_SYSTEM));
volumeBar
.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(volman.this, "Starting Vol Tracking", Toast.LENGTH_LONG).show();
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(volman.this, "Now Stopping", Toast.LENGTH_LONG).show();
}
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
manager.setStreamVolume(
AudioManager.STREAM_SYSTEM, progress,
AudioManager.FLAG_SHOW_UI);
Toast.makeText(volman.this, "Now going Silent!", Toast.LENGTH_LONG).show();
}
});
return false;
}
});
}
private DialogPreference findPreference(String string) {
return null;
}
}
Here is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="path.to.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".volman"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.PREFERENCE" />
</intent-filter>
</activity>
</application>
You start with
DialogPreference dp = (DialogPreference) findPreference("mediavolume");
but then
private DialogPreference findPreference(String string) {
return null;
}
So you're getting a NullPointerException exception in dp.setOnPreferenceChangeListener(...) , no?

"Unfortunately (my app) has Stopped" error when trying to run my application

(Home.java), I am trying to create a loading screen that eventually leads to this page (Home.java.)
package com.androidpeople.splash;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class VirtualSkiInstructor extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("Main Activity");
setContentView(textView);
}
}
Loading Screen (SplashScreen.java). This is the code that creates the Splash Screen, and the loading page
package com.androidpeople.splash;
import your.custom.splash.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SplashScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final int welcomeScreenDisplay = 3000;
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {
int wait = 0;
#Override
public void run() {
try {
super.run();
while (wait < welcomeScreenDisplay) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
System.out.println("EXc=" + e);
} finally {
startActivity(new Intent(SplashScreen.this,
VirtualSkiInstructor.class));
finish();
}
}
};
welcomeThread.start();
}
}
(splash.xml)
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center" android:background="#6B8AAD">
<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="18sp"
android:textStyle="bold" android:textColor="#fff"></TextView>
</LinearLayout>
(strings.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Compass</string>
<string name="app_name">Compass</string>
</resources>
Add below code to add VirtualSkiInstructor activity into your manifest file.
<activity
android:name=".VirtualSkiInstructor"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Add VirtualSkiInstructor Activity in the AndroidManifest.xml file.
<activity android:name=".VirtualSkiInstructor"> </activity>

Problem with Camera.startPreview()

I'm following this tutorial to learn Android's Camera API. I made it the end of the first section (right before "Providing an overlay" begins), and I'm getting the following error:
06-20 23:33:50.903: ERROR/AndroidRuntime(1114): at android.hardware.Camera.startPreview(Native Method)
06-20 23:33:50.903: ERROR/AndroidRuntime(1114): at com.sobel.Sobel.startCamera(Sobel.java:73)
06-20 23:33:50.903: ERROR/AndroidRuntime(1114): at com.sobel.Sobel.surfaceChanged(Sobel.java:36)
(Full trace)
Git repo here. Main Activity here. Manifest here.
I checked and re-checked my code and have followed the tutorial to a t, so what could be causing this error?
Try setting the type of Surface in initCamera().
private void initCamera() {
mCamSV = (SurfaceView)findViewById(R.id.surface_camera);
mCamSH = mCamSV.getHolder();
mCamSH.addCallback(this);
**mCamSH.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);**
}
EDIT 1
I am copying all the files here which worked for me with android 2.2 sdk
Activity
package com.stack.camera;
import java.io.IOException;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.FrameLayout;
public class CameraStackActivity extends Activity implements SurfaceHolder.Callback {
private Camera mCam;
private SurfaceView mCamSV;
private SurfaceHolder mCamSH;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
initCamera();
}
#Override
public void onDestroy() {
stopCamera();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
startCamera(holder, width, height);
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
mCam = Camera.open();
try {
mCam.setPreviewDisplay(holder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
private void initCamera() {
mCamSV = (SurfaceView)findViewById(R.id.surface_camera);
mCamSH = mCamSV.getHolder();
mCamSH.addCallback(this);
mCamSH.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private void startCamera(SurfaceHolder sh, int width, int height) {
Camera.Parameters p = mCam.getParameters();
// Camera.Size s = p.getSupportedPreviewSizes().get(0);
p.setPreviewSize(width, height);
mCam.setParameters(p);
try {
mCam.setPreviewDisplay(sh);
} catch (Exception e) {
}
mCam.startPreview();
}
private void stopCamera() {
mCamSH.removeCallback(this);
mCam.stopPreview();
mCam.release();
}
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView android:id="#+id/surface_camera"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
</FrameLayout>
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stack.camera"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="CameraStackActivity"
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>
Check if it still doesnt work out for you.

Categories

Resources