我只想要一个继承自" android.widget.Button"的类。并使用它而不是普通的按钮。 当我给自定义按钮充气时,系统无法找到班级。 我的包裹:
com.kahl.test
这是我的Main.axml:
<?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.kahl.test.test.MyButton
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
MyButton.cs:
namespace test {
public class MyButton : Button{
public MyButton (Context context)
:base (context){
Initialize ();
}
public MyButton (Context context, IAttributeSet attrs)
:base (context, attrs){
Initialize ();
}
public MyButton (Context context, IAttributeSet attrs, int defStyle)
:base (context, attrs, defStyle){
Initialize ();
}
void Initialize (){
}
}
}
和MainActivity:
namespace test{
[Activity (Label = "test", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity{
int count = 1;
protected override void OnCreate (Bundle bundle){
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
test.MyButton button = FindViewById<test.MyButton>(Resource.Id.myButton);
button.Click += delegate {
button.Text = string.Format ("{0} clicks!", count++);
};
}
}
}
logcat的:
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] Android.Views.InflateException: Exception of type 'Android.Views.InflateException' was thrown.
[MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System.Runt ime.ExceptionServices/ExceptionDispatchInfo.cs:61
[MonoDroid] at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue*) [0x00084] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/src/Ru ntime/JNIEnv.g.cs:1029
[MonoDroid] at Android.App.Activity.SetContentView (int) [0x00070] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/platfo rms/android-19/src/generated/Android.App.Activity.cs:4879
[MonoDroid] at test.MainActivity.OnCreate (Android.OS.Bundle) [0x00016] in c:\Users\Kahlei\Desktop\test\test\MainActivity.cs:22
[MonoDroid] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/platfo rms/android-19/src/generated/Android.App.Activity.cs:2475
[MonoDroid] at (wrapper dynamic-method) object.d7001677-56ac-4bd2-97b5- 7a496a39e428 (intptr,intptr,intptr) <IL 0x00017, 0x00027>
[MonoDroid] --- End of managed exception stack trace ---
[MonoDroid] android.view.InflateException: Binary XML file line #1: Error inflating class com.kahl.test.test.MyButton
[MonoDroid] at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
[MonoDroid] at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
[MonoDroid] at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
[MonoDroid] at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
[MonoDroid] at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
[MonoDroid] at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
[MonoDroid] at android.app.Activity.setContentView(Activity.java:1929)
[MonoDroid] at md56996cb2aea8f756b255ad7a6f28adcb8.MainActivity.n_onCreate(Native Method)
[MonoDroid] at md56996cb2aea8f756b255ad7a6f28adcb8.MainActivity.onCreate(MainActivity.java:28)
[MonoDroid] at android.app.Activity.performCreate(Activity.java:5231)
[MonoDroid] at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
[MonoDroid] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
[MonoDroid] at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
[MonoDroid] at android.app.ActivityThread.access$800(ActivityThread.java:135)
[MonoDroid] at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
[MonoDroid] at android.os.Handler.dispatchMessage(Handler.java:102)
[MonoDroid] at android.os.Looper.loop(Looper.java:136)
[MonoDroid] at android.app.ActivityThread.main(ActivityThread.java:5017)
[MonoDroid] at java.lang.reflect.Method.invokeNative(Native Method)
[MonoDroid] at java.lang.reflect.Method.invoke(Method.java:515)
[MonoDroid] at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
[MonoDroid] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
[MonoDroid] at dalvik.system.NativeStart.main(Native Method)
[MonoDroid] Caused by: java.lang.ClassNotFoundException: Didn't find class "com.kahl.test.test.MyButton" on path: DexPathList[[zip file "/data/app/com.kahl.test-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.kahl.test-2, /system/lib]]
[MonoDroid] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
[MonoDroid] at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
[MonoDroid] at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
[MonoDroid] at android.view.LayoutInflater.createView(LayoutInflater.java:559)
[MonoDroid] at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
[MonoDroid] ... 22 more
[AndroidRuntime] Shutting down VM
答案 0 :(得分:0)
在我看来,com.kahl.test应该是命名空间,而不是应用程序的包名。