Android 开发问题

Android 开发问题,第1张

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

Android开发问题
    • 1.Fragment 内findViewByid空指针问题
    • 2.getApplication 空指针问题
    • 3.NullPointerException异常:
    • 4.Can't toast on a thread that has not called Looper.prepare()问题
    • 5.okhttp3 response内的数据无法在外部使用
    • 6.TabLayout 加载适配器后TabItem不显示问题



提示:以下是本篇文章正文内容,下面案例可供参考

1.Fragment 内findViewByid空指针问题
Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

解决方法:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v=inflater.inflate(R.layout.fragment_main, container, false);

        smartRefreshLayout= v.findViewById(R.id.smart_refreshLayout);

        grid_div=v.findViewById(R.id.grid_divs);

        tv_add_dev =v.findViewById(R.id.tv_add_dev);
        layout_2_set_location =v.findViewById(R.id.layout_2_set_location);
 
        return v;

    }
2.getApplication 空指针问题
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()’ on a null object reference

解决办法:定义可以在OnCreat外,但FindViewByid要在Oncreat内。


3.NullPointerException异常:
FragmentManager.beginTransaction()
Fragment fragment =this.getsupportFragmentManager();

4.Can’t toast on a thread that has not called Looper.prepare()问题

问题描述:

 java.lang.NullPointerException: Can't toast on a thread that has not called Looper.prepare()

如果在一个线程中没有调用Looper.prepare(),就不能在该线程中创建Toast。


这个问题是因为在子线程中d出Toast导致的。


解决办法:先调用Looper.prepare();再调用Toast.makeText().show();最后再调用Looper.loop();

Looper.prepare();
Toast.makeText(Modify_pho_or_passActivity.this,"修改成功!",Toast.LENGTH_LONG).show();
Looper.loop();
5.okhttp3 response内的数据无法在外部使用

问题描述:在okhttp中我们成功的访问后台,然后从onResponse中获得了返回的json数据),这个返回的数据只能在onResponse方法内调用,放到方法外这个数据就无法使用,追根揭底就是okhttp是异步请求,你没办法将异步请求的结果放到主线程中使用。


自己使用的RecycleView,在onResponse外使用,无数据进adapter;在onResponse内使用,提示

Only the original thread that created a view hierarchy can touch its views.

异常的意思是说只有创建这个view的线程才能 *** 作这个 view,普通会认为是将view创建在非UI线程中才会出现这个错误。



解决办法:在onResponse内创建了runOnUiThread,写入recycleView的配置,将其在主线程中 *** 作。



@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
               final String responseData = response.body().string();
               //解析json
               devices = JsonManager.parseJsonArray(responseData, DeviceEntity.class);
              // Log.e("devices",devices.get(0).getDevicename());
               runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       if(devices!=null)
                       {
                           rv_devices_list.setLayoutManager(new LinearLayoutManager(DeviceManagerActivity.this));
                           devListAdapter =new DevListAdapter(devices);
                           rv_devices_list.setAdapter(devListAdapter);
                           // rv_devices_list.setVisibility(View.VISIBLE);
                       }
                   }
               });
           }
       });
6.TabLayout 加载适配器后TabItem不显示问题

问题描述:
在XMl文件中静态加载TabItem,tabLayout.setupWithViewPager(viewPager)之前可以显示,之后就不能显示。


原因及解决办法:
https://blog.csdn.net/sundy_tu/article/details/52682246

最好从PageAdapter内动态添加TabItem。


 @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        title.clear();
        title.add("查询数据");
        title.add("统计数据");
        return title.get(position);
    }
``

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/625894.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-16
下一篇 2022-04-16

发表评论

登录后才能评论

评论列表(0条)

保存