android.view.inflateexception片段

时间:2014-12-17 06:31:27

标签: android android-fragments android-viewpager

我对应用程序开发仍然很陌生,所以我试图围绕创建一个跨越5个页面的基本音板(或者我猜的片段)。我从默认的“Tabbed Activity'”中对我的java代码进行了建模。在Android Studio中创建项目时的选项。

我的问题是,当我的MainActivity.java代码引用多个片段时,应用程序会在模拟器上进行测试时崩溃。第一页加载并且所有声音都有效,但是当我尝试滑动/单击新标签时,应用程序就会放弃。我尝试了几种解决方法,并发现如果我让所有5个选项卡引用相同的片段,代码工作正常,但我的故障排除技能会在此之后停止。

我的代码如下:

MainActivity.java

import java.util.Locale;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {

SectionsPagerAdapter mSectionsPagerAdapter;

ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {

        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }

}

public void playSound (View v){
    String tag = (String) v.getTag();
    int resId = getResources().getIdentifier(tag, "raw", getPackageName());
    Intent playSound = new Intent(MainActivity.this, SoundPlayerService.class);
    playSound.putExtra("soundid", resId);
    startService(playSound);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new fragment_a();
            case 1:
                return new fragment_b();
            case 2:
                return new fragment_c();
            case 3:
                return new fragment_d();
            case 4:
                return new fragment_e();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 5;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
            case 3:
                return getString(R.string.title_section4).toUpperCase(l);
            case 4:
                return getString(R.string.title_section5).toUpperCase(l);
        }
        return null;
    }
}


public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";


    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_a, container, false);
        return rootView;
    }
}


}

fragment_a.java (注意:其他4个片段的类似代码)

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class fragment_a extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a, container, false);
    }

}

fragment_a.xml 的样本(基本上每个片段都有一个8x3表,包含24个单元格,每个单元格映射为播放115种声音中的一种)。

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

<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:stretchColumns="1"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_centerInParent="true"
    android:background = "@drawable/wallpaper">
    <TableRow
        android:id="@+id/TableRow01"
        android:layout_width="fill_parent"
        android:layout_height="0px"
        android:layout_weight="1" >
        <Button
            android:id="@+id/file001"
            android:tag="file001"
            android:layout_margin="1dp"
            android:layout_weight="1"
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:text="@string/file001"
            android:textColor="#FFFFF0"
            android:textStyle="normal"
            android:paddingLeft="3dp"
            android:paddingRight="3dp"
            android:paddingTop="3dp"
            android:paddingBottom="3dp"
            android:onClick="playSound"
            android:alpha="0.9"
            android:background="@drawable/button"/>
        <Button
            android:id="@+id/file002"
            android:tag="file002"
            android:layout_weight="1"
            android:layout_margin="1dp"
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:text="@string/file002"
            android:textColor="#FFFFF0"
            android:textStyle="normal"
            android:paddingLeft="3dp"
            android:paddingRight="3dp"
            android:paddingTop="3dp"
            android:paddingBottom="3dp"
            android:onClick="playSound"
            android:alpha="0.9"
            android:background="@drawable/button"/>
        <Button
            android:id="@+id/file003"
            android:tag="file003"
            android:layout_weight="1"
            android:layout_margin="1dp"
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:text="@string/file003"
            android:textColor="#FFFFF0"
            android:textStyle="normal"
            android:paddingLeft="3dp"
            android:paddingRight="3dp"
            android:paddingTop="3dp"
            android:paddingBottom="3dp"
            android:onClick="playSound"
            android:alpha="0.9"
            android:background="@drawable/button" />
    </TableRow>

错误日志如下:

android.view.InflateException: Binary XML file line #3: Error inflating class <unknown>
                at android.view.LayoutInflater.createView(LayoutInflater.java:620)
                at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
                at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                at quirkules.com.dukenukemsoundboard.fragment_c.onCreateView(fragment_c.java:16)
                at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
                at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
                at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
                at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
                at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
                at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
                at android.support.v4.view.ViewPager$3.run(ViewPager.java:249)
                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                at android.view.Choreographer.doFrame(Choreographer.java:543)
                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                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)
         Caused by: java.lang.reflect.InvocationTargetException
                at java.lang.reflect.Constructor.constructNative(Native Method)
                at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
                at android.view.LayoutInflater.createView(LayoutInflater.java:594)
                at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
                at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                at quirkules.com.dukenukemsoundboard.fragment_c.onCreateView(fragment_c.java:16)
                at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
                at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
                at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
                at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
                at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
                at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
                at android.support.v4.view.ViewPager$3.run(ViewPager.java:249)
                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                at android.view.Choreographer.doFrame(Choreographer.java:543)
                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                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)
         Caused by: java.lang.OutOfMemoryError
                at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:594)
                at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:429)
                at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
                at android.content.res.Resources.loadDrawable(Resources.java:2110)
                at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
                at android.view.View.<init>(View.java:3554)
                at android.view.ViewGroup.<init>(ViewGroup.java:470)
                at android.widget.LinearLayout.<init>(LinearLayout.java:176)
                at android.widget.LinearLayout.<init>(LinearLayout.java:172)
                at android.widget.TableLayout.<init>(TableLayout.java:105)
                at java.lang.reflect.Constructor.constructNative(Native Method)
                at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
                at android.view.LayoutInflater.createView(LayoutInflater.java:594)
                at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
                at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                at quirkules.com.dukenukemsoundboard.fragment_c.onCreateView(fragment_c.java:16)
                at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
                at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
                at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
                at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
                at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
                at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
                at android.support.v4.view.ViewPager$3.run(ViewPager.java:249)
                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                at android.view.Choreographer.doFrame(Choreographer.java:543)
                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                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)

我注意到当我修改SectionsPagerAdapter以返回5份&#34;新片段_a()&#34;它工作正常。错误日志似乎表明我的onCreateView存在问题,但我在其他地方找不到任何可以提供帮助的解决方案。非常感谢任何帮助!

编辑:根据要求, fragment_c.java 的代码:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class fragment_c extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_c, container, false);
        return rootView;
    }
}

1 个答案:

答案 0 :(得分:1)

当您的图像大于设备时会发生此错误..删除图像或仅在需要图像时缩小图像大小或使用9补丁图像格式。 在你的片段中尝试一次..

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