Android 开发问题

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



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

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。这个问题是因为在子线程中弹出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);
    }

7.Adapter内设置字体颜色无效

参考链接
有效:
holder.tv_1.setTextColor(Color.parseColor(“#FF0000”));
无效:
holder.tv_1.setTextColor(R.color.red);

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值