我正在尝试使用带有ActionBarSherlock库的片段编写应用程序。
所以,我有一个问题。
我的布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_FrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
class="ru.neverdark.phototools.fragments.TitlesFragment"
android:id="@+id/main_titlesFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
我的MainActivity类:
package ru.neverdark.phototools;
import ru.neverdark.phototools.log.Log;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import android.os.Bundle;
public class MainActivity extends SherlockFragmentActivity {
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.message("Enter");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
My TitlesFragment class:
package ru.neverdark.phototools.fragments;
import ru.neverdark.phototools.Constants;
import ru.neverdark.phototools.R;
import ru.neverdark.phototools.log.Log;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockListFragment;
public class TitlesFragment extends SherlockListFragment {
boolean mDualPane;
int mCurrentCheckPosition = 0;
final String[] TITLES = getResources().getStringArray(R.array.main_menuTitles);
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.message("Enter");
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.menu_item,
R.id.menuItem_label_title,
TITLES));
View detailsFrame = getActivity().findViewById(R.id.main_detailFragment);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState != null) {
mCurrentCheckPosition = savedInstanceState.getInt(Constants.CURRENT_CHOICE, 0);
}
if (mDualPane) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
showDetails(mCurrentCheckPosition);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(Constants.CURRENT_CHOICE, mCurrentCheckPosition);
}
@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
showDetails(position);
}
private void showDetails(int index) {
mCurrentCheckPosition = index;
if (mDualPane == true) {
getListView().setItemChecked(index, true);
showFragment(index);
} else {
showActivity(index);
}
}
private void showFragment(int index) {
}
private void showActivity(int index) {
}
}
当我运行应用程序时,我得到一个异常
java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.neverdark.phototools/ru.neverdark.phototools.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
调用setContentView函数时发生异常。
提前感谢您的帮助。
答案 0 :(得分:0)
问题已解决。
问题字符串:
final String[] TITLES = getResources().getStringArray(R.array.main_menuTitles);
我改为:
final String[] TITLES = {"test1", "test2"};//getResources().getStringArray(R.array.main_menuTitles);
它对我来说很好。
塔伦,谢谢你帮助调查问题。