java.lang.NullPointerException:尝试调用虚拟方法';void android.widget.TextView.setText(java.lang.CharSequence)和#x27;
我想在片段的textview上显示字符串(解析的json文件) 我用导航抽屉活动开始了这个项目 解析成功,但是 java.lang.NullPointerException:尝试调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)” 发生错误 我怎样才能修好它 MainAcitvity.javajava.lang.NullPointerException:尝试调用虚拟方法';void android.widget.TextView.setText(java.lang.CharSequence)和#x27;,android,nullpointerexception,Android,Nullpointerexception,我想在片段的textview上显示字符串(解析的json文件) 我用导航抽屉活动开始了这个项目 解析成功,但是 java.lang.NullPointerException:尝试调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)” 发生错误 我怎样才能修好它 MainAcitvity.java TextView tv=(TextView)findViewById(R.id.text_slideshow); Str
TextView tv=(TextView)findViewById(R.id.text_slideshow);
StringBuilder sb=new StringBuilder();
String json = null;
try {
InputStream is = getAssets().open("tour.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
Log.i("TEST", "afterfileio");
JSONParser parser=new JSONParser();
JSONObject jsonObject= (JSONObject) parser.parse(json);
JSONObject jsonObject1=(JSONObject)jsonObject.get("facilityConveInfo");
JSONArray jsonArray = (JSONArray) jsonObject1.get("row");
Log.i("TEST", ""+jsonArray.size() + "");
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject subJsonObject = (JSONObject) jsonArray.get(i);
String FACILITY_NM =subJsonObject.get("FACILITY_NM").toString();
sb.append(FACILITY_NM);
}
tv.setText(sb.toString());
TextView电视=(TextView)findviewbyd(R.id.text\u幻灯片);
StringBuilder sb=新的StringBuilder();
字符串json=null;
试一试{
InputStream=getAssets().open(“tour.json”);
int size=is.available();
字节[]缓冲区=新字节[大小];
is.read(缓冲区);
is.close();
json=新字符串(缓冲区,“UTF-8”);
Log.i(“测试”、“后文件IO”);
JSONParser=新的JSONParser();
JSONObject JSONObject=(JSONObject)parser.parse(json);
JSONObject JSONObject 1=(JSONObject)JSONObject.get(“facilityConveInfo”);
JSONArray JSONArray=(JSONArray)jsonObject1.get(“行”);
Log.i(“TEST”,“+jsonArray.size()+”);
for(int i=0;i
fragment_slideshow.xml
<TextView
android:id="@+id/text_slideshow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
SlideFragment.java
public class SlideshowFragment extends Fragment {
private SlideshowViewModel slideshowViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
slideshowViewModel =
ViewModelProviders.of(this).get(SlideshowViewModel.class);
View root = inflater.inflate(R.layout.fragment_slideshow, container, false);
final TextView textView = root.findViewById(R.id.text_slideshow);
slideshowViewModel.getText().observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;
公共类SlideshowFragment扩展了片段{
私人SlideshowViewModel SlideshowViewModel;
公共视图onCreateView(@NonNull layoutiner充气机,
视图组容器,捆绑包savedInstanceState){
幻灯片视图模型=
ViewModelProviders.of(this.get)(SlideshowViewModel.class);
视图根=充气机。充气(R.layout.fragment\u幻灯片,容器,假);
final TextView TextView=root.findviewbyd(R.id.text\u幻灯片);
slideshowViewModel.getText().observe(这个,新的观察者(){
@凌驾
公共void onChanged(@Nullable String s){
textView.setText;
}
});
返回根;
在我看来,您从未创建过片段,因此sb
引用null
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, yourFragment);
fragmentTransaction.commit();