Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.NullPointerException:尝试调用虚拟方法';void android.widget.TextView.setText(java.lang.CharSequence)和#x27;_Android_Nullpointerexception - Fatal编程技术网

java.lang.NullPointerException:尝试调用虚拟方法';void android.widget.TextView.setText(java.lang.CharSequence)和#x27;

java.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上显示字符串(解析的json文件)

我用导航抽屉活动开始了这个项目

解析成功,但是

java.lang.NullPointerException:尝试调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”

发生错误

我怎样才能修好它

MainAcitvity.java

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();
    
  • 祝你好运

    可能会重复