我有webview内容,它将发送需要在片段中更新的数据。我编写了一个javscript监听器类,我可以从webview获取值。但是每次在webview中发生更改时,我都无法更新这些更新的值。 片段与我的活动一起启动时显示初始默认值(包含片段和webview )。但是在应用程序启动之后,每当我尝试通过我的javscript监听器类更改fragment中的编辑字段时,它会抛出空指针异常。我甚至试图通过Main活动对象更新相同的内容,但仍然没有说出空指针异常。
主要Activty.java
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("Inside MainActivity onCreate...");
setContentView(R.layout.activity_main);
ui_controlFragment = (UIControlFragment)getFragmentManager().findFragmentById(R.id.ui_control_fragment);
}
if (ui_controlFragment != null) {//here the condition passes
System.out.println("ui_controlFragment Not NULL.................");
}
}
public void updateUI2(double lat,double lng){
System.out.println("Inside main activity updateUI2()");
ui_controlFragment = (UIControlFragment)getFragmentManager().findFragmentById(R.id.ui_control_fragment);
if (ui_controlFragment != null) {//here the condition fails. if i comment this condition it throws null pointer exception.
System.out.println("Inside updateUI2, ui_controlFragment is not null");
ui_controlFragment.updateUI(lat,lng);
}
}
}
UIControlFragment.java
public class UIControlFragment extends Fragment {
Activity mActivity;
View rootView;
EditText editLat;
EditText editLng;
public double latitude=35.0;
public double longitude=139.0;
@Override
public void onAttach (Activity activity){
super.onAttach(activity);
System.out.println("Inside onAttach UIControlFragment...");
mActivity = activity;
}
@Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
System.out.println("Inside onCreate UIControlFragment...");
editLat = new EditText(mActivity.getApplicationContext());
editLng = new EditText(mActivity.getApplicationContext());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle){
System.out.println("Inside onCreateView UIControlFragment");
LayoutInflater layoutInflater = mActivity.getLayoutInflater();
rootView = layoutInflater.inflate(R.layout.control_ui, viewGroup, false);
editLat = (EditText) rootView.findViewById(R.id.editLat);
editLat.setText("");
editLng = (EditText) rootView.findViewById(R.id.editLng);
editLng.setText("");
return rootView;
}
@Override
public void onResume(){
super.onResume();
System.out.println("Inside onResume UIControlFragment......");
updateUI(latitude,longitude);//here it works because values are set during initial fragment launching.
}
public void updateUI(double lat,double lng){
System.out.println("--------Inside updateUI UIControlFragment..lat: "+lat);
editLat.setText(String.valueOf(lat));
editLng.setText(String.valueOf(lng));
}
}
JSListener.java //这是webview调用javascript方法的地方。我是一个UpdateView()方法
public class JSListener{
UIControlFragment uiControlFragment;
MainActivity mainActivity = new MainActivity();
@JavascriptInterface
public void UpdateView(double lat,double lng){
System.out.println("lat:"+lat+", lng:"+lng);
try{
mainActivity.updateUI2(lat,lng); //here im calling the updateUI method written inside MainActivity.java
/*
I also tried the below code, but that too didnt work as it too gave null value for uicontrolfragment object.
uiControlFragment.updateUI(lat,lng);
*/
}catch(NullPointerException e){
e.printStackTrace();
}
}
}
我注意到的是一旦使用值启动的片段,则无法在两者之间进行更新。就像我的情况一样,每当webview发生变化时,我都会有lat,lng值。如何在首次启动后更新片段中的编辑字段。
答案 0 :(得分:0)
好的,我能够解决这个问题。 问题是我有多个UICOntrol&的实例。 MainActivity类。 我维护了一个相同的对象,并将其用于所有未来的UI编辑字段更改。 此外,我遇到了一个错误,像UI应该只通过UI线程更改。我通过在UI线程中执行UI更改来解决相同的问题,如下所示:
getMainActivityObj().runOnUiThread(new Runnable() {
@Override
public void run() {
uiControlFragment.editLat.setText(String.valueOf(latitude));
uiControlFragment.editLng.setText(String.valueOf(longitude));
}
});