对于我的主要详细信息流应用,我试图在我的平板电脑上对我的大陆列表视图进行充气,但它不会这样做,我最终在我的日志猫中出现了这些错误。有谁知道我做错了什么?
大陆列表xml
menu = [
# TODO: define your encrypt, decrypt and secure_encrypt functions
("Encrypt message", encrypt),
("Decrypt message", decrypt),
("Secure Encrypt Message", secure_encrypt),
("Exit", sys.exit),
]
for n, (label, _) in enumerate(menu, start=1):
print("{}) {}".format(n, label))
selection = input("Please select: ")
assert 1 <= selection <= len(menu), "TODO: better error handling"
menu[selection - 1][1]()
ContinentsListActivity.java
<LinearLayout 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:layout_marginLeft="16dp"
android:layout_marginRight="16dp" android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal" android:orientation="horizontal"
android:showDividers="middle" tools:context=".ItemListActivity">
<fragment android:id="@+id/continents_list" android:name="com.apptacularapps.md.ContinentsListFragment"
android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"
tools:layout="@android:layout/list_content" />
<FrameLayout android:id="@+id/item_detail_container" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_weight="3" />
</LinearLayout>
ContinentsListFragment.java
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class ContinentsListActivity extends ActionBarActivity
implements ContinentsListFragment.Callbacks {
private boolean mTwoPane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_continents_list);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.continents_list_contaniner, new ContinentsListFragment());
ft.commit();
if (findViewById(R.id.item_detail_container) != null) {
mTwoPane = true;
((ContinentsListFragment) getSupportFragmentManager()
.findFragmentById(R.id.continents_list))
.setActivateOnItemClick(true);
}
}
@Override
public void onItemSelected(String id) {
if("1".equals(id)){
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.continents_list, fragment)
.commit();
} else {
Intent detailIntent = new Intent(this, ContinentsListActivity.class);
startActivity(detailIntent);
}
}
}
}
logcat的
package com.apptacularapps.md;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ContinentsListFragment extends ListFragment {
private static final String STATE_ACTIVATED_POSITION = "activated_position";
private Callbacks mCallbacks = sContinentsCallbacks;
private int mActivatedPosition = ListView.INVALID_POSITION;
public interface Callbacks {
public void onItemSelected(String id);
}
private static Callbacks sContinentsCallbacks = new Callbacks() {
@Override
public void onItemSelected(String id) {
}
};
public ContinentsListFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: replace with a real list adapter.
setListAdapter(new ArrayAdapter<ContinentsContent.ContinentsItem>(
getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
ContinentsContent.ITEMS));
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (savedInstanceState != null
&& savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Activities containing this fragment must implement its callbacks.
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException("Activity must implement fragment's callbacks.");
}
mCallbacks = (Callbacks) activity;
}
@Override
public void onDetach() {
super.onDetach();
// Reset the active callbacks interface to the Continents implementation.
mCallbacks = sContinentsCallbacks;
}
@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
mCallbacks.onItemSelected(ContinentsContent.ITEMS.get(position).id);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mActivatedPosition != ListView.INVALID_POSITION) {
outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
}
}
public void setActivateOnItemClick(boolean activateOnItemClick) {
getListView().setChoiceMode(activateOnItemClick
? ListView.CHOICE_MODE_SINGLE
: ListView.CHOICE_MODE_NONE);
}
private void setActivatedPosition(int position) {
if (position == ListView.INVALID_POSITION) {
getListView().setItemChecked(mActivatedPosition, false);
} else {
getListView().setItemChecked(position, true);
}
mActivatedPosition = position;
}
}
答案 0 :(得分:3)
您的onAttach
的实施,
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException("Activity must implement fragment's callbacks.");
}
要解决此问题,您必须将implements Callbacks
添加到托管片段的Activity
。
编辑:
在您的活动的xml中,替换
<fragment android:id="@+id/continents_list" android:name="com.apptacularapps.md.ContinentsListFragment"
android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"
tools:layout="@android:layout/list_content" />
带
<FrameLayout android:id="@+id/continents_list_contaniner"
android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
在setContentView
之后执行的和onCreate():
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.continents_list_contaniner, new ContinentsListFragment());
ft.commit();
<强> EDIT2 强>:
在您的活动替换
中import android.app.FragmentManager;
import android.app.FragmentTransaction;
与
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction