我是Android新手。我正在尝试创建一个创建饼图的简单应用程序。
我有2个活动,一个是MainActivity,另一个是PieChart,都扩展了Activity,我有一个扩展View的MyView类。
在MainActivity中,我有一个绑定到onClick事件的按钮(设置为android:onClick =" createPieChart" Button XML属性),当我点击它时,一个名为' creaTorta()'应该更改活动并创建饼图。
这是MainActivity.java:
public class MainActivity extends Activity {
private View vista;
public void creaPieChart(View myView){
Intent viewPieChart = new Intent(this.getApplicationContext(), PieChart.class);
startActivity(viewPieChart);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vista= (View)findViewById(R.id.viewtorta1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是PieChart活动:
public class PieChart extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Log.d("Point", "Here I am!");
setContentView(R.layout.activity_la_torta);
Log.d("Point", "Here I am again!");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.la_torta, menu);
return true;
}
}
这是MyView.java:
public class MyView extends View {
private float cx=0,cy=0;
private RectF r = new RectF(100,100,430,430);
private float valori[] = {10,20,10,30,25,5};
private Paint p,ct;
public MyView(Context context) {
super(context);
p=new Paint();
p.setAntiAlias(true);
ct=new Paint();
ct.setAntiAlias(true);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);
float degStart=0,x,y;
int i=0;
ct.setColor(Color.argb(255, 255, 255, 255));
for (float count : valori){
float degVal=(float) (count*3.6);
p.setColor(Color.argb(255, 0, 125, 255-i));
x=(float)(Math.cos((((degStart-90))*Math.PI)/180)*200)+265 ;
y=(float)(Math.sin((((degStart-90))*Math.PI)/180)*200)+265;
canvas.drawArc(r, degStart-90 , degVal, true, p);
canvas.drawText(""+degVal, x ,y , ct);
degStart+=degVal;
i+=50;
}
}
}
当我点击按钮时,我收到以下错误: 致命的主要原因 java.lang.RuntimeException:无法启动活动ComponentInfo() android.view.InflateException:二进制XML文件第18行:错误通知类it.adriano.piechart.MyView
编辑:这是activity_la_torta.xml文件。当我在PieChart onCreate()中调用setContentView(R.layout.activity_la_torta)时应用程序崩溃;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="300dp"
android:layout_height="500dp"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<Button
android:id="@+id/chiuditorta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<it.adriano.unatorta.MyView
android:id="@+id/myView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:id="@+id/viewtorta1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
这是完整的堆栈跟踪(对于缩进很抱歉,如果有更好的方式发布,请告诉我):
03-14 13:41:52.009: E/AndroidRuntime(2189):
FATAL EXCEPTION: main
03-14 13:41:52.009: E/AndroidRuntime(2189):
java.lang.RuntimeException: Unable to start activity ComponentInfo{it.adriano.unatorta/it.adriano.unatorta.LaTorta}:
android.view.InflateException: Binary XML file line #18:
Error inflating class it.adriano.unatorta.MyView
03-14 13:41:52.009: E/AndroidRuntime(2189):
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-14 13:41:52.009: E/AndroidRuntime(2189):
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-14 13:41:52.009: E/AndroidRuntime(2189):
at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-14 13:41:52.009: E/AndroidRuntime(2189):
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-14 13:41:52.009: E/AndroidRuntime(2189):
at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 13:41:52.009: E/AndroidRuntime(2189):
at android.os.Looper.loop(Looper.java:137)
03-14 13:41:52.009: E/AndroidRuntime(2189):
at android.app.ActivityThread.main(ActivityThread.java:4745)
03-14 13:41:52.009: E/AndroidRuntime(2189):
at java.lang.reflect.Method.invokeNative(Native Method)
03-14 13:41:52.009: E/AndroidRuntime(2189):
at java.lang.reflect.Method.invoke(Method.java:511)
03-14 13:41:52.009: E/AndroidRuntime(2189):
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-14 13:41:52.009: E/AndroidRuntime(2189):
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-14 13:41:52.009: E/AndroidRuntime(2189):
at dalvik.system.NativeStart.main(Native Method)
03-14 13:41:52.009: E/AndroidRuntime(2189):
Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class it.adriano.unatorta.MyView
03-14 13:41:52.009: E/AndroidRuntime(2189): at android.view.LayoutInflater.createView(LayoutInflater.java:596)
03-14 13:41:52.009: E/AndroidRuntime(2189): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
03-14 13:41:52.009: E/AndroidRuntime(2189): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-14 13:41:52.009: E/AndroidRuntime(2189): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-14 13:41:52.009: E/AndroidRuntime(2189): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-14 13:41:52.009: E/AndroidRuntime(2189): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-14 13:41:52.009: E/AndroidRuntime(2189): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
03-14 13:41:52.009: E/AndroidRuntime(2189): at android.app.Activity.setContentView(Activity.java:1867)
03-14 13:41:52.009: E/AndroidRuntime(2189): at it.adriano.unatorta.LaTorta.onCreate(LaTorta.java:21)
03-14 13:41:52.009: E/AndroidRuntime(2189): at android.app.Activity.performCreate(Activity.java:5008)
03-14 13:41:52.009: E/AndroidRuntime(2189): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-14 13:41:52.009: E/AndroidRuntime(2189): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-14 13:41:52.009: E/AndroidRuntime(2189): ... 11 more
03-14 13:41:52.009: E/AndroidRuntime(2189): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
03-14 13:41:52.009: E/AndroidRuntime(2189): at java.lang.Class.getConstructorOrMethod(Class.java:460)
03-14 13:41:52.009: E/AndroidRuntime(2189): at java.lang.Class.getConstructor(Class.java:431)
03-14 13:41:52.009: E/AndroidRuntime(2189): at android.view.LayoutInflater.createView(LayoutInflater.java:561)
03-14 13:41:52.009: E/AndroidRuntime(2189): ... 22 more
我该如何解决?我不明白它为什么不启动startActivity(viewPieChart);正常。我在弄什么?
感谢您的关注。
答案 0 :(得分:1)
java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
您的MyView
类没有从XML充气的视图所需的MyView(Context, AttributeSet)
构造函数重载。
例如,更改当前的构造函数
public MyView(Context context) {
super(context);
p=new Paint();
p.setAntiAlias(true);
ct=new Paint();
ct.setAntiAlias(true);
}
使用共享init函数的两个必需构造函数重载:
public MyView(Context context) {
super(context);
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
p=new Paint();
p.setAntiAlias(true);
ct=new Paint();
ct.setAntiAlias(true);
}