---恢复内容开始---
今天写智能灯客户端时遇到了不少问题:
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连接出了问题:
android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
stack over flow 上的解决方法:
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连接出了问题:
android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
stack over flow 上的解决方法:
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
}