java.lang.RuntimeException:无法启动活动ComponentInfo错误膨胀类碎片

时间:2015-06-30 00:22:23

标签: android class android-fragments fragment

您好我在Android Studio中收到此错误: 任何人都可以帮助解决此错误.. 我已从代码中删除了几行,但所有必需的东西都在那里我相信... 提前谢谢..

  

错误

Process: stockquote.anirudh.com.stockquote, PID: 6263
    java.lang.RuntimeException: Unable to start activity ComponentInfo{stockquote.anirudh.com.stockquote/stockquote.anirudh.com.stockquote.MainActivity}: android.view.InflateException: Binary XML file line #127: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: android.view.InflateException: Binary XML file line #127: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at stockquote.anirudh.com.stockquote.MainActivity.onCreate(MainActivity.java:46)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.ClassCastException: stockquote.anirudh.com.stockquote.MainActivity@19a16f79 must implement OnFragmentInteractionListener
            at stockquote.anirudh.com.stockquote.NavigationDrawerFragment.onAttach(NavigationDrawerFragment.java:103)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
            at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1206)
            at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2159)
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
            at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:547)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at stockquote.anirudh.com.stockquote.MainActivity.onCreate(MainActivity.java:46)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  

MAINACTIVITY

public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
public final static String STOCK_SYMBOL = "com.anirudh.stockquote.STOCK";
private SharedPreferences stockSymbolsEntered;
private TableLayout stockTableScrollView;
private EditText stockSymbolEditText;
Button enterStockSymbolButton;
Button deleteStocksButton;
Button updateStocksButton;

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

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
          getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);


    drawerFragment.setup((DrawerLayout)findViewById(R.id.drawer_layout),toolbar);

    stockSymbolsEntered = getSharedPreferences("stockList", MODE_PRIVATE);
    stockTableScrollView = (TableLayout) findViewById(R.id.stockTableScrollView);
    stockSymbolEditText = (EditText) findViewById(R.id.stockSymbolEditText);
    enterStockSymbolButton = (Button) findViewById(R.id.enterStockSymbolButton);
    }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {

        Toast.makeText(this, "Application created by Anirudh", Toast.LENGTH_LONG).show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}
  

navigationDrawerFragment.JAVA

package stockquote.anirudh.com.stockquote;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link NavigationDrawerFragment.OnFragmentInteractionListener} interface
  * to handle interaction events.
 * Use the {@link NavigationDrawerFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NavigationDrawerFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

public static final String PREF_FILE_NAME = "testpref";
public static final String KEY_USER_LEARNED_DRAWER = "user_learned_drawer";
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;

private boolean mUserLearnedDrawer;
private boolean mFromSavedInstanceState;


// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;


// TODO: Rename and change types and number of parameters
public static NavigationDrawerFragment newInstance(String param1, String param2)            
{
    NavigationDrawerFragment fragment = new NavigationDrawerFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}


public NavigationDrawerFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     mUserLearnedDrawer=Boolean.valueOf(readFromPreference(getActivity(),KEY_USER_LEARNED_DRAWER , "false"));

    if (savedInstanceState!=null)
    {
        mFromSavedInstanceState = true;

    }


    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_navigation_drawer, container,     false);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public void setup( DrawerLayout drawerLayout, final Toolbar toolbar) {

    mDrawerLayout = drawerLayout;
    mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar
            , R.string.drawer_open, R.string.drawer_close) {
        @Override

        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            Log.d("VIVZ", "onDrawerOpened");
        }

        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }


    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    public void onFragmentInteraction(Uri uri);
}


public static void saveToPreference ( Context context, String preferenceName , String preferenceValue){

    SharedPreferences sharedPreference = context.getSharedPreferences( "PREF_FILE_NAME" , Context.MODE_PRIVATE);
    SharedPreferences.Editor editor= sharedPreference.edit();

    editor.putString(preferenceName , preferenceValue);
    editor.apply();

}

public static String readFromPreference ( Context context, String preferenceName , String defaultValue){

    SharedPreferences sharedPreference = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
   return  sharedPreference.getString(preferenceName,defaultValue);

}

}

  

fragment_navigation_drawer.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"

tools:context="stockquote.anirudh.com.stockquote.NavigationDrawerFragment">

<!-- TODO: Update blank fragment layout -->
<TextView

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

  

activity_main.xml中

    <android.support.v4.widget.DrawerLayout     xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start">


<TableLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/TableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

       <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar" />

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">





        </TableRow>

    </TableLayout>

    <fragment
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:id="@+id/fragment_navigation_drawer"
    android:layout_gravity="start"

    app:layout="@layout/fragment_navigation_drawer"
    android:name="stockquote.anirudh.com.stockquote.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

     </android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:1)

如果仔细查看日志,就会说

Caused by: java.lang.ClassCastException: stockquote.anirudh.com.stockquote.MainActivity@19a16f79 must implement OnFragmentInteractionListener
            at stockquote.anirudh.com.stockquote.NavigationDrawerFragment.onAttach(NavigationDrawerFragment.java:103)

声明活动时,需要实现OnFragmentInteractionListener:

public class MainActivity extends ActionBarActivity implements OnFragmentInteractionListener {

并确保实现属于OnFragmentInteractionListener接口

的任何方法