膨胀类片段时出错,inflateexception

时间:2017-08-09 08:16:16

标签: java android android-fragments layout-inflater inflate-exception

我有错误的东西它写的像android视图膨胀异常错误膨胀类片段及其指向MainActivity.java类,我试图找到但仍然没有运气,因为Android工作室没有错误但错误显示在虚拟设备中运行时。

activity_main.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"
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.ai_agamcompaq.flexible.MainActivity">

<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="com.example.ai_agamcompaq.flexible.FragmentA"
    android:id="@+id/fragment"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="26dp" />

<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="com.example.ai_agamcompaq.flexible.FragmentB"
    android:id="@+id/fragment2"
    android:layout_below="@+id/fragment"
    android:layout_alignStart="@+id/fragment"
    android:layout_marginTop="39dp" />
</RelativeLayout>

MainActivity.java

import android.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements Communicator{

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

@Override
public void respond(int i) {
    FragmentManager manager=getFragmentManager();
    FragmentB f2= (FragmentB) manager.findFragmentById(R.id.fragment2);
    f2.changedata(i);
}
}

FragmentA.java

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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FragmentA extends Fragment implements 
AdapterView.OnItemClickListener {

ListView list;
Communicator communicator;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    communicator=(Communicator)getActivity();
    list= (ListView) getActivity().findViewById(R.id.listView);
    ArrayAdapter adapter=ArrayAdapter.createFromResource(getActivity(),R.array.titles,android.R.layout.simple_list_item_1);
    list.setAdapter(adapter);
    list.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
    communicator.respond(i);
}

FragmentB.java

import android.app.Fragment;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;


public class FragmentB extends Fragment {
    TextView text;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_b,container,false);
        return view;
    }

    @Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    text=(TextView) getActivity().findViewById(R.id.textView);
}
public void changedata(int i){
    Resources res=getResources();
    String[] description=res.getStringArray(R.array.description);
    text.setText(description[i]);
}
}




          Communicator.java
          public interface Communicator {
          public void respond(int i);
          }

fragment_b.xml

    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:id="@+id/editText" />
</LinearLayout>

fragment_a.xml

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentLeft="true"
    android:id="@+id/listView" />

的strings.xml

<resources>
    <string name="app_name">flexible</string>
    <string-array name="titles">
        <item>one</item>
        <item>two</item>
        <item>three</item>
        <item>four</item>
    </string-array>
    <string-array name="description">
        <item>oneoneoneoneoneoneoneoneoneoneo</item>
        <item>twotwotwotwotwotwotwotwotwotwo</item>
        <item>threethreethreethreethreethreethr</item>
        <item>fourfourfourfourfourfourfourfourfo</item>
    </string-array>

</resources>

我有错误的东西它写的像android视图膨胀异常错误膨胀类片段及其指向MainActivity.java类,我试图找到但仍然没有运气,因为Android工作室没有错误但错误显示在虚拟设备中运行时。

2 个答案:

答案 0 :(得分:0)

试试这种方式。将您的控件绑定在片段A的onmyview方法中

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.change_pass_frag, container);

        regmail=(EditText)view.findViewById(R.id.regemail);
        oldpass=(EditText)view.findViewById(R.id.oldpass);


        return view;
    }

答案 1 :(得分:0)

我知道了,我必须给fragment_a充气 查看view = inflater.inflate(R.layout.fragment_a,container,false);