سؤال

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?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

Check constructors that your HomeView implements:

 public HomeView(Context context, AttributeSet atts) {
     super(context, atts); 
 } 
<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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top