Android 片段已膨胀,但方法没有影响
我是android/java开发新手,在正确实现我的方法“updateBinaryInput()”时遇到了问题。在我的代码中,我膨胀了一个片段,为了简单起见,我只想在当前膨胀的布局中设置一个文本视图。我不确定是否需要从主活动调用此方法。根据我的理解,因为布局文件是通过片段“fragment”膨胀的,所以处理膨胀的layout.xml的方法应该在片段中。有人能给我一个很好的例子,说明处理当前片段布局的方法的良好实现。Android 片段已膨胀,但方法没有影响,android,android-fragments,methods,android-inflate,Android,Android Fragments,Methods,Android Inflate,我是android/java开发新手,在正确实现我的方法“updateBinaryInput()”时遇到了问题。在我的代码中,我膨胀了一个片段,为了简单起见,我只想在当前膨胀的布局中设置一个文本视图。我不确定是否需要从主活动调用此方法。根据我的理解,因为布局文件是通过片段“fragment”膨胀的,所以处理膨胀的layout.xml的方法应该在片段中。有人能给我一个很好的例子,说明处理当前片段布局的方法的良好实现。 下面是我的片段: package com.example.tabswithswi
下面是我的片段:
package com.example.tabswithswipe;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class binaryToDecimalFragment extends Fragment {
/////initialize text views////
TextView binaryInputTextView;
TextView binaryTitleTextView;
TextView convertTextView;
TextView result2TextView;
/////initialize buttons////
Button one2Button;
Button zero2Button;
Button clear2Button;
Button bck2Button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
updateBinaryInput();
}
///my method////
public void updateBinaryInput(){
//should change the text view of the binary input
//binaryInputTextView.setText(binaryInputString);
binaryInputTextView.setText("sometext");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.binarytodecimal, container, false);
////link ui objects///
binaryTitleTextView= (TextView) rootView.findViewById(R.id.binaryTitleTextView);
convertTextView= (TextView)rootView.findViewById(R.id.converterTitleTextView);
binaryInputTextView=(TextView)rootView.findViewById(R.id.binaryInputTextView);
result2TextView=(TextView)rootView.findViewById(R.id.result2TextView);
////initialize buttons////
one2Button=(Button)rootView.findViewById(R.id.one2Button);
zero2Button=(Button)rootView.findViewById(R.id.zero2Button);
clear2Button=(Button)rootView.findViewById(R.id.clear2Button);
bck2Button=(Button)rootView.findViewById(R.id.bck2Button);
return rootView;
}
}
你必须理解这一点。在
onCreateView()之前调用onCreate()
。如果在onCreate()中调用updateBinaryInput()
方法,则要更新的TextView不会初始化。我想你会因此得到一个NullPointerException
初始化TextView后,在onCreateView中调用您的方法
大概是这样的:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
///my method////
public void updateBinaryInput(){
//should change the text view of the binary input
//binaryInputTextView.setText(binaryInputString);
binaryInputTextView.setText("sometext");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.binarytodecimal, container, false);
////link ui objects///
binaryTitleTextView= (TextView) rootView.findViewById(R.id.binaryTitleTextView);
convertTextView= (TextView)rootView.findViewById(R.id.converterTitleTextView);
binaryInputTextView=(TextView)rootView.findViewById(R.id.binaryInputTextView);
result2TextView=(TextView)rootView.findViewById(R.id.result2TextView);
////initialize buttons////
one2Button=(Button)rootView.findViewById(R.id.one2Button);
zero2Button=(Button)rootView.findViewById(R.id.zero2Button);
clear2Button=(Button)rootView.findViewById(R.id.clear2Button);
bck2Button=(Button)rootView.findViewById(R.id.bck2Button);
updateBinaryInput(); // invoke methode after init the Views
return rootView;
}
谢谢,我应该知道的。至少你现在知道了。