简体   繁体   English

由于致命异常,Android Studio“很遗憾,我的项目已停止”

[英]Android Studio “Unfortunately my project has stopped” due to FATAL EXCEPTION

When I run my project with the emulator, it popped up "Unfortunately project has stopped" with a fatal exception, can anyone look at my project and know what error is? 当我使用模拟器运行项目时,它弹出一个“不幸的是项目已停止”的致命错误,任何人都可以查看我的项目并知道错误是什么吗? I got following logcat error: 我收到以下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}: android.view.InflateException: Binary XML file line #24: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: android.view.InflateException: Binary XML file line #24: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
    at com.example.admin.fragmentsmemecreator23.MainActivity.onCreate(MainActivity.java:19)
    at android.app.Activity.performCreate(Activity.java:6237)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
 Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at com.example.admin.fragmentsmemecreator23.BottomPictureFragment.onCreateView(BottomPictureFragment.java:19)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1226)
    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1328)
    at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2284)
    at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
    at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314)
    at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
    at com.example.admin.fragmentsmemecreator23.MainActivity.onCreate(MainActivity.java:19) 
    at android.app.Activity.performCreate(Activity.java:6237) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
 Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
    at android.view.LayoutInflater.createView(LayoutInflater.java:645)
    at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)

There are three java classes and three xml files: 1. MainActivity.java 2. TopSectionFragment.java 3. BottomPictureFragment.java 4. top_section_fragment.xml 5. bottom_picture_fragment.xml 6.activity_main.xml 一共有三个Java类和三个xml文件:1. MainActivity.java 2. TopSectionFragment.java 3. BottomPictureFragment.java 4. top_section_fragment.xml 5. bottom_picture_fragment.xml 6.activity_main.xml

  1. MainActivity.java MainActivity.java

     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. TopSectionFragment.java TopSectionFragment.java

     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. BottomPictureFragment.java BottomPictureFragment.java

     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. top_section_fragment.xml top_section_fragment.xml

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

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

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

One solution would be , You should replace the activity with the fragment first and then pass arguments of top and bottom value to that fragment. 一种解决方案是,您应该先用片段替换活动,然后将top和bottom值的参数传递给该片段。

It would be something like 就像

@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();

After doing this you can get the arguments in the BottomPictureFragment inside onCreateView() and call the setMeme(top,bottom) function from the onCreateView(). 完成此操作后,您可以在onCreateView()中的BottomPictureFragment中获取参数,并从onCreateView()中调用setMeme(top,bottom)函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM