문제

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.

alt text

다른 팁

On a side note, and this is more of an observation, in your DragableSpace code i've noticed two things.

  1. 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.

  2. 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top