我有錯誤的東西,它的書面形式像android視圖膨脹異常錯誤在膨脹類的片段,它指向MainActivity.java類,我試圖找到但仍然沒有運氣,因爲沒有錯誤在android studio中,但在虛擬設備中運行時顯示錯誤。錯誤膨脹類的片段,inflateexception
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 studio中沒有錯誤,但在虛擬設備中運行時顯示錯誤。
show logcat please –
首先,你不是在'FragmentA'的'onCreateView()'中返回佈局。但是,我們需要看到完整的堆棧跟蹤,以確定當前的問題。 –