有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android Studio“不幸的是,我的项目已经停止”由于致命的异常

当我用emulator运行我的项目时,它会弹出“不幸的是,项目已经停止”,并出现一个致命的异常。有人能看看我的项目并知道是什么错误吗?我发现以下logcat错误:

**03-07 18:42:53.990 2615-2615/com.example.admin.fragmentsmemecreator23 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.fragmentsmemecreator23, PID: 2615
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.fragmentsmemecreator23/com.example.admin.fragmentsmemecreator23.MainActivity}: 安卓.view.InflateException: Binary XML file line #24: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
    at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
    at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at 安卓.app.ActivityThread.-wrap11(ActivityThread.java)
    at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at 安卓.os.Handler.dispatchMessage(Handler.java:102)
    at 安卓.os.Looper.loop(Looper.java:148)
    at 安卓.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: 安卓.view.InflateException: Binary XML file line #24: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
    at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at 安卓.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
    at 安卓.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
    at com.example.admin.fragmentsmemecreator23.MainActivity.onCreate(MainActivity.java:19)
    at 安卓.app.Activity.performCreate(Activity.java:6237)
    at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
    at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at 安卓.app.ActivityThread.-wrap11(ActivityThread.java) 
    at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at 安卓.os.Handler.dispatchMessage(Handler.java:102) 
    at 安卓.os.Looper.loop(Looper.java:148) 
    at 安卓.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
 Caused by: 安卓.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
    at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at com.example.admin.fragmentsmemecreator23.BottomPictureFragment.onCreateView(BottomPictureFragment.java:19)
    at 安卓.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
    at 安卓.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
    at 安卓.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1226)
    at 安卓.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1328)
    at 安卓.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2284)
    at 安卓.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
    at 安卓.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314)
    at 安卓.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
    at 安卓.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
    at 安卓.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
    at 安卓.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at 安卓.view.LayoutInflater.rInflate(LayoutInflater.java:835)
    at 安卓.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at 安卓.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
    at 安卓.view.LayoutInflater.rInflate(LayoutInflater.java:831)
    at 安卓.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:423) 
    at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at 安卓.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
    at 安卓.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
    at com.example.admin.fragmentsmemecreator23.MainActivity.onCreate(MainActivity.java:19) 
    at 安卓.app.Activity.performCreate(Activity.java:6237) 
    at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
    at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
    at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at 安卓.app.ActivityThread.-wrap11(ActivityThread.java) 
    at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at 安卓.os.Handler.dispatchMessage(Handler.java:102) 
    at 安卓.os.Looper.loop(Looper.java:148) 
    at 安卓.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
 Caused by: 安卓.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
    at 安卓.view.LayoutInflater.createView(LayoutInflater.java:645)
    at com.安卓.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
    at 安卓.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
    at 安卓.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)

有三个java类和三个xml文件:1。主要活动。java 2。这是一块碎片。java 3。底部图像折射。java 4。顶部_部分_片段。xml 5。下面是图片碎片。xml 6。主要活动。xml

  1. 主要活动。爪哇

    public class MainActivity extends AppCompatActivity implements TopSectionFragment.TopSectionListener{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
        }
    
        //This gets called when the user clicks the button
        @Override
        public void createMeme(String top, String bottom) {
            BottomPictureFragment bottomFragment=(BottomPictureFragment)getSupportFragmentManager().findFragmentById(R.id.fragment2);
            bottomFragment.setMemeText(top,bottom);
    
        }
    
        @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) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    }
    
  2. 这是一块碎片。爪哇

    public class TopSectionFragment extends Fragment{
        private static EditText topTextInput;
        private static EditText bottomTextInput;
    
        TopSectionListener activityCommander;
    
        public interface TopSectionListener{
            public void createMeme(String top,String bottom);
        }
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            try {
                activityCommander = (TopSectionListener) activity;
            }catch (ClassCastException e){
                System.out.println(e.getStackTrace());
                throw new ClassCastException(activity.toString());
            }
        }
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.top_section_fragment, container,false);
            topTextInput=(EditText) view.findViewById(R.id.topTextInput);
            bottomTextInput=(EditText) view.findViewById(R.id.bottomTextInput);
            final Button button=(Button) view.findViewById(R.id.button);
    
            button.setOnClickListener(
                    new View.OnClickListener(){
                        public void onClick(View v){
                            buttonClicked(v);
                        }
                    }
            );
            return view;
        }
    
        public void buttonClicked(View view){
           activityCommander.createMeme(topTextInput.getText().toString(),bottomTextInput.getText().toString());
        }
    }
    
  3. 底部图像折射。爪哇

    public class BottomPictureFragment extends Fragment{
        private static TextView topMemeText;
        private static TextView bottomMemeText;
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.bottom_picture_fragment, container,false);
            topMemeText=(TextView)view.findViewById(R.id.topMemeText);
            bottomMemeText=(TextView) view.findViewById(R.id.bottomMemeText);
    
            return view;
        }
    
        public void setMemeText(String top,String bottom){
            topMemeText.setText(top);
            bottomMemeText.setText(bottom);
        }
    }
    
  4. 顶部_部分_片段。xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
        安卓:layout_width="match_parent" 安卓:layout_height="match_parent">
    
        <EditText
            安卓:id="@+id/topTextInput"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:layout_centerHorizontal="true"
            安卓:layout_marginTop="15dp"
            安卓:width="300dp"/>
    
        <EditText
            安卓:id="@+id/bottomTextInput"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:layout_below="@+id/topTextInput"
            安卓:layout_centerHorizontal="true"
            安卓:layout_marginTop="15dp"
            安卓:width="300dp"/>
    
        <Button
            安卓:id="@+id/button"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:layout_below="@+id/bottomTextInput"
            安卓:layout_centerHorizontal="true"
            安卓:layout_marginTop="15dp"
            安卓:text="@string/button_text"/>
    </RelativeLayout>
    
  5. 下面是图片碎片。xml

    <RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
        安卓:layout_width="match_parent" 安卓:layout_height="match_parent"
        安卓:background="@drawable/chirag">
    
        <TextView
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:textAppearance="?安卓:attr/textAppearanceLarge"
            安卓:text="@string/top_text"
            安卓:id="@+id/topMemeText"
            安卓:layout_alignParentTop="true"
            安卓:layout_centerHorizontal="true"
            安卓:textColor="#FFF"
            安卓:gravity="center_horizontal" />
    
        <TextView
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:textAppearance="?安卓:attr/textAppearanceLarge"
            安卓:text="@string/bottom_text"
            安卓:id="@+id/bottomMemeText"
            安卓:layout_alignParentBottom="true"
            安卓:layout_centerHorizontal="true"
            安卓:textColor="#FFF"
            安卓:gravity="center_horizontal" />
    </RelativeLayout>
    
  6. 主要活动。xml

    <?xml version="1.0" encoding="utf-8"?>
    <安卓.support.design.widget.CoordinatorLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
        xmlns:app="http://schemas.安卓.com/apk/res-auto"
        xmlns:tools="http://schemas.安卓.com/tools"
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:fitsSystemWindows="true"
        tools:context="com.example.admin.fragmentsmemecreator23.MainActivity">
    
        <安卓.support.design.widget.AppBarLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:theme="@style/AppTheme.AppBarOverlay">
    
            <安卓.support.v7.widget.Toolbar
                安卓:id="@+id/toolbar"
                安卓:layout_width="match_parent"
                安卓:layout_height="?attr/actionBarSize"
                安卓:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
    
        </安卓.support.design.widget.AppBarLayout>
    
        <include layout="@layout/content_main" />
    
    </安卓.support.design.widget.CoordinatorLayout>
    

共 (1) 个答案

  1. # 1 楼答案

    一种解决方案是,您应该首先用片段替换活动,然后将具有top和bottom值的参数传递给该片段

    大概是

    @Override
    public void createMeme(String top, String bottom) 
            Fragment fragment = new BottomPictureFragment();
            Bundle args = new Bundle();
            args.putString("Top", top);
            args.putString("Bottom",bottom);
            fragment .setArguments(args);
            FragmentManager frgManager = getSupportFragmentManager().beginTransaction().replace(R.id.activity_main, fragment).commit();
    

    完成此操作后,可以在onCreateView()内的BottomPictureFragment中获取参数,并从onCreateView()调用setMeme(top,bottom)函数