Android homescreen project, RuntimeException - java

I developed a android homescreen, the code seems right but when I run the project I get the following error:
java.lang.RuntimeException:
Unable to instantiate activity ComponentInfo{com.matthieu.launcher/
com.matthieu.launcher.DragableSpace}:
java.lang.InstantiationException: com.matthieu.launcher.DragableSpace
Here is the complete log: http://pastebin.com/iYtYW2W6
The code is straight from the Android Launcher.
DragableSpace: http://pastebin.com/jpWDtFPF
MainActivity:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DragableSpace space = new DragableSpace(this.getApplicationContext());
setContentView(space);
}
}
attr.xml in values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="DragableSpace">
<attr name="default_screen" format="integer"/>
</declare-styleable>
</resources>
Three xml files initial_screen, left_screen and right_screen, all have same code apart from the id:
<LinearLayout android:id="#+id/center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
</LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.matthieu.launcher"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<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>
Any ideas how to fix that?

Had a quick look at the code as it is now. NullPointerException at line 169 of DragableSpace, due to no child view - fix this by adding a child view in the MainActivity:
TextView child = new TextView(this);
child.setText("This is a Test");
space.addView(child);
So now the code runs and logs the touch events to LogCat but nothing appears to happen (I'm guessing you want 3 spaces which can be dragged and repositioned on the screen) - some initial problems, I think, the SNAP_VELOCITY value of 1000 seems a bit high to me for some dragging gestures, none of the layout xml files appear to be used, and if the current 'screen' is the full width and height of the device, where does it get dragged to?
Updated: Add the previous post's xml layout as the main.xml:
<?xml version="1.0" encoding="utf-8"?>
Now, in the MainActivity, set the content as:
setContentView(R.layout.main);
Also, to make it easier to see what's happening, I set the background colours to different colours - add the following colours to the strings.xml file:
<color name="green">#21d081</color>
<color name="blue">#00A0FF</color>
<color name="orange">#EA6700</color>
And edit the screen xml files to add a colour e.g. in the left_screen.xml file, add the following attribute:
android:background="#color/blue"
And use a different colour for the initial_screen.xml and the right_screen.xml and when you run the project, you can drag across the screen and reveal a new area.

On a side note, and this is more of an observation, in your DragableSpace code i've noticed two things.
In public DragableSpace(Context context, AttributeSet attrs) {... you haven't called a.recycle(); after you've accessed all your properties of obtainStyledAttributes. This is bad i think.
I'm not entirely sure, as i've just picked up java, but i found that i had to change super.DragableSpace(...) to this.DragableSpace(...). This could have just been particular to my component, but wasn't entirely sure if that was correct or not.

DragableSpace is a ViewGroup. DragableSpace is not an Activity. Home screens are activities. Hence, DragableSpace cannot be a home screen.

Related

How do I add a custom picture and title to an activity's top bar

I'm targeting API level 9.
My main activity's top bar looks like this:
AndroidManifest.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dabestappsever.bigtext" >
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name">
<activity
android:name="com.dabestappsever.bigtext.MainActivity"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
... MORE CODE HERE ...
</application>
</manifest>
res/menu/menu_main.xml looks like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
</menu>
I tried adding #drawable/logo to the Manifest, but no luck.
How do I add a custom icon with a custom title next to it for my main activity?
Try inserting these 2 lines inside the Activity open tag
android:icon="#drawable/<icon>"
android:label="#string/<title>"
Looks like I found part of the answer: How to set different label for launcher rather than activity title?
When I added android:label="#string/app_name" to <intent-filter> open tag, I was able to put android:label="#string/main_activity_label" in <activity> open tag, and the app label was #string/app_name while the bar in MainActivity read #string/main_activity_label
Still no luck on the icon though...
To set icon in ActionBar add getSupportActionBar().setIcon(R.drawable.icon); in onCreate() method of your activity.
public class MainActivity extends ActionBarActivity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //your layout
getSupportActionBar().setIcon(R.drawable.icon); //this will set icon in actionbar
....
....
}
}
And to set title in actrionbar write following in tag of manifest file
android:label="#string/activity_title"

Simple map based app not working

I'm attempting to get a simple map based app running but when I install on device and try to "open" I receive the error "unfortunately application has stopped" On emulator I receive the message :
[2014-03-18 21:37:16 - FirstMap] Installation error: Unknown failure
[2014-03-18 21:37:16 - FirstMap] Please check logcat output for more details.
[2014-03-18 21:37:16 - FirstMap] Launch canceled!
Why do I receive this message ? I think my code looks good as I have followed these steps : https://developers.google.com/maps/documentation/android/v1/hello-mapview ?
Here is my code :
MainActivity.java :
package com.example.firstmap;
import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class MainActivity extends MapActivity {
#Override
protected boolean isRouteDisplayed() {
return false;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
}
}
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="mykey"
/>
AndroidManifect.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstmap"
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.firstmap.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>
Refer to my post here:
Want to use GoogleMaps - OnMyLocationChangeListener but can't implement it? Any other options
I have described what exactly to use and how.
Hope you find it helpful .. :)
There are many things missing from your manifest file. Try following the official instructions on how to use google maps google map instructions
The instructions you followed refer to version 1 google maps for android which is deprecated.
Use Google Map Api V2, The important changes that you will have to make is
1) Make use of
<fragment
android:id="#+id/map"
android:name="android.app.DialogFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
i.e You have to use fragment tag in activity_main.xml instead of the mapview tag.
2) Refer Google play services.....
example: Google Maps Can't add reference to Play Services
Best Link to refer for newbies implementing Google Maps Api V2: http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/

Displaying Images in Android using Java

I am splitting this off from my other thread here: Display image in popout window after button is clicked -- Android/Java
The old thread got very convoluted and confusing, and a bit off-topic, so I wanted to create another one that is clearer with more information.
I am trying to display an image in Android using an image file path that changes every time the app is run. I know there are ways to declare resources in the XML layout file, but since the picture I'd like to display is taken from the camera, I can't do it that way. I need to be able to display the image without hardcoding it.
The following is the code that I have:
photo_viewer.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pictureViewer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/viewImage" />
</RelativeLayout>
Java method to display image:
public void viewPhoto(String file){
ImageView imageView = new ImageView(getApplicationContext());
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Bitmap image = BitmapFactory.decodeFile(file);
imageView.setImageBitmap(image);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.pictureViewer);
rl.addView(imageView, lp);
}
However, when I run the above code the application crashes, saying it has stopped working. "file" is a string denoting the directory where the image from the camera is stored, and I know this part is correct. I can use it (using a different method) to change the wallpaper. It's just displaying it that is causing me trouble. I have tried various methods to simply display the image (dialogs, "drawable"s, bitmap factory, etc) and had the same problem with all of them. I feel like it's something simple that I have missed, since I'm new to Android app development. I'm hoping one of you folks might be able to shed some light on this.
EDIT:
I have decided to try another route, but I am still getting a null pointer exception. Here is all my code:
main class, viewPhoto method:
/* View photo */
public void viewPhoto(String file){
Intent imageView = new Intent(this, PhotoViewer.class);
imageView.putExtra("directory", file);
startActivity(imageView);
}
PhotoViewer class:
package com.androidproject;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class PhotoViewer extends Activity {
public String file;
ImageView image;
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.photo_viewer);
//get file path of image
Bundle extras = getIntent().getExtras();
file = extras.getString("directory");
Drawable drawable = Drawable.createFromPath(file);
image.setImageDrawable(drawable);
}
}
photo_viewer.xml (created using the Eclipse tool, so it's not just any xml file)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/photo_viewer"
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=".PhotoViewer" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/viewImage"/>
</RelativeLayout>
Project manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Main"
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=".PhotoViewer"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.androidproject.PhotoViewer" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
The app continues to crash and tell me "unfortunately it stopped working". Here's the logcat of the error:
As always, help is appreciated!
The null pointer exception is because
Drawable drawable = Drawable.createFromPath(file);
image.setImageDrawable(drawable);
is referencing an uninitiated variable (image) which is null
My best guess would be that to eliminate said error you'd want to initiate the variable
ImageView image = (ImageView)findViewById(R.id.imageView);

Android warning: Possible overdraw, root element paints

In my android app, I am getting the warning about the possible overdraw. I am trying to follow the thread here to fix it:
"Possible overdraw: Root element paints background "
But If I try it, it makes all my layouts messed up, like the buttons are in different places and different looking, and if I open a dialog box, its width is really small...
I made a theme here:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
<item name="android:background">#drawable/bg_normal</item>
</style>
</resources>
Then in manifest I have
<activity
android:name="com.activity.idsconnect.ActivityAdminMainMenu"
android:label="#string/title_activity_activity_admin_main_menu"
android:theme="#style/MyTheme"
android:screenOrientation="portrait" >
</activity>
And then removed the background attribute from the root linear layout on the activity.
Am I doing something wrong here? Does anyone know?
Thanks.

How to add a background theme and/or background wallpaper to my app?

How to add a background theme and/or background wallpaper to my app? Right now my app background is plane.
Create an XML file in the values directory that contains the style. It could be something like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="YourTheme" parent="#android:style/Theme">
<item name="android:windowBackground">#drawable/your_background</item>
</style>
</resources>
For the case above to work, you need to have an image called your_background.png in the drawable folder. Then, you can apply that style to either your whole app or one specific activity by doing this in your manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bla.bla">
<application android:theme="#style/YourTheme"> <!-- This is for the whole app -->
<activity android:name="bla.bla.YourActivity"
android:theme="#style/SplashTheme"/><!-- This is for a specific activity -->
</application>
</manifest>
You might be using some layout XML for UI. Set layout (linear, relative, etc) background with any image you want to set your background to. Make sure that the image is available in the res/drawable folder.

Categories