我正在尝试存档具有2个片段(BaseFragment和BlankFragment)的多组(平板电脑和手机)应用。当我运行时,它显示消息并指示onCreate(MainActivity.java:18)** **
android.view.InflateException:二进制XML文件行#8:错误 膨胀类片段
**
第18行--- setContentView(R.layout.activity_main);
**
FATAL EXCEPTION: main
Process: com.santossingh.tablet, PID: 2164
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.santossingh.tablet/com.santossingh.tablet.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at com.santossingh.tablet.MainActivity.onCreate(MainActivity.java:18)
这是我的代码: 清单代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.santossingh.tablet"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.santossingh.tablet.MainActivity"/>
</activity>
</application>
</manifest>
MainActivity类:
public class MainActivity extends AppCompatActivity implements BaseFragment.onclickedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public void onSelectedItem(String s) {
BlankFragment blankFragment=
(BlankFragment)getSupportFragmentManager().findFragmentById(R.id
.blankFrg);
if (blankFragment==null){
Intent intent= new Intent(MainActivity.this,SecondActivity.class)
.putExtra("moviename",s);
startActivity(intent);
}else{
blankFragment.update(s);
}
}
}
2)BaseFragment
public class BaseFragment extends Fragment {
ListView list;
ArrayAdapter<String> adapter;
String[] data;
private onclickedListener onclickedListener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_second, container, false);
list=(ListView)view.findViewById(R.id.listView);
data=getResources().getStringArray(R.array.movie_name);
adapter=new ArrayAdapter<String>(getActivity(), R.layout.listview_fragment, R.id.textView, data);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String moviename=data[position];
onclickedListener.onSelectedItem(moviename);
}
});
return view;
}
public interface onclickedListener{
public void onSelectedItem(String s);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
onclickedListener=(onclickedListener)context;
}catch (Exception e){
throw e;
}
}
}
3)BlankFragment
public class BlankFragment extends Fragment {
String[] data2;
TextView r;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_second, container, false);
r=(TextView)view.findViewById(R.id.result);
data2=getResources().getStringArray(R.array.movie_detail);
return view;
}
public void update(String s){
if (s.equals("superman")){
r.setText(s+" ["+data2[0]+"]");
}
else if (s.equals("Hulk")){
r.setText(s+" ["+data2[0]+"]");
}
else if (s.equals("Ironman")){
r.setText(s+" ["+data2[0]+"]");
}
else if (s.equals("Spiderman")){
r.setText(s+" ["+data2[0]+"]");
}
else if (s.equals("Batman")){
r.setText(s+" ["+data2[0]+"]");
}
}
}
4)SecondActivity
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String moviename= getIntent().getStringExtra("moviename");
BlankFragment blankFragment=(BlankFragment)getSupportFragmentManager().findFragmentById(R
.id.blankFrg);
blankFragment.update(moviename);
}
}
** 我解决了...在onCreateView()的BaseFragment类下是一个小错误,这是解决方案: **
**
View view = inflater.inflate(R.layout.fragment_base,container,false);
**