在纵向中,当单击列表项但是横向工作正常时,它会给出零点异常...在AnotherAcitivity.java片段f2在调试时变为空...
package com.example.myfragmentqadvancedel;
import com.example.myfragmentqadvancedel.FragmentA.Communicator;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
public class MainActivity extends Activity implements Communicator{
FragmentA f1;
FragmentB f2;
FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getFragmentManager();
f1=(FragmentA) manager.findFragmentById(R.id.fragment1);
f1.setCommunicator(this);
}
@Override
public void respond(int index) {
f2=(FragmentB) manager.findFragmentById(R.id.fragment2);
if (f2!=null && f2.isVisible()) {
f2.changeData(index );
}else {
Intent intent = new Intent(this,AnotherActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
}
FragmentA
package com.example.myfragmentqadvancedel;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FragmentA extends Fragment implements OnItemClickListener {
ListView list;
Communicator communicator;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_a, container,false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
list=(ListView) getActivity().findViewById(R.id.listView1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.chapters, android.R.layout.simple_list_item_1);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
}
public void setCommunicator(Communicator communicator) {
this.communicator = communicator;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int i, long arg3) {
communicator.respond(i);
}
public interface Communicator{
public void respond(int index);
}
}
AnotherActivity.java
package com.example.myfragmentqadvancedel;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class AnotherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
Intent intent = getIntent();
int index=intent.getIntExtra("index", 0);
FragmentB f2= (FragmentB) getFragmentManager().findFragmentById(R.id.fragment2);
if(f2!=null) <-- f2 gets null, index gets +ve value as checked in debugger
f2.changeData(index);
}
}
FragmentB
package com.example.myfragmentqadvancedel;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FragmentB extends Fragment{
TextView text;
@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) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
text=(TextView) getActivity().findViewById(R.id.textView1);
}
public void changeData(int index){
String[] descriptions = getResources().getStringArray(R.array.description);
text.setText(descriptions[index]);
}
}
logcat的 05-27 16:40:37.866:E / AndroidRuntime(961):致命异常:主
05-27 16:40:37.866: E/AndroidRuntime(961): java.lang.NullPointerException
05-27 16:40:37.866: E/AndroidRuntime(961): at com.example.myfragmentqadvance.MainActivity.respond(MainActivity.java:26)
05-27 16:40:37.866: E/AndroidRuntime(961): at com.example.myfragmentqadvance.Fragment1.onItemClick(Fragment1.java:42)
05-27 16:40:37.866: E/AndroidRuntime(961): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
05-27 16:40:37.866: E/AndroidRuntime(961): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
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=".MainActivity">
<fragment
android:id="@+id/fragment1"
android:name="com.example.myfragmentqadvancedel.FragmentA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#0f0">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
fragment_b.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00f" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#fff"/>
</LinearLayout>
activity_another.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=".AnotherActivity" >
<fragment
android:id="@+id/fragment2"
android:name="com.example.myfragmentqadvancedel.FragmentB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp" />
</RelativeLayout>
答案 0 :(得分:0)
您在Fragment A中的界面似乎是空的。您应该使用onAttach(Activity activity)
方法,而不是在按ID查找片段后设置接口。在这里,您可以强制您的父Activity实现此接口。看看下面的代码。
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
/* Check if the Activity implemented Callbacks from this Fragment */
try {
communicator = (Communicator) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement " + this.getClass().getSimpleName() + "Callbacks");
}
}
答案 1 :(得分:0)
当我将代码frm onCreate保存到AnotherActivity的onStart时,它可以工作......不知道将它们放入onCreate的概率是什么
公共类AnotherActivity扩展了Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
}
@Override
protected void onStart() {
super.onStart();
Intent intent = getIntent();
int index=intent.getIntExtra("index", 0);
FragmentB f2= (FragmentB) getFragmentManager().findFragmentById(R.id.fragment2);
if(f2!=null)
f2.changeData(index);
}
}