gpt4 book ai didi

使用 MpAndroidChart api 饼图 api 时出现 java.lang.IllegalArgumentException : DrawerLayout must be measured with MeasureSpec. EXACTLY.error

转载 作者:太空宇宙 更新时间:2023-11-04 12:02:02 26 4
gpt4 key购买 nike

嗨,我正在尝试在饼图顶部实现一个抽屉导航,但它不断崩溃,但它似乎打开了其他 Activity ,非常感谢。我使用 apptheme 作为主题,使用 MpAndroidChart api 作为饼图

Java 代码:

package com.example.gurpal.expenseapp;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.Utils;
import java.util.ArrayList;

public class PieChartActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
{ActionBarDrawerToggle toggle;
Database db;

private static final int DURATION = 5000;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pie_chart);
getSupportActionBar().setDisplayShowCustomEnabled(true);
db = new Database(this);
Cursor res= db.ViewData();


PieChart expensesPieChart = (PieChart)findViewById(R.id.chart);

ArrayList<Entry> entries = new ArrayList<>();
ArrayList<String> expensesCategories = new ArrayList<>();
if(res.getCount() > 0)
{
int count = 0;
res.moveToFirst();
do
{
entries.add(new Entry((float)res.getDouble(2), count));
expensesCategories.add(res.getString(1));
count++;
} while(res.moveToNext());
}

PieDataSet expensesDataSet = new PieDataSet(entries, null);

PieData expensesData = new PieData(expensesCategories, expensesDataSet);

expensesDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
expensesPieChart.setDrawSliceText(false);
expensesPieChart.setCenterText("Expenses");
expensesPieChart.setCenterTextSize(30);
expensesPieChart.setDescription("Money spent (in $CAD)");
expensesDataSet.setValueTextSize(25);
expensesPieChart.setDescriptionColor(Color.WHITE);
expensesPieChart.setData(expensesData);
Legend legend = expensesPieChart.getLegend();
legend.setFormSize(15); // set the size of the legend forms/shapes
legend.setForm(Legend.LegendForm.CIRCLE); // set what type of form/shape should be used
legend.setWordWrapEnabled(true);
legend.setTextSize(15);
legend.setTextColor(Color.WHITE);
expensesPieChart.animateY(DURATION);
expensesPieChart.invalidate();
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

if(toggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

switch (item.getItemId()) {
case R.id.nav_menu_1:
startActivity(new Intent(PieChartActivity.this, NavDrawer.class));
break;

case R.id.nav_menu_2:
startActivity(new Intent(PieChartActivity.this, Expenses.class));
break;

case R.id.nav_menu_3:
startActivity(new Intent(PieChartActivity.this, Income.class));
break;

case R.id.nav_menu_4:
startActivity(new Intent(PieChartActivity.this, PieChartActivity.class));
break;

case R.id.nav_menu_5:
Toast.makeText(PieChartActivity.this, "menu button test 5", Toast.LENGTH_SHORT).show();
break;

}

DrawerLayout dl = (DrawerLayout) findViewById(R.id.drawerLayout);
if(dl.isDrawerOpen(GravityCompat.START))
dl.closeDrawer(GravityCompat.START);

return false;
}

}

XML 代码

    <?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.gurpal.expenseapp.PieChartActivity"
android:id="@+id/drawerLayout">


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">


<com.github.mikephil.charting.charts.PieChart
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/chart"
/>
</RelativeLayout>


<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:menu="@menu/nav_menu"
android:layout_gravity="start"
android:id="@+id/navigationView">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

最佳答案

尝试删除饼图tools:context:

tools:context="com.example.gurpal.expenseapp.PieChartActivity"

关于使用 MpAndroidChart api 饼图 api 时出现 java.lang.IllegalArgumentException : DrawerLayout must be measured with MeasureSpec. EXACTLY.error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40813848/

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