我正在嘗試使用自定義視圖創建應用程序,並且我不斷收到「錯誤膨脹類」。 它一定是我錯過了一些關於自定義視圖的基礎知識,但我不知道是什麼。這是一個帶有自定義視圖的非常簡單的程序,還需要什麼才能使其工作? (注意:爲了解決這個問題,我把SurfaceView類放在Activity類中,這並不是大型應用程序中的情況,我沒有在這裏顯示AndroidManifest.xml文件,但它正是通過在蝕嚮導生成)SurfaceView和錯誤膨脹類
這裏是java:
package com.mypackage;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceView;
public class SimpleCustomViewActivity extends Activity {
class TheView extends SurfaceView{
private static final String TAG = "TheView";
public TheView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i(TAG,"TheView(" + context + "," + attrs + ")");
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_layout);
TheView v = (TheView) findViewById(R.id.myview);
}
}
這裏是文件RES /佈局/ simple_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.mypackage.SimpleCustomView.TheView
android:id="@+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
我改變了簡單查看應用程序將反映您的建議。它仍然墜毀。有一個很長的堆棧跟蹤,它似乎最初由com.mypackage.SimpleCustomViewActivity.TheView上的ClassNotFoundException引發。所以我也試着讓班級公開,但它仍然崩潰。 – JimC
另外,我注意到當我們指com.mypackage.SimpleCustomViewActivity時,我們都寫了com.mypackage.SimpleCustomView。但是這個特殊的錯誤似乎從來沒有出現在我測試過的源代碼中,並且當我將它複製到這個Web窗體時必須被引入。 – JimC
好的。我得到它的工作。我必須將TheView移出到另一個文件,而不是將其作爲子類。非常感謝你的幫助。最簡單的事情有時可能會讓開發人員的腦子數小時。我希望上述錯誤在編譯時被捕獲。 – JimC