我需要在lib中创建自定义视图。我的代码: 自定义lib中的自定义视图(在具有一个类(是视图)的项目中):
public class PagingHTMLView extends SurfaceView {
public PagingHTMLView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public PagingHTMLView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PagingHTMLView(Context context) {
super(context);
}
}
我创建测试项目并右键单击以测试项目=>构建路径=>配置构建路径=>选择项目选项卡并选择lib项目。 我在测试项目中创建了一个活动:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PagingHTMLView p=new PagingHTMLView(this);
p.setLayoutParams(new LayoutParams(-1, -1));
RelativeLayout l=(RelativeLayout) findViewById(R.id.layout);
l.addView(p);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.inazaruk.app"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/layout">
</RelativeLayout>
并收到错误:
06-09 13:52:09.181: E/AndroidRuntime(265): Uncaught handler: thread main exiting due to uncaught exception
06-09 13:52:09.200: E/AndroidRuntime(265): java.lang.NoClassDefFoundError: theTG.race.PagingHTMLView.PagingHTMLView
06-09 13:52:09.200: E/AndroidRuntime(265): at com.example.testhtmlview.MainActivity.onCreate(MainActivity.java:16)
06-09 13:52:09.200: E/AndroidRuntime(265): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-09 13:52:09.200: E/AndroidRuntime(265): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-09 13:52:09.200: E/AndroidRuntime(265): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-09 13:52:09.200: E/AndroidRuntime(265): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-09 13:52:09.200: E/AndroidRuntime(265): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-09 13:52:09.200: E/AndroidRuntime(265): at android.os.Handler.dispatchMessage(Handler.java:99)
06-09 13:52:09.200: E/AndroidRuntime(265): at android.os.Looper.loop(Looper.java:123)
06-09 13:52:09.200: E/AndroidRuntime(265): at android.app.ActivityThread.main(ActivityThread.java:4363)
06-09 13:52:09.200: E/AndroidRuntime(265): at java.lang.reflect.Method.invokeNative(Native Method)
06-09 13:52:09.200: E/AndroidRuntime(265): at java.lang.reflect.Method.invoke(Method.java:521)
06-09 13:52:09.200: E/AndroidRuntime(265): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-09 13:52:09.200: E/AndroidRuntime(265): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-09 13:52:09.200: E/AndroidRuntime(265): at dalvik.system.NativeStart.main(Native Method)
如果在测试项目中我使用代码创建活动:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.inazaruk.app"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/layout">
<theTG.race.PagingHTMLView.PagingHTMLView
android:id="@+id/test"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</RelativeLayout>
我收到错误:
06-09 14:08:33.080: E/AndroidRuntime(290): Uncaught handler: thread main exiting due to uncaught exception
06-09 14:08:33.110: E/AndroidRuntime(290): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testhtmlview/com.example.testhtmlview.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class theTG.race.PagingHTMLView.PagingHTMLView
06-09 14:08:33.110: E/AndroidRuntime(290): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
06-09 14:08:33.110: E/AndroidRuntime(290): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-09 14:08:33.110: E/AndroidRuntime(290): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-09 14:08:33.110: E/AndroidRuntime(290): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-09 14:08:33.110: E/AndroidRuntime(290): at android.os.Handler.dispatchMessage(Handler.java:99)
06-09 14:08:33.110: E/AndroidRuntime(290): at android.os.Looper.loop(Looper.java:123)
06-09 14:08:33.110: E/AndroidRuntime(290): at android.app.ActivityThread.main(ActivityThread.java:4363)
06-09 14:08:33.110: E/AndroidRuntime(290): at java.lang.reflect.Method.invokeNative(Native Method)
06-09 14:08:33.110: E/AndroidRuntime(290): at java.lang.reflect.Method.invoke(Method.java:521)
06-09 14:08:33.110: E/AndroidRuntime(290): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-09 14:08:33.110: E/AndroidRuntime(290): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-09 14:08:33.110: E/AndroidRuntime(290): at dalvik.system.NativeStart.main(Native Method)
06-09 14:08:33.110: E/AndroidRuntime(290): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class theTG.race.PagingHTMLView.PagingHTMLView
06-09 14:08:33.110: E/AndroidRuntime(290): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
06-09 14:08:33.110: E/AndroidRuntime(290): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
06-09 14:08:33.110: E/AndroidRuntime(290): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
06-09 14:08:33.110: E/AndroidRuntime(290): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-09 14:08:33.110: E/AndroidRuntime(290): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-09 14:08:33.110: E/AndroidRuntime(290): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
06-09 14:08:33.110: E/AndroidRuntime(290): at android.app.Activity.setContentView(Activity.java:1622)
06-09 14:08:33.110: E/AndroidRuntime(290): at com.example.testhtmlview.MainActivity.onCreate(MainActivity.java:15)
06-09 14:08:33.110: E/AndroidRuntime(290): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-09 14:08:33.110: E/AndroidRuntime(290): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-09 14:08:33.110: E/AndroidRuntime(290): ... 11 more
06-09 14:08:33.110: E/AndroidRuntime(290): Caused by: java.lang.ClassNotFoundException: theTG.race.PagingHTMLView.PagingHTMLView in loader dalvik.system.PathClassLoader@43b869c0
06-09 14:08:33.110: E/AndroidRuntime(290): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
06-09 14:08:33.110: E/AndroidRuntime(290): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
06-09 14:08:33.110: E/AndroidRuntime(290): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
06-09 14:08:33.110: E/AndroidRuntime(290): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
06-09 14:08:33.110: E/AndroidRuntime(290): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
06-09 14:08:33.110: E/AndroidRuntime(290): ... 20 more