gpt4 book ai didi

java - 圆形布局 : Cannot resolve method onCreate(android. os.Bundle)

转载 作者:搜寻专家 更新时间:2023-11-01 08:40:39 25 4
gpt4 key购买 nike

我想在我的 Android 应用程序中使用 CircularLayout。

我在 MainActivity.java 文件中遇到以下错误:无法解析 onCreate(android.os.Bundle) 方法,即使我已经扩展了 AppCompatActivity。

因为我需要从 AppCompatActivity 和 View 访问方法,所以我使用了嵌套类,错误可能是由于此。不确定如何使用它。我也有以下警告:private inner class Activity is never used。请让我知道延长两个类(class)。我找到了几个答案并使用了它们,包括 Extending from two classesHow to extend 2 classes?但他们似乎都不适合我。我想知道我可以对我的代码进行哪些更改才能使其正常工作。

这是我的 MainActivity.java 文件的样子:

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);
}
}

这是我的 content_main.xml 文件:

<?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>

最佳答案

  1. 我想建议您以更有意义的方式调用 View 的子类。在你的情况下

    私有(private)类 Activity 扩展 View {

  2. 关于 onCreate,这是您怀疑的来源,您将它放在了 View 的子类中。由于 View 没有名为 onCreate 的方法,因此编译器会报错。解决方案是将方法及其内容移动到外部类中,即 extends Activity

  3. 您的 View 子类是内部的和私有(private)的。我怀疑您是否能够在您的 xml 中引用它

关于java - 圆形布局 : Cannot resolve method onCreate(android. os.Bundle),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32912930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com