无法从主Activity(带接口)更改Fragment中的TextView,

时间:2016-07-09 12:29:15

标签: android interface nullpointerexception textview fragment

虽然我已经阅读了类似的后期问题,但我还没有找到解决问题的方法。正如您在标题中看到的,我无法在TextFragment中更改Textview。 我收到此错误:07-09 15:05:55.593 6441-6441 / com.eguide.dimitris.campusguide E / AndroidRuntime:FATAL EXCEPTION:main 07-09 15:05:55.593 6441-6441 / com.eguide.dimitris.campusguide E / AndroidRuntime: java.lang.NullPointerException 07-09 15:05:55.593 6441-6441 / com.eguide.dimitris.campusguide E / AndroidRuntime:at com.eguide.dimitris.campusguide.mainActivity $ 1.onVisitStart(mainActivity.java:66)

所以我会感激任何帮助。 提前致谢。

  

mainActivity

.book-info span {
   float: left; 
}
  

textFragment

public class mainActivity  extends Activity implements inter {
public PlaceManager placeManager;
public PlaceEventListener placeEventListener;
public String name;
TextView tt;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    Gimbal.setApiKey(this.getApplication(), "5436462c-6400-48f4-b6af-e0ad2094550f");
    CommunicationManager.getInstance().startReceivingCommunications();
    tt=(TextView) findViewById(R.id.test);
}



@Override
public void respond(String data) {
    FragmentManager manager2=getFragmentManager();
    textFragment frag2= (textFragment) manager2.findFragmentById(R.id.text_fragment);
    frag2.changeText(data);
}

@Override
public void barstatus() {
    FragmentManager manager1=getFragmentManager();
    imageFragment frag1=(imageFragment) manager1.findFragmentById(R.id.image_fragment);
    frag1.changeStatus();
}

@Override
public void barstatus2() {
    FragmentManager manager1=getFragmentManager();
    imageFragment frag1=(imageFragment) manager1.findFragmentById(R.id.image_fragment);
    frag1.changeStatus2();
}


public void changestatus(){
    placeEventListener= new PlaceEventListener() {
        @Override
        public void onVisitStart(Visit visit) {
            super.onVisitStart(visit);
            name=visit.getPlace().getName().toString();
            if (name!=null){
                respond(name);
                barstatus2();
            }
        }
    };
  

imageFragment

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class textFragment extends Fragment {

TextView text1;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.text_fragment, container,false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    text1 = (TextView)getActivity().findViewById(R.id.textF);
    text1.setText("Press Start button to find buildings near you");

}
public void changeText(String data){
    text1 = (TextView)getActivity().findViewById(R.id.textF);
    text1.setText(data.toString());

}

}
  

接口

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.transition.Visibility;
import android.view.ActionProvider;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;



public class imageFragment extends Fragment {

private ProgressBar bar;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.image_fragment,container,false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    bar=(ProgressBar) getActivity().findViewById(R.id.progressBar);
    bar.setVisibility(View.INVISIBLE);

}

public void changeStatus(){
    bar=(ProgressBar) getActivity().findViewById(R.id.progressBar);
        bar.setVisibility(View.VISIBLE);
    }

public void changeStatus2(){
    bar=(ProgressBar) getActivity().findViewById(R.id.progressBar);
    bar.setVisibility(View.INVISIBLE);
}
}

伙计们还注意到我在mainActivity中尝试了这个但没有改变。

public interface inter {
public  void respond(String data);

public void barstatus();

public void barstatus2();
}

编辑: 如果不是

inter inte;


public void changestatus(){
    placeEventListener= new PlaceEventListener() {
        @Override
        public void onVisitStart(Visit visit) {
            super.onVisitStart(visit);
            name=visit.getPlace().getName().toString();
            if (name!=null){
                inte.respond(name);
                inte.barstatus2();
            }
        }
    };

使用

public void changestatus(){
placeEventListener= new PlaceEventListener() {
    @Override
    public void onVisitStart(Visit visit) {
        super.onVisitStart(visit);
        name=visit.getPlace().getName().toString();
        if (name!=null){
            respond(name);
            barstatus2();
        }
    }

我可以做出改变......你能解释一下为什么吗? 我不希望这个代码块在onStart()上运行。 我希望这个代码块在一个方法中工作。

1 个答案:

答案 0 :(得分:-1)

您需要对textFragment进行少量更改,在TextView方法中初始化onCreateView()

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.text_fragment, container,false);

    text1 = (TextView)getActivity().findViewById(R.id.textF);
    text1.setText("Press Start button to find buildings near you");

    return view;
}