它是使用兼容包的小型 Android 2.2 测试应用程序。我正在尝试在列表项选择的另一个活动上更新另一个片段上的文本视图。但问题是,每次第一次单击都会返回空指针异常,并且只有在第二次尝试时,它的文本才会发生变化。我想知道为什么会这样,什么是好的解决方案。
列表活动:-
public class ListActivity extends FragmentActivity implements
ListFragment.OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
}
@Override
public void onItemSelected(int index) {
// TODO Auto-generated method stub
// Check to see if there is a frame in which to embed the
// detail fragment directly in the containing UI.
View detailsFrame = findViewById(R.id.detailcontainer);
if (detailsFrame != null
&& detailsFrame.getVisibility() == View.VISIBLE) {
DetailFragment detailFragment = (DetailFragment) getSupportFragmentManager()
.findFragmentById(R.id.detailcontainer);
if (detailFragment == null) {
detailFragment = new DetailFragment();
}
// Execute a transaction, replacing any existing fragment
// with this one inside the frame.
getSupportFragmentManager().beginTransaction()
.replace(R.id.detailcontainer, detailFragment).commit();
detailFragment.setTextView(index);
} else {
// Otherwise we need to launch a new activity to display
Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
}
列表片段:-
public class ListFragment extends Fragment {
private OnItemSelectedListener listener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] countries = new String[] { "India", "Pakistan", "Sri Lanka",
"China", "Bangladesh", "Nepal", "Afghanistan", "North Korea",
"South Korea", "Japan" };
View view = inflater.inflate(R.layout.list_fragment, container, false);
ListView listView = (ListView) view.findViewById(R.id.listView);
// Populate list
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this.getActivity(), android.R.layout.simple_list_item_1,
countries);
listView.setAdapter(adapter);
// operation to do when an item is clicked
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(), "ListItem Number " + position,
Toast.LENGTH_SHORT).show();
listener.onItemSelected(position);
}
});
return view;
}
// Container Activity must implement this interface
public interface OnItemSelectedListener {
public void onItemSelected(int index);
}
// To ensure that the host activity implements this interface
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnItemSelectedListener) {
listener = (OnItemSelectedListener) activity;
} else {
throw new ClassCastException(activity.toString()
+ " must implemenet ListFragment.OnItemSelectedListener");
}
}
public void operation(int index) {
listener.onItemSelected(index);
}
}
详细活动:-
public class DetailActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DetailFragment details;
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
finish();
return;
}
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
details = new DetailFragment();
details.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, details).commit();
}
Bundle extras = getIntent().getExtras();
int index = extras.getInt("index");
try {
details = (DetailFragment) getSupportFragmentManager()
.findFragmentById(R.id.detailfragment);
details.setTextView(index);
} catch (NullPointerException ex) {
ex.getStackTrace();
}
}
}
细节片段:-
public class DetailFragment extends Fragment {
String[] capitals;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (container == null)
return null;
capitals = new String[] { "delhi", "karachi", "colombo", "beijing",
"dhaka", "katmandu", "Afghanistan", "pyongyang", "seoul",
"tokyo" };
View v = inflater.inflate(R.layout.detail_fragment, container, false);
return v;
}
public void setTextView(int index) {
try {
TextView view = (TextView) getView().findViewById(R.id.detailView);
view.setText(capitals[index]);
} catch (NullPointerException ex) {
ex.getStackTrace();
}
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
}
更新:- 我正在添加 detailFragment xml:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@color/lightblue"
android:orientation="vertical" >
<TextView
android:id="@+id/detailView"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/detail_frag" />
</RelativeLayout>