Android-Fragment java.lang.RuntimeException:无法启动活动

时间:2015-12-13 10:42:01

标签: java android eclipse android-fragments

我正在构建一个使用3个片段的应用程序,第一个片段显示主页,第二个用于详细信息,第三个用于播放视频。任何文件都没有错误,但是当我运行它时,我遇到运行时错误,我不知道如何修复它。任何人都可以帮助我吗?

MainActivity.java:

public class MainActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

final Context context = this;

private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

// nav drawer title
private CharSequence mDrawerTitle;

// used to store app title
private CharSequence mTitle;

// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;

private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;



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

    setContentView(R.layout.activity_main);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mTitle = mDrawerTitle = getTitle();

    // load slide menu items
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

    // nav drawer icons from resources
    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

    navDrawerItems = new ArrayList<NavDrawerItem>(); 

    // adding nav drawer items to arrayonCreateOptionsMenu
    // Home
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
    // Find People
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
    // Photos
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));




    // Recycle the typed array
    navMenuIcons.recycle();

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    // setting the nav drawer list adapter
    adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems);
    mDrawerList.setAdapter(adapter);

    // enabling action bar app icon and behaving it as toggle button
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
            R.drawable.ic_drawer, //nav menu toggle icon
            R.string.app_name
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            // calling onPrepareOptionsMenu() to hide action bar icons
            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        // on first time display view for first nav item
        displayView(0);
    }
}

/**
 * Slide menu item click listener
 * */
private class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // display view for selected nav drawer item
        displayView(position);
    }
}

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


    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle action bar actions click
    switch (item.getItemId()) {
    case R.id.action_settings:
        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.custom); 
        dialog.setTitle("Event Story");


        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Develop and maintained by Raffcomm Technologies Sdn. Bhd");


        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

/* *
 * Called when invalidateOptionsMenu() is triggered
 */

@Override
public boolean onPrepareOptionsMenu(Menu menu) {


    // if nav drawer is opened, hide the action items
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);


    return super.onPrepareOptionsMenu(menu);
}

/**
 * Diplaying fragment view for selected nav drawer list item
 * */

public void onNavigationDrawerItemSelected(int position) {
    // TODO Auto-generated method stub

}
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        fragment = new VideoFragment();
        break;
    case 2:
        fragment = new ThirdFragment();
        break;
    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}
public void openBrowser(View view){

    //Get url from tag
    String url = (String)view.getTag();

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.addCategory(Intent.CATEGORY_BROWSABLE);

    //pass the url to intent data
    intent.setData(Uri.parse(url));

    startActivity(intent);
}

}

HomeFragment:

public class HomeFragment extends Fragment{


public HomeFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    return rootView;
}

}

fragment_main.xml:

<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: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.eventstory.MainActivity$PlaceholderFragment" >

 <ImageView
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:src="@drawable/homepage"
    android:scaleType = "fitXY"
    />

</RelativeLayout>

logcat的:

12-13 19:02:16.376: W/ResourceType(9909): Found multiple library tables, ignoring...
12-13 19:02:17.658: I/art(9909): Alloc sticky concurrent mark sweep GC freed 2108(151KB) AllocSpace objects, 0(0B) LOS objects, 36% free, 11MB/17MB, paused 594us total 6.623ms
12-13 19:02:17.664: I/art(9909): Alloc partial concurrent mark sweep GC freed 273(25KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 11MB/18MB, paused 794us total 6.052ms
12-13 19:02:17.676: I/art(9909): Alloc concurrent mark sweep GC freed 26(12KB) AllocSpace objects, 1(81KB) LOS objects, 40% free, 11MB/18MB, paused 903us total 10.972ms
12-13 19:02:17.676: I/art(9909): Forcing collection of SoftReferences for 549MB allocation
12-13 19:02:17.685: I/art(9909): Alloc concurrent mark sweep GC freed 11(344B) AllocSpace objects, 0(0B) LOS objects, 40% free, 11MB/18MB, paused 887us total 9.436ms
12-13 19:02:17.685: E/art(9909): Throwing OutOfMemoryError "Failed to allocate a 576000012 byte allocation with 7766112 free bytes and 180MB until OOM"
12-13 19:02:17.704: I/art(9909): Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 40% free, 11MB/18MB, paused 925us total 9.536ms
12-13 19:02:17.704: I/art(9909): Forcing collection of SoftReferences for 549MB allocation
12-13 19:02:17.715: I/art(9909): Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 40% free, 11MB/18MB, paused 1.967ms total 10.570ms
12-13 19:02:17.715: E/art(9909): Throwing OutOfMemoryError "Failed to allocate a 576000012 byte allocation with 7766112 free bytes and 180MB until OOM"
12-13 19:02:17.715: D/skia(9909): --- allocation failed for scaled bitmap
12-13 19:02:17.766: D/AndroidRuntime(9909): Shutting down VM
12-13 19:02:17.768: E/AndroidRuntime(9909): FATAL EXCEPTION: main
12-13 19:02:17.768: E/AndroidRuntime(9909): Process: com.example.eventstory, PID: 9909
12-13 19:02:17.768: E/AndroidRuntime(9909): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eventstory/com.example.eventstory.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.app.ActivityThread.access$800(ActivityThread.java:156)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.os.Looper.loop(Looper.java:211)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.app.ActivityThread.main(ActivityThread.java:5373)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at java.lang.reflect.Method.invoke(Native Method)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at java.lang.reflect.Method.invoke(Method.java:372)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
12-13 19:02:17.768: E/AndroidRuntime(9909): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.view.LayoutInflater.createView(LayoutInflater.java:633)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at com.example.eventstory.HomeFragment.onCreateView(HomeFragment.java:19)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.app.Fragment.performCreateView(Fragment.java:2053)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.app.BackStackRecord.run(BackStackRecord.java:834)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.app.Activity.performStart(Activity.java:6005)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342)
12-13 19:02:17.768: E/AndroidRuntime(9909):     ... 10 more
12-13 19:02:17.768: E/AndroidRuntime(9909): Caused by: java.lang.reflect.InvocationTargetException
12-13 19:02:17.768: E/AndroidRuntime(9909):     at java.lang.reflect.Constructor.newInstance(Native Method)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.view.LayoutInflater.createView(LayoutInflater.java:607)
12-13 19:02:17.768: E/AndroidRuntime(9909):     ... 24 more
12-13 19:02:17.768: E/AndroidRuntime(9909): Caused by: java.lang.OutOfMemoryError: Failed to allocate a 576000012 byte allocation with 7766112 free bytes and 180MB until OOM
12-13 19:02:17.768: E/AndroidRuntime(9909):     at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1004)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.content.res.Resources.loadDrawableForCookie(Resources.java:2694)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.content.res.Resources.loadDrawable(Resources.java:2576)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.widget.ImageView.<init>(ImageView.java:146)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.widget.ImageView.<init>(ImageView.java:135)
12-13 19:02:17.768: E/AndroidRuntime(9909):     at android.widget.ImageView.<init>(ImageView.java:131)
12-13 19:02:17.768: E/AndroidRuntime(9909):     ... 27 more
12-13 19:02:20.327: I/Process(9909): Sending signal. PID: 9909 SIG: 9

2 个答案:

答案 0 :(得分:0)

您的活动应该实现NavigationDrawerCallbacks界面。

public class MainActivity extends Activity implements NavigationDrawerCallbacks

答案 1 :(得分:0)

在MainActivity中实现ListFragment.OnFragmentInteractionListener ..