我在Android Studio中尝试Fragments时遇到以下错误。启动应用时出现以下异常:
04 - 24 21: 02: 16.830 21108 - 21108 / com.example.user.samplefragments E / AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo {
com.example.user.samplefragments / com.example.user.samplefragments.MainActivity
}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line # 7: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java: 710)
at android.view.LayoutInflater.rInflate(LayoutInflater.java: 752)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 495)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 397)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java: 364)
at android.app.Activity.setContentView(Activity.java: 1930)
at com.example.user.samplefragments.MainActivity.onCreate(MainActivity.java: 19)
at android.app.Activity.performCreate(Activity.java: 5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2299)
at android.app.ActivityThread.access$700(ActivityThread.java: 150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1280)
at android.os.Handler.dispatchMessage(Handler.java: 99)
at android.os.Looper.loop(Looper.java: 137)
at android.app.ActivityThread.main(ActivityThread.java: 5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java: 511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.user.samplefragments.MenuFragment.onCreateView(MenuFragment.java: 23)
at android.app.Fragment.performCreateView(Fragment.java: 1699)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java: 879)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java: 1053)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java: 1155)
at android.app.Activity.onCreateView(Activity.java: 4872)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java: 686)
at android.view.LayoutInflater.rInflate(LayoutInflater.java: 752)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 495)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 397)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java: 364)
at android.app.Activity.setContentView(Activity.java: 1930)
at com.example.user.samplefragments.MainActivity.onCreate(MainActivity.java: 19)
at android.app.Activity.performCreate(Activity.java: 5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2299)
at android.app.ActivityThread.access$700(ActivityThread.java: 150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1280)
at android.os.Handler.dispatchMessage(Handler.java: 99)
at android.os.Looper.loop(Looper.java: 137)
at android.app.ActivityThread.main(ActivityThread.java: 5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java: 511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 869)
at dalvik.system.NativeStart.main(Native Method)
字符串
这是我的MainActvity.java:
package com.example.user.samplefragments;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity implements MenuFragment.OnItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onItemSelected(String link) {
DetailFragment fragment = (DetailFragment)getFragmentManager().findFragmentById(R.id.detailfragment);
fragment.setText(link);
}
}
This is my Fragment 1 :
package com.example.user.samplefragments;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by USER on 24-04-2016.
*/
public class MenuFragment extends Fragment{
private OnItemSelectedListener listener;
public MenuFragment(){}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu,container,false);
Button btn = (Button)getView().findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateDetail("fake");
}
});
return view;
}
public void updateDetail(String uri){
String newTime = String.valueOf(System.currentTimeMillis());
listener.onItemSelected(newTime);
}
public interface OnItemSelectedListener {
public void onItemSelected(String link);
}
}
This is my Fragment 2 :
package com.example.user.samplefragments;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by USER on 24-04-2016.
*/
public class DetailFragment extends Fragment{
public DetailFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail,container,false);
return view;
}
public void setText(String url){
TextView tv = (TextView)getView().findViewById(R.id.textView);
tv.setText(url);
}
}
This is my activity_main.xml :
Just using two fragments inside the main layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="@+id/menufragment"
class="com.example.user.samplefragments.MenuFragment" /**Declaring the class of the fragment*/
android:layout_height="match_parent"
android:layout_width="5dp">
</fragment>
<fragment
android:id="@+id/detailfragment"
class="com.example.user.samplefragments.DetailFragment"
android:layout_height="match_parent"
android:layout_width="5dp">
</fragment>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2" />
</LinearLayout>
型
正如你所看到的,我得到的错误:
java.lang.RuntimeException:无法启动Activity DataInfo {com.example.user.samplefragments/com.example.user.samplefragments.MainActivity}:android.view.InflateException:二进制XML文件行#7:膨胀类片段时出错。
我知道当软件包名称错误时会出现此问题,但我提供了正确的软件包名称。如何解决此问题?
3条答案
按热度按时间s6fujrry1#
解决方案:
您可以使用以下命令修改
MenuFragment
:字符串
为什么:
每次调用
onCreateView
时,片段中的视图即将创建。那时片段中没有视图。所以getView()
总是返回在onCreateView
中调用的null。这就是为什么在xml中膨胀片段时会得到NullPointerException
。要避免NullPointer,请尝试以下操作:
型
bihw5rsg2#
我觉得这条线会让应用崩溃,
字符串
将此行替换为
型
如果您的按钮包含在片段xml(R.layout.menu)中,它将工作。
5ktev3wc3#
请分享您的活动xml布局代码。在膨胀片段时存在一些问题。
请不要忘记将
android:name
属性设置为片段路径