0

I'm trying to add a ListView and a Custom View in my layout, but I always get this error:

"...Error inflating class com.example..."

Without the Custom View it works and only the ListView is shown.


Activity:

package com.example.training;

public class PracticeActivity extends ActionBarActivity{
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.practice_layout);
      initListView();  //private function
   }
   //Other stuff
}

Custom View:

package com.example.training;

public class LinearChart extends View{

  LinearChart(Context context, AttributeSet attrs){
      super(context,attrs);
  }

  LinearChart(Context context){
      super(context);
  }

  LinearChart(Context context, AttributeSet attrs, int defStyle){
      super(context,attrs, defStyle);
  }

  @Override
  protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);
      canvas.drawColor(Color.WHITE);
      Paint paint = new Paint();
      paint.setColor(Color.BLUE);
      paint.setStrokeWidth(10);
      canvas.drawLine(10, 10, 30, 30, paint); 
  }
}

XML-Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/practice_listview"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_centerHorizontal="true" >
    </ListView>

    <view
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/practice_listview"
        class="com.example.training.LinearChart" />

</RelativeLayout>
1
  • Make sure you clean your project to refresh the generated files. Commented Oct 16, 2014 at 10:15

3 Answers 3

1

There are no public constructors on your class. Make sure you override the constructors. It should look like this instead:

  public LinearChart(Context context, AttributeSet attrs){
      super(context,attrs);
  }

  public LinearChart(Context context){
      super(context);
  }

  public LinearChart(Context context, AttributeSet attrs, int defStyle){
      super(context,attrs, defStyle);
  }

Notice the addiction of public modifier.

Sign up to request clarification or add additional context in comments.

2 Comments

Can not believe that this is the solution :D Thanks alot
You can check your logcat for more information. Normaly this will throw Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet] which tells you that the constructor was not found. The default access modifier is private so LayoutInflater couldn't find it. Have a great day.
0
  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
    android:id="@+id/practice_listview"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_centerHorizontal="true" >
</ListView>

<com.example.training.LinearChart
    android:id="@+id/view1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/practice_listview"
     />

  </RelativeLayout>

1 Comment

show me the exception. but first clean the project and rebuild it by placing my code in xml.
0

Instead of <view in your xml file, you need the fully qualified name of the class (or use a new xml namespace). So, in this example, your xml might look like:

<com.example.training.LinearChart
    android:id="@+id/view1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

(and therefore remove the class: attribute in your current XML). This should then inflate correctly.

1 Comment

That's the same thing. You don't "need" to do this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.