Android 导航抽屉活动、调用片段、空对象引用
我哪里出错了?我试图在Communicate_fragment中设置editText,并将其传递给receive_fragment,并在textView中显示它 如果我不使用导航抽屉活动,这不会是一个问题,因为我可以在MainActivity中绘制片段,这是我现在无法做到的,但我正在尝试学习如何以这种方式实现它 请给我指出正确的方向,因为我是初学者,正在努力学习手工艺 AndroidManager说:Android 导航抽屉活动、调用片段、空对象引用,android,android-fragments,navigation-drawer,Android,Android Fragments,Navigation Drawer,我哪里出错了?我试图在Communicate_fragment中设置editText,并将其传递给receive_fragment,并在textView中显示它 如果我不使用导航抽屉活动,这不会是一个问题,因为我可以在MainActivity中绘制片段,这是我现在无法做到的,但我正在尝试学习如何以这种方式实现它 请给我指出正确的方向,因为我是初学者,正在努力学习手工艺 AndroidManager说: Attempt to invoke virtual method 'void fragment
Attempt to invoke virtual method 'void fragmentdrawer.recieve_fragment.setText(java.lang.String)' on a null object reference
但我不明白为什么
主要内容:
通信\u碎片类:
package fragmentdrawer;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.example.name.fragmentdrawer.R;
public class communicate_fragment extends Fragment{
private static EditText topTextInput;
private static Button button;
TopSectionListener activityCommander;
public interface TopSectionListener{
public void createMeme(String text);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
activityCommander = (TopSectionListener) activity;
}catch (ClassCastException e) {
throw new ClassCastException(activity.toString());
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_communicate, container, false);
topTextInput = (EditText) rootView.findViewById(R.id.message);
final Button button = (Button) rootView.findViewById(R.id.buttonSend);
button.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v){
buttonClicked(v);
}
}
);
return rootView;
}
public void buttonClicked(View view) {
activityCommander.createMeme(topTextInput.getText().toString());
}
}
接收类帧
package fragmentdrawer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.name.fragmentdrawer.R;
public class recieve_fragment extends Fragment{
private static TextView textView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_recieve, container, false);
textView = (TextView) rootView.findViewById(R.id.displayMsg);
return rootView;
}
public void setText(String text){
textView.setText(text);
}
}
receive_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/displayMsg"
android:layout_gravity="center" />
</FrameLayout>
您可以发布receive\u fragment类的xml文件吗?可能textview的id不正确,因此findViewById()无法找到它,从而导致空值。@NarayanAcharya我现在已经添加了它,谢谢!可以现在能用了吗?不,同样的问题@NarayanAcharya
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/displayMsg"
android:layout_gravity="center" />
</FrameLayout>