2

I want to use CircularLayout in my android app.

I'm getting following error in MainActivity.java file: cannot resolve method onCreate(android.os.Bundle) even though I have extended AppCompatActivity.

As I need to access methods from both AppCompatActivity and View, I've used nested classes and the error is probably due of that. Not sure how to use that. I'm also the following warning: private inner class Activity is never used. Please let me know to extend two classes. I've found several answers and used them including Extending from two classes and How to extend 2 classes? but none of them seems to work for me. I would like to know what changes can I make in my code for it to work.

This is how my MainActivity.java file looks like:

package com.example.shalini.circlelayout;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {
private class Activity extends View {
    private final static int TOTAL_DEGREE = 360;
    private final static int START_DEGREE = -90;

    private Paint mPaint;
    private RectF mOvalRect = null;

    private int mItemCount = 5;
    private int mSweepAngle;

    private int mInnerRadius;
    private int mOuterRadius;
    private Bitmap mCenterIcon;
    private int[] mColors = {Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE, Color.CYAN};
    private String[] mTitles = {"APPT CENTER", "MEDS CABINET", "CHECK-IN", "MY TRACKERS", "MY ACCOUNTS"};


    public Activity(Context context) {
        this(context, null);
    }

    public Activity(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Activity(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStrokeWidth(2);

        mSweepAngle = TOTAL_DEGREE / mItemCount;

        mInnerRadius = 125;
        mOuterRadius = 400;

        mCenterIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        int width = getWidth();
        int height = getHeight();

        if (mOvalRect == null) {
            mOvalRect = new RectF(width / 2 - mOuterRadius, height / 2 - mOuterRadius, width / 2 + mOuterRadius, height / 2 + mOuterRadius);
        }

        for (int i = 0; i < mItemCount; i++) {
            int startAngle = START_DEGREE + i * mSweepAngle;
            mPaint.setColor(mColors[i]);
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawArc(mOvalRect, startAngle, mSweepAngle, true, mPaint);

            mPaint.setColor(Color.BLACK);
            mPaint.setStyle(Paint.Style.STROKE);
            canvas.drawArc(mOvalRect, startAngle, mSweepAngle, true, mPaint);

            int centerX = (int) ((mOuterRadius + mInnerRadius) / 2 * Math.cos(Math.toRadians(startAngle + mSweepAngle / 2)));
            int centerY = (int) ((mOuterRadius + mInnerRadius) / 2 * Math.sin(Math.toRadians(startAngle + mSweepAngle / 2)));
            canvas.drawBitmap(mCenterIcon, width / 2 + centerX - mCenterIcon.getWidth() / 2, height / 2 + centerY - mCenterIcon.getHeight() / 2, null);

            mPaint.setColor(Color.BLACK);
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawText(mTitles[i], width / 2 + centerX - mCenterIcon.getWidth() / 2, height / 2 + centerY + mCenterIcon.getHeight(), mPaint);
        }

        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(width / 2, height / 2, mInnerRadius, mPaint);
        canvas.drawBitmap(mCenterIcon, width / 2 - mCenterIcon.getWidth() / 2, height / 2 - mCenterIcon.getHeight() / 2, null);

        super.onDraw(canvas);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

And this is my content_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/lib/com.google.custom"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">
<com.example.shalini.circlelayout.MainActivity.Activity
android:id="@+id/pie"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
custom:dividerWidth="5dp"
custom:innerCircle="@drawable/profile_pic_icon"
custom:innerRadius="50dp"
custom:layoutMode="pie"
custom:sliceDivider="@android:color/transparent" >

<RelativeLayout
    android:id="@+id/appt_center_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_launcher1" >

    <TextView
        android:id="@+id/one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"

        android:text="@string/appcenter"
        android:textStyle="bold" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_launcher2" >

    <TextView
        android:id="@+id/two"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"

        android:text="@string/medscabinet"
        android:textStyle="bold" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_launcher3" >

    <TextView
        android:id="@+id/three"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"

        android:text="@string/cjeckin"
        android:textStyle="bold" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_launcher4" >

    <TextView
        android:id="@+id/four"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"

        android:text="@string/mytrackers"
        android:textStyle="bold" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_launcher5" >

    <TextView
        android:id="@+id/five"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"

        android:text="@string/myaccounts"
        android:textStyle="bold" />
</RelativeLayout>
</com.example.shalini.circlelayout.MainActivity.Activity>

</RelativeLayout>
Community
  • 1
  • 1
shitty_coder
  • 123
  • 3
  • 15
  • Please post your logcat. – Phantômaxx Oct 02 '15 at 17:51
  • Well, I changed by putting onCreate outside View subclass. Also I made View public, now there is no more any java compilation error or warning. But now the problem is with activity_main.xml and content_main.xml file which are not showing any design preview. Also on running this app, I'm getting error **Unfortunately, App has stopped". Please have a look at my logcat http://stackoverflow.com/questions/32914891/android-studio-activity-main-xml-and-content-main-xml-not-showing-design-previe – shitty_coder Oct 02 '15 at 19:59

1 Answers1

3
  1. I would like to suggest you to call your subclass of View in a more meaningfully way. In your case

    private class Activity extends View {

  2. Regarding onCreate, which is the source of your doubts, you put it inside your View's subclass. Since View has no method called onCreate, the compiler complains about it. The solution is to move the method, and its content in the outer class, the one that extends Activity

  3. your subclass of View is inner and private. I doubt you will be ever able to reference it in your xml

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Thanks! That really helped, I changed by putting onCreate outside View subclass. Also I made View public, now there is no more any java compilation error or warning. – shitty_coder Oct 02 '15 at 19:04
  • I strongly suggest you to move your View's subclass in its own compilation unit (a dedicated java file for it) – Blackbelt Oct 02 '15 at 19:08