博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
12月17日问题
阅读量:5256 次
发布时间:2019-06-14

本文共 10472 字,大约阅读时间需要 34 分钟。

---恢复内容开始---

今天写智能灯客户端时遇到了不少问题:

  1. 界面开发时想做一个底部有三个选项卡的界面,网上找了好久找到了 BottomNavigationView + ViewPager + Fragment的解决方法,图标是在阿里矢量图库找的,界面虽然做好了但原理不是很明白。。。。

  

public class MainActivity extends FragmentActivity implements Status.OnFragmentInteractionListener,Model.OnFragmentInteractionListener,         Sence.OnFragmentInteractionListener, BottomNavigationView.OnNavigationItemSelectedListener, ViewPager.OnPageChangeListener
package com.example.administrator.ledcontralapp;import android.net.Uri;import android.support.annotation.NonNull;import android.support.design.widget.BottomNavigationView;import android.support.v4.app.FragmentActivity;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.MenuItem;import android.view.View;import android.widget.ImageButton;import layout.Model;import layout.Sence;import layout.Status;public class MainActivity extends FragmentActivity implements Status.OnFragmentInteractionListener,Model.OnFragmentInteractionListener,        Sence.OnFragmentInteractionListener, BottomNavigationView.OnNavigationItemSelectedListener, ViewPager.OnPageChangeListener {
//不是很明白为啥要继承和实现接口(那么多接口)Status.OnFragmentInteractionListener,Model.OnFragmentInteractionListener,Sence.OnFragmentInteractionListener, //这几个不实现还报错
private ViewPager viewPager;    private MenuItem menuItem;    private BottomNavigationView bottomNavigationView;    private ImageButton imageButton;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        viewPager = (ViewPager)findViewById(R.id.viewPager);        bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom);        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));        //BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);        bottomNavigationView.setOnNavigationItemSelectedListener(                new BottomNavigationView.OnNavigationItemSelectedListener() {                    @Override                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {                        Log.v("zhang","click");                        switch (item.getItemId()) {                            case R.id.Model:                                viewPager.setCurrentItem(0);                                Log.v("zhang","model");                                break;                            case R.id.Scene:                                viewPager.setCurrentItem(1);                                Log.v("zhang","sence");                                break;                            case R.id.Status:                                viewPager.setCurrentItem(2);                                Log.v("zhang","status");                                break;                            default:                                viewPager.setCurrentItem(3);                                break;                        }                        return false;                    }                });    }    @Override    public boolean onNavigationItemSelected(@NonNull MenuItem item) {        return false;    }    @Override    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {    }    @Override    public void onPageSelected(int position) {    }    @Override    public void onPageScrollStateChanged(int state) {    }    @Override    public void onFragmentInteraction(Uri uri) {    }}

---恢复内容结束---

  2.再做Fragment的界面时为控件添加监听器时老是出现对象为空的异常:没弄清楚Fragment的机制 重写了onCreatView()方法

@Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        // Inflate the layout for this fragment        View view = inflater.inflate(R.layout.fragment_status, container, false);        ledContral = new LEDContral();        imageButton = (ImageButton)view.findViewById(R.id.ledlight);        imageButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if(light)                {                    ledContral.closeLED();                    imageButton.setImageDrawable(getResources().getDrawable(R.drawable.ledoff));                    light = false;                }                else                {                    ledContral.openLED();                    imageButton.setImageDrawable(getResources().getDrawable(R.drawable.ledon));                    light = true;                }            }        });        return view;    }

  3:在android studio中引入eclipse写的java代码时,socket连接出了问题:

  

      stack over flow 上的解决方法:

 

 

55

This exception is thrown when application attempts to perform a networking operation in the main thread. Use below code in your onViewCreated to avoid this error else Call your networking operations (getting data from web server) request in thread or Asynch class.

 

if (SDK_INT > 8)     {        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()                .permitAll().build();        StrictMode.setThreadPolicy(policy);        //your codes here    }

---恢复内容开始---

今天写智能灯客户端时遇到了不少问题:

  1. 界面开发时想做一个底部有三个选项卡的界面,网上找了好久找到了 BottomNavigationView + ViewPager + Fragment的解决方法,图标是在阿里矢量图库找的,界面虽然做好了但原理不是很明白。。。。

  

public class MainActivity extends FragmentActivity implements Status.OnFragmentInteractionListener,Model.OnFragmentInteractionListener,         Sence.OnFragmentInteractionListener, BottomNavigationView.OnNavigationItemSelectedListener, ViewPager.OnPageChangeListener
package com.example.administrator.ledcontralapp;import android.net.Uri;import android.support.annotation.NonNull;import android.support.design.widget.BottomNavigationView;import android.support.v4.app.FragmentActivity;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.MenuItem;import android.view.View;import android.widget.ImageButton;import layout.Model;import layout.Sence;import layout.Status;public class MainActivity extends FragmentActivity implements Status.OnFragmentInteractionListener,Model.OnFragmentInteractionListener,        Sence.OnFragmentInteractionListener, BottomNavigationView.OnNavigationItemSelectedListener, ViewPager.OnPageChangeListener {
//不是很明白为啥要继承和实现接口(那么多接口)Status.OnFragmentInteractionListener,Model.OnFragmentInteractionListener,Sence.OnFragmentInteractionListener, //这几个不实现还报错
private ViewPager viewPager;    private MenuItem menuItem;    private BottomNavigationView bottomNavigationView;    private ImageButton imageButton;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        viewPager = (ViewPager)findViewById(R.id.viewPager);        bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom);        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));        //BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);        bottomNavigationView.setOnNavigationItemSelectedListener(                new BottomNavigationView.OnNavigationItemSelectedListener() {                    @Override                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {                        Log.v("zhang","click");                        switch (item.getItemId()) {                            case R.id.Model:                                viewPager.setCurrentItem(0);                                Log.v("zhang","model");                                break;                            case R.id.Scene:                                viewPager.setCurrentItem(1);                                Log.v("zhang","sence");                                break;                            case R.id.Status:                                viewPager.setCurrentItem(2);                                Log.v("zhang","status");                                break;                            default:                                viewPager.setCurrentItem(3);                                break;                        }                        return false;                    }                });    }    @Override    public boolean onNavigationItemSelected(@NonNull MenuItem item) {        return false;    }    @Override    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {    }    @Override    public void onPageSelected(int position) {    }    @Override    public void onPageScrollStateChanged(int state) {    }    @Override    public void onFragmentInteraction(Uri uri) {    }}

---恢复内容结束---

  2.再做Fragment的界面时为控件添加监听器时老是出现对象为空的异常:没弄清楚Fragment的机制 重写了onCreatView()方法

@Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        // Inflate the layout for this fragment        View view = inflater.inflate(R.layout.fragment_status, container, false);        ledContral = new LEDContral();        imageButton = (ImageButton)view.findViewById(R.id.ledlight);        imageButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if(light)                {                    ledContral.closeLED();                    imageButton.setImageDrawable(getResources().getDrawable(R.drawable.ledoff));                    light = false;                }                else                {                    ledContral.openLED();                    imageButton.setImageDrawable(getResources().getDrawable(R.drawable.ledon));                    light = true;                }            }        });        return view;    }

  3:在android studio中引入eclipse写的java代码时,socket连接出了问题:

  

      stack over flow 上的解决方法:

 

 

55

This exception is thrown when application attempts to perform a networking operation in the main thread. Use below code in your onViewCreated to avoid this error else Call your networking operations (getting data from web server) request in thread or Asynch class.

 

if (SDK_INT > 8)     {        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()                .permitAll().build();        StrictMode.setThreadPolicy(policy);        //your codes here    }

转载于:https://www.cnblogs.com/xiaochenztx/p/8052782.html

你可能感兴趣的文章
磁盘测试工具
查看>>
代码变量、函数命名神奇网站
查看>>
redis cli命令
查看>>
Problem B: 占点游戏
查看>>
python常用模块之sys, os, random
查看>>
HDU 2548 A strange lift
查看>>
Linux服务器在外地,如何用eclipse连接hdfs
查看>>
react双组件传值和传参
查看>>
[Kaggle] Sentiment Analysis on Movie Reviews
查看>>
价值观
查看>>
mongodb命令----批量更改文档字段名
查看>>
使用 SharedPreferences 分类: Andro...
查看>>
TLA+(待续...)
查看>>
题解: [GXOI/GZOI2019]与或和
查看>>
MacOS copy图标shell脚本
查看>>
国外常见互联网盈利创新模式
查看>>
Oracle-05
查看>>
linux grep 搜索查找
查看>>
Not enough free disk space on disk '/boot'(转载)
查看>>
android 签名
查看>>