Android Fragment InflateException

时间:2017-03-21 13:11:56

标签: java android android-fragments exception fragment

我正在尝试与我的片段进行通信,但代码不同意我并给我错误

 E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.aadi.fragmentsdemo, PID: 3435
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aadi.fragmentsdemo/com.example.aadi.fragmentsdemo.MainActivity}: android.view.InflateException: Binary XML file line #28: 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 #28: 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 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
                  at android.app.Activity.setContentView(Activity.java:2145)
                  at com.example.aadi.fragmentsdemo.MainActivity.onCreate(MainActivity.java:12)
                  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.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                  at com.example.aadi.fragmentsdemo.Frag.onCreateView(Frag.java:48)
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1255)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1472)
                  at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1691)
                  at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3440)
                  at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                  at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:378)
                  at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
                  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
                  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 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378) 
                  at android.app.Activity.setContentView(Activity.java:2145) 
                  at com.example.aadi.fragmentsdemo.MainActivity.onCreate(MainActivity.java:12) 
                  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) 
Application terminated.

我已经尝试过StackOverflow线程的所有解决方案但是我的代码似乎有问题我也不是专家而不是初学者所以请大家帮帮我,下面是代码和错误

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.aadi.fragmentsdemo.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id="@+id/textView2" />

<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="com.example.aadi.fragmentsdemo.Frage"
    android:id="@+id/fragment2"
    tools:layout="@layout/frag1"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true" />

<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="com.example.aadi.fragmentsdemo.Frag"
    android:id="@+id/fragment"
    tools:layout="@layout/frag"
    android:layout_below="@+id/fragment2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="92dp" />
    </RelativeLayout>

第二个片段XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="@string/name"
    android:ems="10"
    android:id="@+id/et" />

<Button
    android:text="@string/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btn" />
</LinearLayout>

第一个片段java类

package com.adi.fragmentcomm;

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;
import android.widget.TextView;

/**
* Created by AADI on 3/21/2017.
 */

public class Frag extends Fragment {
TextView tv;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.frag, container, false);

    tv = (TextView) getActivity().findViewById(R.id.textView);
    return v;
}

public void set(String a){
    tv.setText(a);
}
}

第二个片段java类

package com.adi.fragmentcomm;


import android.content.Context;
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;
import android.widget.Button;
import android.widget.EditText;

/**
* Created by AADI on 3/21/2017.
 */

public class Frag2 extends Fragment {
Button b;
EditText e;
comm ob;

public interface comm{
    void mem(String text);
}


@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
        ob = (comm) context;
    }catch (ClassCastException e){
        throw new ClassCastException(context.toString());
    }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.frag2, container, false);
    b = (Button) getActivity().findViewById(R.id.btn);
    e = (EditText) getActivity().findViewById(R.id.et);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ob.mem(e.getText().toString());            }
    });
    return v;
}


}

MainActivity java class

package com.adi.fragmentcomm;

import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends FragmentActivity implements Frag2.comm {

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

    @Override
    public void mem(String text) {
        Frag f = (Frag) getSupportFragmentManager().findFragmentById(R.id.fragment);
        f.set(text);

    }
}

2 个答案:

答案 0 :(得分:0)

如果findFragmentById

,请使用class而不是name属性
parser = argparse.ArgumentParser(
                 description='This program allows for you to create a User in AD and add that User to a specific AD Group',
                 epilog='Note: In order to run this script you must have login credentials of an AD admin. \
                 You must also know the username, if you intend to search for users, or the group name if intending to search for group. \
                 The logging level is set to info by default.')

parser.add_argument('--listuser', nargs=?, default='None', type=str,help='Lists attributes for a given user. Example: "--listuser --username <AD username(s)> "')
    parser.add_argument('--listusergroups', nargs=?, default='None', type=str, help='Lists the groups a user is associated with attributes for a given user. Example: "--listusergroups < AD username(s)>"')
    parser.add_argument('--listgroupmembers', nargs=?, default='None', type=str,help='Lists the members of a given group. Example: "--listgroupmembers --groups <group(s)>"')
    parser.add_argument('--addgroupmembers', nargs=?, default='None', type=str, help='Adds members to a specific AD group. Example: "./AD_Utility --addgroupmembers <group(s)> --username <username(s)>"')
    parser.add_argument('--renamegroup', nargs=?, default='None', type=str, help='Renames the AD group"./AD_Utility --rename <group(s)> <new_group_name>"')
    parser.add_argument('--disableuser', nargs=?, default='None', type=str, help='Disables user from AD. Example: "./AD_Utility --disableusers --username <username(s)>"')
    parser.add_argument('--removeuserfromgroup', nargs=?, default='None', type=str, help='Disables user from AD. Example: "./AD_Utility --removeuserfromgroup <username> --group <groupname>"')
    parser.add_argument('--creategroups', nargs=?, default='None', type=str, help='Enter the name of the group you want to search for. Example: "aws')
    parser.add_argument('--username', nargs=?, default='None', type=str, help='Username of the account you intend to gain information on. Example: "amazing')
    parser.add_argument('--scope', nargs=?, default='None', type=str, help='Scope of the search for which you are executing. Example: "aws')
args = parser.parse_args()

function_mapping = {
 "listuser": attributes_of_a_user, # or optionally: {"function": attributes_of_a_user, "nargs": "?", "default":..}
 "listusergroups": find_users_groups,
 "listgroupmembers": get_users_associated_with_groups,
 "addgroupmembers": add_users_to_group,
# "renamegroup": 
 "disableuser": disable_user,
 "creategroups": create_group,
 "createuser": create_user_in_ad,
 # "username"
 # "scope"
}

for function_name, function in function_mapping.iteritems():
      if args[function_name]:
        function(*args[function_name])

service = args.serviceaccount
group = args.groups
listuser = args.listuser
listusergroups = args.listusergroups
addgroupmembers = args.addgroupmembers
disableuser = args.disableuser
group_name, new_group_name = args.renamegroup
scope = args.scope
username = args.username
new_users = get_users()

答案 1 :(得分:0)

引用视图时使用的上下文/视图错误 更改Frag2 onCreateView()

中的代码
View v = inflater.inflate(R.layout.frag2, container, false);
b = (Button) v.findViewById(R.id.btn);
e = (EditText) v.findViewById(R.id.et);

在引用其中的视图时使用膨胀的视图对象。

Frage片段

中需要进行类似的修正