每当我在模拟器上运行应用程序时,它说不幸的是应用程序已停止工作,日志会读取以下行

时间:2016-05-02 10:28:04

标签: android

MainActivity文件

package com.example.intel.dualboot;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements StatusAsyncTask.StatusAsyncTaskListener, SwipeRefreshLayout.OnRefreshListener, MainActivityListener {
    private static final String TAG = "db::MainActivity";

    /* public static final int ACT_INSTALL_ROM        = 1;
    public static final int ACT_CHANGE_PAGE        = 2;
    public static final int ACT_SELECT_ICON        = 3;
    public static final int ACT_UNINSTALL_ROM      = 4;

    public static final String INTENT_EXTRA_SHOW_ROM_LIST = "show_rom_list";*/

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(Build.VERSION.SDK_INT == 20) {
            showDeprecatedLAlert();
            return;
        }

        setContentView(R.layout.activity_main);

        // This activity is using different background color, which would cause overdraw
        // of the whole area, so disable the default background
        getWindow().setBackgroundDrawable(null);

        Utils.installHttpCache(this);
        PreferenceManager.setDefaultValues(this, R.xml.settings, false);

        m_srLayout = (InSwipeRefreshLayout)findViewById(R.id.refresh_layout);
        m_srLayout.setOnRefreshListener(this);

        m_curFragment = -1;

        m_fragmentTitles = getResources().getStringArray(R.array.main_fragment_titles);
        m_drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        m_drawerList = (ListView) findViewById(R.id.left_drawer);

        String[] fragmentClsNames = new String[MainFragment.MAIN_FRAG_CNT];
        for(int i = 0; i < fragmentClsNames.length; ++i)
            fragmentClsNames[i] = MainFragment.getFragmentClass(i).getName();

        m_fragments = new MainFragment[MainFragment.MAIN_FRAG_CNT];
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction t = fragmentManager.beginTransaction();
        for(int i = 0; i < m_fragments.length; ++i) {
            m_fragments[i] = (MainFragment)fragmentManager.findFragmentByTag(fragmentClsNames[i]);
            if(m_fragments[i] == null) {
                m_fragments[i] = MainFragment.newFragment(i);
                t.add(R.id.content_frame, m_fragments[i], fragmentClsNames[i]);
            }
            t.hide(m_fragments[i]);
        }
        t.commit();

        // Set the adapter for the list view
        m_drawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list, m_fragmentTitles));
        // Set the list's click listener
        m_drawerList.setOnItemClickListener(new DrawerItemClickListener());

        m_drawerTitle = getText(R.string.app_name);
        m_drawerToggle = new ActionBarDrawerToggle(
                this, m_drawerLayout, R.string.drawer_open, R.string.drawer_close) {
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(m_title);
            }

            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(m_drawerTitle);
            }
        };
        m_drawerLayout.setDrawerListener(m_drawerToggle);

        final ActionBar bar = getSupportActionBar();
        if(bar != null) {
            bar.setDisplayHomeAsUpEnabled(true);
            bar.setHomeButtonEnabled(true);
        }

        /*  if (getIntent().hasExtra(INTENT_EXTRA_SHOW_ROM_LIST) &&
                getIntent().getBooleanExtra(INTENT_EXTRA_SHOW_ROM_LIST, false)) {
            getIntent().removeExtra(INTENT_EXTRA_SHOW_ROM_LIST);
            selectItem(1);
        } else if(savedInstanceState != null) {
            selectItem(savedInstanceState.getInt("curFragment", 0));
        } else {
            selectItem(0);
        }*/
    }

    /*@Override
    protected void onNewIntent(Intent i) {
        super.onNewIntent(i);
        if (i.hasExtra(INTENT_EXTRA_SHOW_ROM_LIST) &&
                i.getBooleanExtra(INTENT_EXTRA_SHOW_ROM_LIST, false)) {
            selectItem(1);
        }
    }*/

    @Override
    protected void onStop() {
        super.onStop();
        Utils.flushHttpCache();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curFragment", m_curFragment);
    }

    @Override
    public boolean onCreateOptionsMenu (Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);

        m_refreshItem = menu.findItem(R.id.action_refresh);
        if(!StatusAsyncTask.instance().isComplete())
            m_refreshItem.setEnabled(false);
        return true;
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            selectItem(position);
        }
    }

    /** Swaps fragments in the main content view */
    private void selectItem(int position) {
        if(position < 0 || position >= m_fragments.length) {
            Log.e(TAG, "Invalid fragment index " + position);
            return;
        }

        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction t = fragmentManager.beginTransaction();

        if(m_curFragment != -1)
            t.hide(m_fragments[m_curFragment]);
        t.show(m_fragments[position]);
        t.commit();

        m_curFragment = position;

        // Highlight the selected item, update the title, and close the drawer
        m_drawerList.setItemChecked(position, true);
        setTitle(m_fragmentTitles[position]);
        m_drawerLayout.closeDrawer(m_drawerList);
    }

    @Override
    public void setTitle(CharSequence title) {
        m_title = title;
        getSupportActionBar().setTitle(m_title);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        if(m_drawerToggle != null)
            m_drawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(m_drawerToggle != null)
            m_drawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem it) {
        if (m_drawerToggle.onOptionsItemSelected(it))
            return true;

        switch(it.getItemId()) {
            case R.id.action_refresh:
                refresh(false);
                return true;

            case R.id.action_reboot:
            {
                AlertDialog.Builder b = new AlertDialog.Builder(this);
                b.setTitle("Reboot")
                        .setCancelable(true)
                        .setNegativeButton("Cancel", null)
                        .setItems(R.array.reboot_options, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                switch (i) {
                                    case 0: Utils.reboot(""); break;
                                    case 1: Utils.reboot("recovery"); break;
                                    case 2: Utils.reboot("bootloader"); break;
                                }
                            }
                        })
                        .create().show();
                return true;
            }

            default:
                return false;
        }
    }

    public void startRefresh(boolean notifyRefreshLayout) {
        if(notifyRefreshLayout)
            m_srLayout.setRefreshing(true);

        if(m_refreshItem != null)
            m_refreshItem.setEnabled(false);

        for(int i = 0; i < m_fragments.length; ++i)
            m_fragments[i].startRefresh();

        StatusAsyncTask.instance().setListener(this);
        StatusAsyncTask.instance().execute();
    }


    @Override
    public void refresh(boolean b) {
        refresh(true);
    }

    @Override
    public void setRefreshComplete() {
        m_srLayout.setRefreshing(false);

        if(m_refreshItem != null)
            m_refreshItem.setEnabled(true);

        for(int i = 0; i < m_fragments.length; ++i)
            m_fragments[i].setRefreshComplete();
    }

    @Override
    public void onFragmentViewCreated() {
        if(++m_fragmentViewsCreated == m_fragments.length) {
            // postDelayed because SwipeRefresher view ignores
            // setRefreshing call otherwise
            m_srLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent i = getIntent();
                    if(i == null || !i.getBooleanExtra("force_refresh", false)) {
                        startRefresh(true);
                    } else {
                        i.removeExtra("force_refresh");
                        refresh(false);
                    }
                }
            }, 1);
        }
    }

    @Override
    public void onFragmentViewDestroyed() {
        --m_fragmentViewsCreated;
    }

    @Override
    public void addScrollUpListener(InSwipeRefreshLayout.ScrollUpListener l) {
        m_srLayout.addScrollUpListener(l);
    }

    @Override
    public void onStatusTaskFinished(StatusAsyncTask.Result res) {
        for(int i = 0; i < m_fragments.length; ++i)
            m_fragments[i].onStatusTaskFinished(res);
    }

    @Override
    public void onRefresh() {
        refresh(false);
    }

    @TargetApi(20)
    private void showDeprecatedLAlert() {
        SpannableString msg = new SpannableString("Android Developer preview has bugs");
        Linkify.addLinks(msg, Linkify.ALL);

        AlertDialog.Builder b = new AlertDialog.Builder(this);
        b.setTitle("Unsupported Android version")
                .setCancelable(false)
                .setMessage(msg)
                .setNegativeButton("Exit Application", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
                    }
                });

        AlertDialog d = b.create();
        d.show();

        TextView msgView = (TextView)d.findViewById(android.R.id.message);
        msgView.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private DrawerLayout m_drawerLayout;
    private ListView m_drawerList;
    private String[] m_fragmentTitles;
    private MainFragment[] m_fragments;
    private int m_curFragment;
    private CharSequence m_title;
    private ActionBarDrawerToggle m_drawerToggle;
    private CharSequence m_drawerTitle;
    private MenuItem m_refreshItem;
    private int m_fragmentViewsCreated;
    private InSwipeRefreshLayout m_srLayout;
}

我收到此错误

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.intel.dualboot/com.example.intel.dualboot.MainActivity}: java.lang.ClassCastException: minor_activities.Status cannot be cast to com.example.intel.dualboot.MainFragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
    at android.app.ActivityThread.access$600(ActivityThread.java:156)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:153)
    at android.app.ActivityThread.main(ActivityThread.java:5297)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: minor_activities.Status cannot be cast to com.example.intel.dualboot.MainFragment
    at com.example.intel.dualboot.MainFragment.newFragment(MainFragment.java:28)
    at com.example.intel.dualboot.MainActivity.onCreate(MainActivity.java:77)
    at android.app.Activity.performCreate(Activity.java:5122)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358) 
    at android.app.ActivityThread.access$600(ActivityThread.java:156) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:153) 
    at android.app.ActivityThread.main(ActivityThread.java:5297) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 

activity_main.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="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout">

    <com.example.intel.dualboot.InSwipeRefreshLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/refresh_layout"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true">
        <FrameLayout android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </com.example.intel.dualboot.InSwipeRefreshLayout>
    <ListView android:id="@+id/left_drawer"
        android:layout_width="@dimen/lviewdimen"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111" />
</android.support.v4.widget.DrawerLayout>

2 个答案:

答案 0 :(得分:0)

这是你的错误......

android.support.v4.widget.SwipeRefreshLayout无法强制转换为com.example.intel.dualboot.InSwipeRefreshLayout

答案 1 :(得分:0)

您已将XML中的SwipeToRefresh初始化为&#34; android.support.v4.widget.SwipeRefreshLayout&#34;但是您正尝试将该视图的对象创建为&#34; com.example.intel.dualboot.InSwipeRefreshLayout&#34;。所以你因错误的类转换而出错了。改变&#34; android.support.v4.widget.SwipeRefreshLayout&#34; to&#34; com.example.intel.dualboot.InSwipeRefreshLayout&#34;在xml中解决您的问题。