在android中尝试在片段之间获取数据时出现java.lang.NullPointerException
我正在尝试开发一个依赖于web服务的android应用程序,我正在使用片段让我的应用程序在手机和平板电脑上运行 这是在两种模式下保存片段并检查哪种模式的活动 然后根据具体模式定义侦听器,在DetailFragment(横向)或DetailActivity(纵向)中显示详细信息 这是DetailFragment的代码在android中尝试在片段之间获取数据时出现java.lang.NullPointerException,java,android,android-fragments,Java,Android,Android Fragments,我正在尝试开发一个依赖于web服务的android应用程序,我正在使用片段让我的应用程序在手机和平板电脑上运行 这是在两种模式下保存片段并检查哪种模式的活动 然后根据具体模式定义侦听器,在DetailFragment(横向)或DetailActivity(纵向)中显示详细信息 这是DetailFragment的代码 public class DetailsFragment extends Fragment { public static DetailsFragment newInsta
public class DetailsFragment extends Fragment {
public static DetailsFragment newInstance(int index) {
DetailsFragment f = new DetailsFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(container == null)
return null;
View v = inflater.inflate(R.layout.details,container,false);
TextView txtAffichage = (TextView) v.findViewById(R.id.txtAffichage);
txtAffichage.setText(AppelsFragment.appels.get(getShownIndex()).getNumOrdre().toString());
return v;
}
}
此指令引发的问题
txtAffichage.setText(AppelsFragment.appels.get(getShownIndex()).getNumOrdre().toString());
我想我定义清单的方式是
public static List<Appel> appels = null;
以及details.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txtAffichage"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
此行:
TextView txtAffichage = (TextView) v.findViewById(R.id.txtAffichage);
可能正在返回
null
。确保R.id.txtAffichage
在布局资源文件的两个版本(纵向和横向)中都有效。我编辑了我的文章以添加一些代码,但经过一些研究(和),如果我错了,请纠正我。。asyncTask中的问题与片段相关,asnycTask负责从Rest API中获取数据作为列表,该列表列出了我在片段中尝试显示和使用的数据,由于连接和网络质量的原因,asyncTask无法在片段尝试显示数据之前检索数据
当触发此指令时
TextView txtAffichage = (TextView) v.findViewById(R.id.txtAffichage);
txtAffichage.setText(AppelsFragment.appels.get(getShownIndex()).getNumOrdre().toString());
列表appels仍然是空的,从那里可以看到异常NullPointerException
我认为装载机是解决这个问题的方法,如果不是,请纠正我
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txtAffichage"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
TextView txtAffichage = (TextView) v.findViewById(R.id.txtAffichage);
TextView txtAffichage = (TextView) v.findViewById(R.id.txtAffichage);
txtAffichage.setText(AppelsFragment.appels.get(getShownIndex()).getNumOrdre().toString());