我的应用程序使用ActionBar选项卡片段来创建/修改设备详细信息。 Device属性分为不同的片段。我想在Tab Activity的OnCreate中初始化每个片段。在每个选项卡片段中都有 SetValues 方法,该方法初始化要在片段中查看的值。我注意到当我点击特定标签时会创建片段。
标签活动
public class CameraDetails extends Activity {
private CameraData camera = new CameraData();
Fragment network = new NetworkFragment();
Fragment remoteUser = new RemoteUserFragment();
private G.Interface mNetworkInterface = null;
private G.Interface mRemoteUserInterface = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cameradetails);
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// network tab
ActionBar.Tab tab = actionBar.newTab();
tab.setText("Network");
tab.setIcon(R.drawable.ic_launcher);
tab.setTabListener(new TabListener(network));
actionBar.addTab(tab);
// remote user tab
tab = actionBar.newTab();
tab.setText("Remote User");
tab.setIcon(R.drawable.ic_launcher);
tab.setTabListener(new TabListener(remoteUser));
actionBar.addTab(tab);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
init();
}
}, 1000);
}
public void init(){
try{
byte[] by = new byte[512];
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory, "dump.hex");
BufferedInputStream in = new BufferedInputStream( new FileInputStream(file));
in.read(by);
in.close();
camera.set( by );
mNetworkInterface.SetValues();
mRemoteUserInterface.SetValues();
} catch(Exception e){
e.printStackTrace();
}
}
public CameraData getCameraData(){
return camera;
}
public void setNetworkListener( G.Interface Interface ){
this.mNetworkInterface = Interface;
}
public void setRemoteUserListener( G.Interface Interface ){
this.mRemoteUserInterface = Interface;
}
}
其中一个片段标签
public class NetworkFragment extends Fragment implements G.Interface {
View rootView;
Activity mActivity;
CameraData camera;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.network_layout, container, false);
Log.i("NetworkFragment", "CreateView");
return rootView;
}
@Override
public void onAttach(Activity activity) {
mActivity = activity;
((CameraDetails)mActivity).setNetworkListener(this);
camera = ((CameraDetails)mActivity).getCameraData();
Log.i("NetworkFragment", "Activity Attached");
super.onAttach(activity);
}
@Override
public void SetValues() {
try {
// ip address
EditText et = (EditText)rootView.findViewById(R.id.cd_ip_address);
et.setText(camera.mNetwork.IPAddress);
// subnet mask
et = (EditText)rootView.findViewById(R.id.cd_subnetmask);
et.setText(camera.mNetwork.SubnetMask);
// gateway
et = (EditText)rootView.findViewById(R.id.cd_gateway);
et.setText(camera.mNetwork.Gateway);
// com port
et = (EditText)rootView.findViewById(R.id.cd_comm_port);
et.setText(""+camera.mNetwork.CommPort);
// mimg port
et = (EditText)rootView.findViewById(R.id.cd_mimg_port);
et.setText(""+camera.mNetwork.MImgPort);
// webserver port
et = (EditText)rootView.findViewById(R.id.cd_web_server_port);
et.setText(""+camera.mNetwork.WebServerPort);
// wifi id
et = (EditText)rootView.findViewById(R.id.cd_wifi_id);
et.setText(camera.mNetwork.WiFiId);
// wifi password
et = (EditText)rootView.findViewById(R.id.cd_wifi_pwd);
et.setText(camera.mNetwork.WiFiPassword);
// wifi mode
Spinner sp = (Spinner)rootView.findViewById(R.id.cd_wifi_mode);
sp.setSelection(camera.mNetwork.WiFiMode, true);
Log.i("NetworkFragment", "SetValues OK");
} catch( Exception e ){
}
}
}
标签活动的初始化方法会将值初始化为片段 NetworkFragment 和 RemoteUserFragment 。为NetworkFragment初始化值。但RemoteUserFragment的NullPointerException。
logcat的
06-11 12:06:38.161: W/System.err(677): java.lang.NullPointerException
06-11 12:06:38.201: W/System.err(677): at com.mtxlabs.cameraviewer.CameraDetails.init(CameraDetails.java:114)
06-11 12:06:38.201: W/System.err(677): at com.mtxlabs.cameraviewer.CameraDetails$1.run(CameraDetails.java:98)
06-11 12:06:38.201: W/System.err(677): at android.os.Handler.handleCallback(Handler.java:615)
06-11 12:06:38.211: W/System.err(677): at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 12:06:38.211: W/System.err(677): at android.os.Looper.loop(Looper.java:137)
06-11 12:06:38.211: W/System.err(677): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-11 12:06:38.211: W/System.err(677): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 12:06:38.211: W/System.err(677): at java.lang.reflect.Method.invoke(Method.java:511)
06-11 12:06:38.211: W/System.err(677): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-11 12:06:38.211: W/System.err(677): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 12:06:38.211: W/System.err(677): at dalvik.system.NativeStart.main(Native Method)
我如何解决这个问题或是否使用其他技术来做到这一点。
提前谢谢
答案 0 :(得分:0)
我在片段的onCreateView中调用了 SetValues 方法。最后我解决了问题
主要活动
public class CameraDetails extends Activity {
private CameraData camera = new CameraData();
Fragment network = new NetworkFragment();
Fragment remoteUser = new RemoteUserFragment();
Fragment rtsp = new RTSPFragmentTab();
Fragment video = new VideoFragmentTab();
Fragment record = new RecordFragmentTab();
Fragment alarm = new AlarmFragmentTab();
Fragment productinfo = new ProductInfoFragmentTab();
private G.Interface mNetworkInterface = null;
private G.Interface mRemoteUserInterface = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cameradetails);
initCameraData();
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// network tab
ActionBar.Tab tab = actionBar.newTab();
tab.setText("Network");
tab.setIcon(R.drawable.ic_launcher);
tab.setTabListener(new TabListener(network));
actionBar.addTab(tab);
// remote user tab
tab = actionBar.newTab();
tab.setText("Remote User");
tab.setIcon(R.drawable.ic_launcher);
tab.setTabListener(new TabListener(remoteUser));
actionBar.addTab(tab);
}
public void initCameraData(){
try{
byte[] by = new byte[512];
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory, "dump.hex");
BufferedInputStream in = new BufferedInputStream( new FileInputStream(file));
in.read(by);
in.close();
camera.set( by );
} catch(Exception e){
e.printStackTrace();
}
}
public void onClick(View v){
getActionBar().setSelectedNavigationItem(0);
if(!mNetworkInterface.Validate())
return;
getActionBar().setSelectedNavigationItem(1);
if(!mRemoteUserInterface.Validate())
return;
try{
byte[] bytes = camera.get();
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory, "dump.hex");
BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(file, false));
out.write(bytes);
out.close();
Log.v("Data", "Setup data updated");
} catch (Exception e) {
e.printStackTrace();
return ;
}
}
public CameraData getCameraData(){
return camera;
}
public void setNetworkListener( G.Interface Interface ){
this.mNetworkInterface = Interface;
}
public void setRemoteUserListener( G.Interface Interface ){
this.mRemoteUserInterface = Interface;
}
}
网络片段
public class NetworkFragment extends Fragment implements G.Interface {
View rootView;
Activity mActivity;
CameraData camera;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.network_layout, container, false);
Log.i("NetworkFragment", "CreateView");
SetValues();
return rootView;
}
@Override
public void onAttach(Activity activity) {
mActivity = activity;
((CameraDetails)mActivity).setNetworkListener(this);
camera = ((CameraDetails)mActivity).getCameraData();
Log.i("NetworkFragment", "Activity Attached");
super.onAttach(activity);
}
@Override
public boolean Validate() {
try {
String s;
int i;
// ip address
camera.mNetwork.IPAddress = G.GetIPText(rootView, R.id.cd_ip_address);
if( camera.mNetwork.IPAddress.isEmpty()){
Toast.makeText(mActivity, "Please enter a valid ip address", Toast.LENGTH_SHORT).show();
return false;
}
// subnet mask
camera.mNetwork.SubnetMask = G.GetIPText(rootView, R.id.cd_subnetmask);
if( camera.mNetwork.SubnetMask.isEmpty() ){
Toast.makeText(mActivity, "Please enter a valid subnetmask", Toast.LENGTH_SHORT).show();
return false;
}
// gateway
camera.mNetwork.Gateway = G.GetIPText(rootView, R.id.cd_gateway);
if( camera.mNetwork.Gateway.isEmpty()){
Toast.makeText(mActivity, "Please enter a valid gateway", Toast.LENGTH_SHORT).show();
return false;
}
// com port
camera.mNetwork.CommPort = G.GetInteger(rootView, R.id.cd_comm_port);
if( camera.mNetwork.CommPort < 0 ){
Toast.makeText(mActivity, "Please enter a valid Communication Port", Toast.LENGTH_SHORT).show();
return false;
}
// mimg port
camera.mNetwork.MImgPort = G.GetInteger(rootView, R.id.cd_mimg_port);
if( camera.mNetwork.MImgPort < 0 ){
Toast.makeText(mActivity, "Please enter a valid MIMG Port", Toast.LENGTH_SHORT).show();
return false;
}
// webserver port
camera.mNetwork.WebServerPort = G.GetInteger(rootView, R.id.cd_web_server_port);
if( camera.mNetwork.WebServerPort < 0 ){
Toast.makeText(mActivity, "Please enter a valid webserver Port", Toast.LENGTH_SHORT).show();
return false;
}
// wifi id
camera.mNetwork.WiFiId = G.GetText(rootView, R.id.cd_wifi_id);
if( camera.mNetwork.WiFiId.isEmpty() ){
Toast.makeText(mActivity, "Please enter a valid WiFi Id", Toast.LENGTH_SHORT).show();
return false;
}
// wifi password
camera.mNetwork.WiFiPassword = G.GetText(rootView, R.id.cd_wifi_pwd);
if( camera.mNetwork.WiFiPassword.isEmpty() ){
Toast.makeText(mActivity, "Please enter a valid WiFi Password", Toast.LENGTH_SHORT).show();
return false;
}
// wifi mode
camera.mNetwork.WiFiMode = (byte)G.GetSpinnerOptions(rootView, R.id.cd_wifi_mode);
if( camera.mNetwork.WiFiMode == -1 ){
Toast.makeText(mActivity, "Please select a valid wifi mode from list", Toast.LENGTH_SHORT).show();
return false;
}
Log.i("NetworkFragment", "Validation OK");
} catch( Exception e ){
e.printStackTrace();
}
return true;
}
public void SetValues() {
try {
// ip address
EditText et = (EditText)rootView.findViewById(R.id.cd_ip_address);
et.setText(camera.mNetwork.IPAddress);
// subnet mask
et = (EditText)rootView.findViewById(R.id.cd_subnetmask);
et.setText(camera.mNetwork.SubnetMask);
// gateway
et = (EditText)rootView.findViewById(R.id.cd_gateway);
et.setText(camera.mNetwork.Gateway);
// com port
et = (EditText)rootView.findViewById(R.id.cd_comm_port);
et.setText(""+camera.mNetwork.CommPort);
// mimg port
et = (EditText)rootView.findViewById(R.id.cd_mimg_port);
et.setText(""+camera.mNetwork.MImgPort);
// webserver port
et = (EditText)rootView.findViewById(R.id.cd_web_server_port);
et.setText(""+camera.mNetwork.WebServerPort);
// wifi id
et = (EditText)rootView.findViewById(R.id.cd_wifi_id);
et.setText(camera.mNetwork.WiFiId);
// wifi password
et = (EditText)rootView.findViewById(R.id.cd_wifi_pwd);
et.setText(camera.mNetwork.WiFiPassword);
// wifi mode
Spinner sp = (Spinner)rootView.findViewById(R.id.cd_wifi_mode);
sp.setSelection(camera.mNetwork.WiFiMode, true);
Log.i("NetworkFragment", "SetValues OK");
} catch( Exception e ){
e.printStackTrace();
}
}
}
无论如何,感谢 @ cricket_007 帮助我理解片段标签操作