Android自定义视图导致强制关闭

发布于 2024-09-28 05:33:33 字数 851 浏览 13 评论 0原文

我在 src > 有一个自定义视图myproject.test> HomeView

在我的主布局 xml 中,我有以下内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_root"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <myproject.test.HomeView
        android:id="@+id/home_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
    </myproject.test.HomeView>

</LinearLayout>

在 HomeActivity 中,我在 onCreate 方法中进行了类似的调用。

setContentView(R.layout.main);
HomeView mHomeView = (HomeView) this.findViewById(R.id.home_view);

调用 setContentView 方法时应用程序强制关闭。看来我的主xml不正确。

有什么想法吗?

I have a custom view in src > myproject.test > HomeView

In my main layout xml I have the following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_root"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <myproject.test.HomeView
        android:id="@+id/home_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
    </myproject.test.HomeView>

</LinearLayout>

In the HomeActivity I have a call like this in the onCreate method.

setContentView(R.layout.main);
HomeView mHomeView = (HomeView) this.findViewById(R.id.home_view);

The app force closed when the setContentView method is called. It seems that my main xml is not correct.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海少女心 2024-10-05 05:33:33

你的意思是它没有到​​达

HomeView mHomeView = (HomeView) this.findViewById(R.id.home_view);

并在它之前崩溃?

检查您的构造函数是否

HomeView(final Context context, final AttributeSet attrs){
     super(context, attrs);

需要

HomeView(final Context context){
     super(context);

AttributeSet

Do you mean its not getting to the

HomeView mHomeView = (HomeView) this.findViewById(R.id.home_view);

and crashing on the line before it?

Check if your constructor is

HomeView(final Context context, final AttributeSet attrs){
     super(context, attrs);

and not

HomeView(final Context context){
     super(context);

you need the AttributeSet

负佳期 2024-10-05 05:33:33

检查 HomeView 实现的构造函数:

 public HomeView(Context context, AttributeSet atts) {
     super(context, atts); 
 } 

Check constructors that your HomeView implements:

 public HomeView(Context context, AttributeSet atts) {
     super(context, atts); 
 } 
何止钟意 2024-10-05 05:33:33
<LinearLayout xmlns:android = 
      http://schemas.android.com/apk/res/android"
    android:id="@+id/home_root"
    android:orientation="vertical"

我的布局都没有 @+id。也许您应该将视图设置为home_root。与您核对R.java布局的名称,或者尝试

setContentView(R.layout.home_root);
<LinearLayout xmlns:android = 
      http://schemas.android.com/apk/res/android"
    android:id="@+id/home_root"
    android:orientation="vertical"

None of my layouts have @+id. Maybe you should set the view to home_root. Check with you R.java for the name of the layout, or try

setContentView(R.layout.home_root);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文