Android主屏幕项目,RuntimeException

时间:2010-12-28 10:05:04

标签: java android eclipse android-2.2-froyo runtimeexception

我开发了一个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

MainActivity

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_screen left_screen right_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> 

任何想法如何解决?

3 个答案:

答案 0 :(得分: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使用不同的颜色,当您运行项目时,可以在屏幕上拖动并显示一个新区域。

alt text

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:0)

DragableSpaceViewGroupDragableSpace不是Activity。主屏幕是活动。因此,DragableSpace不能成为主屏幕。