0

Android ホームスクリーンを開発しました。コードは正しいようですが、プロジェクトを実行すると次のエラーが発生します。

java.lang.RuntimeException:
Unable to instantiate activity ComponentInfo{com.matthieu.launcher/
com.matthieu.launcher.DragableSpace}:
java.lang.InstantiationException: com.matthieu.launcher.DragableSpace

完全なログは次のとおりです: http://pastebin.com/iYtYW2W6

このコードは、Android Launcher から直接取得したものです。

DragableSpace : http://pastebin.com/jpWDtFPF

メインアクティビティ:

public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DragableSpace space = new DragableSpace(this.getApplicationContext());
        setContentView(space);
    }
}

値フォルダー内のattr.xml :

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="DragableSpace">
        <attr name="default_screen" format="integer"/>
    </declare-styleable>
</resources>

3 つの xml ファイルinitial_screenleft_screenright_screenは、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>

マニフェスト:

<?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> 

それを修正する方法はありますか?

4

3 に答える 3

1

余談ですが、これは観察にすぎませんが、DragableSpace コードで 2 つのことに気付きました。

  1. public DragableSpace(Context context, AttributeSet attrs) {... a.recycle(); を呼び出していません。acquireStyledAttributes のすべてのプロパティにアクセスした後。これは悪いと思います。

  2. Javaを手に入れたばかりなので、完全にはわかりませんが、super.DragableSpace(...)をthis.DragableSpace(...)に変更する必要があることがわかりました。これは私のコンポーネントに固有のものである可能性がありますが、それが正しいかどうかは完全にはわかりませんでした.

于 2011-01-03T14:58:20.970 に答える
1

現在のコードを簡単に見てみました。子ビューがないため、DragableSpace の 169 行目の NullPointerException - MainActivity に子ビューを追加して、これを修正します。

TextView child = new TextView(this);        
    child.setText("This is a Test");
    space.addView(child);

したがって、コードが実行され、タッチ イベントが LogCat に記録されますが、何も起こらないように見えます (画面上でドラッグして再配置できる 3 つのスペースが必要だと思います)。いくつかのドラッグ ジェスチャでは少し高いですが、レイアウト xml ファイルは使用されていないようです。また、現在の「画面」がデバイスの幅と高さ全体である場合、どこにドラッグされますか?

更新: 前の投稿の xml レイアウトを main.xml として追加します。

<?xml version="1.0" encoding="utf-8"?>

ここで、MainActivity でコンテンツを次のように設定します。

setContentView(R.layout.main);

また、何が起こっているかを簡単に確認できるように、背景色をさまざまな色に設定し、strings.xml ファイルに次の色を追加します。

<color name="green">#21d081</color>
<color name="blue">#00A0FF</color>
<color name="orange">#EA6700</color>

また、画面の xml ファイルを編集して色を追加します。たとえば、left_screen.xml ファイルに次の属性を追加します。

android:background="@color/blue"

また、initial_screen.xml と right_screen.xml に別の色を使用すると、プロジェクトを実行するときに、画面上でドラッグして新しい領域を表示できます。

代替テキスト

于 2011-01-03T14:46:24.003 に答える
0

DragableSpaceですViewGroupDragableSpaceではありませんActivity。ホーム画面はアクティビティです。したがって、DragableSpaceホーム画面にすることはできません。

于 2010-12-28T11:18:04.760 に答える