单击按钮时打开图形活动,但按下时,主布局应用程序中的任何其他按钮崩溃

时间:2014-12-19 16:58:18

标签: android android-activity

我正在使用achartengine在新活动中向我展示饼图。同样在主布局中我有两个Buttons。一个用于图表,另一个用于规则。

按下图表时,它通常会打开新活动,但每当我尝试按下规则时,应用程序都会停止工作。

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button =(Button)findViewById(R.id.rules);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myIntent=new Intent(MyActivity.this,Rules.class);
                startActivity(myIntent);

            }
        });


        // Getting reference to the button btn_chart
        Button btnChart = (Button) findViewById(R.id.btn_chart);

        // Defining click event listener for the button btn_chart
        OnClickListener clickListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                    openChart();
            }
        };

        // Setting event click listener for the button btn_chart of the MainActivity layout
        btnChart.setOnClickListener(clickListener);
    }

    private void openChart(){

        // Pie Chart Section Names
        String[] code = new String[] {
               "Times played", "Gave up!", "Chicken"
        };

        // Pie Chart Section Value, ode bi samo iz baze pokupia tri broja i to bi bilo to
        double[] distribution = { 82, 35, 56 } ;

        // Color of each Pie Chart Sections
        int[] colors = { Color.BLUE, Color.MAGENTA, Color.CYAN, Color.RED
                };

        // Instantiating CategorySeries to plot Pie Chart
        CategorySeries distributionSeries = new CategorySeries(" Statistics");
        for(int i=0 ;i < distribution.length;i++){
            // Adding a slice with its values and name to the Pie Chart
            distributionSeries.add(code[i], distribution[i]);
        }

        // Instantiating a renderer for the Pie Chart
        DefaultRenderer defaultRenderer  = new DefaultRenderer();
        for(int i = 0 ;i<distribution.length;i++){
            SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
            seriesRenderer.setColor(colors[i]);
            seriesRenderer.setDisplayChartValues(true);
            // Adding a renderer for a slice
            defaultRenderer.addSeriesRenderer(seriesRenderer);
        }

        defaultRenderer.setChartTitle("Statistics! ");
        defaultRenderer.setChartTitleTextSize(25);
        defaultRenderer.setZoomButtonsVisible(true);
        defaultRenderer.setLabelsTextSize(25);
        defaultRenderer.setLegendTextSize(20);
         defaultRenderer.setBackgroundColor(Color.argb(255, 254,255, 59));

        // Creating an intent to plot bar chart using dataset and multipleRenderer
        Intent intent = ChartFactory.getPieChartIntent(getBaseContext(), distributionSeries , defaultRenderer, "Statistics");

        // Start Activity
        startActivity(intent);

    }

    @Override
   public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

这是我的主要布局

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

    <Button
            android:id="@+id/btn_chart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" android:text="Pie chart"/>
    <Button
            android:id="@+id/rules"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btn_chart"
            android:layout_marginTop="28dp"
            android:text="Rules"
            android:layout_alignRight="@+id/btn_chart"
            android:layout_alignLeft="@+id/btn_chart"/>


</RelativeLayout>

我为图表添加了android清单活动。我知道如果删除该活动,那么应用程序将停止工作,我想这就是问题所在,同时也为类规则设置活动只有两个TextViews但是当按下按钮规则时它仍然存在应用程序崩溃。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="in.wptrafficanalyzer.achartenginepiechart">

    <uses-sdk
            android:minSdkVersion="4"
            android:targetSdkVersion="15" />

    <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
        <activity
                android:name="com.example.pie3.MyActivity"
                android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity android:name="org.achartengine.GraphicalActivity" />
        <activity android:name="Rules"/>
    </application>
</manifest>

所以,如果有人能帮助我,我将不胜感激。我读到了活动生命周期,但仍然无法得到答案。我是android编程的新手,很抱歉,如果它很简单..谢谢

logcat的:

12-19 17:20:14.441      921-921/in.wptrafficanalyzer.achartenginepiechart D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so
12-19 17:20:14.441      921-921/in.wptrafficanalyzer.achartenginepiechart D/﹕ HostConnection::get() New Host Connection established 0xb9582510, tid 921
12-19 17:20:14.457      921-921/in.wptrafficanalyzer.achartenginepiechart D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
12-19 17:20:14.457      921-921/in.wptrafficanalyzer.achartenginepiechart D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
12-19 17:20:14.509      921-921/in.wptrafficanalyzer.achartenginepiechart W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
12-19 17:20:14.509      921-921/in.wptrafficanalyzer.achartenginepiechart E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
12-19 17:20:14.509      921-921/in.wptrafficanalyzer.achartenginepiechart E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
12-19 17:20:14.517      921-921/in.wptrafficanalyzer.achartenginepiechart E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
12-19 17:20:14.517      921-921/in.wptrafficanalyzer.achartenginepiechart E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
12-19 17:20:14.517      921-921/in.wptrafficanalyzer.achartenginepiechart D/OpenGLRenderer﹕ Enabling debug mode 0
12-19 17:20:19.665      921-921/in.wptrafficanalyzer.achartenginepiechart D/AndroidRuntime﹕ Shutting down VM
12-19 17:20:19.665      921-921/in.wptrafficanalyzer.achartenginepiechart W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4cabb20)
12-19 17:20:19.669      921-921/in.wptrafficanalyzer.achartenginepiechart E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: in.wptrafficanalyzer.achartenginepiechart, PID: 921
    android.content.ActivityNotFoundException: Unable to find explicit activity class {in.wptrafficanalyzer.achartenginepiechart/com.example.pie3.Rules}; have you declared this activity in your AndroidManifest.xml?
            at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
            at android.app.Activity.startActivityForResult(Activity.java:3424)
            at android.app.Activity.startActivityForResult(Activity.java:3385)
            at android.app.Activity.startActivity(Activity.java:3627)
            at android.app.Activity.startActivity(Activity.java:3595)
            at com.example.pie3.MyActivity$1.onClick(MyActivity.java:30)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

我也尝试在android清单中命名活动:  logcat然后看起来像这样:

12-19 17:46:36.213    1942-1942/in.wptrafficanalyzer.achartenginepiechart D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so
12-19 17:46:36.217    1942-1942/in.wptrafficanalyzer.achartenginepiechart D/﹕ HostConnection::get() New Host Connection established 0xb957b1c0, tid 1942
12-19 17:46:36.229    1942-1942/in.wptrafficanalyzer.achartenginepiechart D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
12-19 17:46:36.233    1942-1942/in.wptrafficanalyzer.achartenginepiechart D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
12-19 17:46:36.293    1942-1942/in.wptrafficanalyzer.achartenginepiechart W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
12-19 17:46:36.293    1942-1942/in.wptrafficanalyzer.achartenginepiechart E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
12-19 17:46:36.293    1942-1942/in.wptrafficanalyzer.achartenginepiechart E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
12-19 17:46:36.305    1942-1942/in.wptrafficanalyzer.achartenginepiechart E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
12-19 17:46:36.305    1942-1942/in.wptrafficanalyzer.achartenginepiechart E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
12-19 17:46:36.305    1942-1942/in.wptrafficanalyzer.achartenginepiechart D/OpenGLRenderer﹕ Enabling debug mode 0
12-19 17:46:43.489    1942-1942/in.wptrafficanalyzer.achartenginepiechart D/dalvikvm﹕ GC_FOR_ALLOC freed 81K, 4% free 3299K/3436K, paused 1ms, total 2ms
12-19 17:46:43.489    1942-1942/in.wptrafficanalyzer.achartenginepiechart I/dalvikvm-heap﹕ Grow heap (frag case) to 4.345MB for 1127532-byte allocation
12-19 17:46:43.493    1942-1951/in.wptrafficanalyzer.achartenginepiechart D/dalvikvm﹕ GC_FOR_ALLOC freed 1K, 4% free 4398K/4540K, paused 4ms, total 4ms
12-19 17:46:43.505    1942-1942/in.wptrafficanalyzer.achartenginepiechart D/AndroidRuntime﹕ Shutting down VM
12-19 17:46:43.505    1942-1942/in.wptrafficanalyzer.achartenginepiechart W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4cabb20)
12-19 17:46:43.505    1942-1942/in.wptrafficanalyzer.achartenginepiechart E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: in.wptrafficanalyzer.achartenginepiechart, PID: 1942
    java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.achartenginepiechart/com.example.pie3.Rules}: java.lang.RuntimeException: native typeface cannot be made
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: native typeface cannot be made
            at android.graphics.Typeface.<init>(Typeface.java:175)
            at android.graphics.Typeface.createFromAsset(Typeface.java:149)
            at com.example.pie3.Rules.onCreate(Rules.java:17)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

您是否在AndroidManifest.xml中将此活动声明为com.example.pie3.Rules?

答案 1 :(得分:0)

就像我说的,我的问题是自定义字体:当我从我的班级和所有布局中删除它时。应用程序工作正常。因此,如果在LOGCAT中有任何人有问题:RuntimeException:无法生成本机字体!!只需删除自定义字体。希望能帮助其他有同样问题的人。干杯