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。

可拖动空间: 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>

三个 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 的第 169 行出现 NullPointerException - 通过在 MainActivity 中添加子视图来解决此问题:

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

所以现在代码运行并将触摸事件记录到 LogCat,但似乎没有发生任何事情(我猜你想要 3 个可以在屏幕上拖动和重新定位的空格) - 我认为一些初始问题,SNAP_VELOCITY 值 1000 似乎对于一些拖动手势对我来说有点高,似乎没有使用任何布局 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 回答
1

在旁注中,这更像是一种观察,在您的 DragableSpace 代码中,我注意到了两件事。

  1. 在 public DragableSpace(Context context, AttributeSet attrs) {... 你还没有调用 a.recycle(); 在您访问了obtainStyledAttributes 的所有属性之后。我认为这很糟糕。

  2. 我不完全确定,因为我刚刚学习了 java,但我发现我必须将 super.DragableSpace(...) 更改为 this.DragableSpace(...)。这可能只是我的组件特有的,但不完全确定这是否正确。

于 2011-01-03T14:58:20.970 回答
0

DragableSpace是一个ViewGroupDragableSpace不是一个Activity。主屏幕是活动。因此,DragableSpace不能是主屏幕。

于 2010-12-28T11:18:04.760 回答