Id 是正确的,但是 - java.lang.NullPointerException: android.widget.TextView.setText(java.lang.CharSequence)



我在使用setText((的NPE时遇到了这么长时间的问题。
Tab1Fragment中的一些状态,我可以通过以下代码看到:

  • 文本视图为空。
  • 全文得到了一个理想的值。

我错过了什么积分吗?我唯一的意图是在单击时从HomeFragment中的列表视图更新Tab1Fragment中的TextView。(好像这远远领先于我(
提前谢谢。

首页片段.java

public class HomeListAdapter extends BaseAdapter {
AddLawMap addLawMap = new AddLawMap();
private final ArrayList<String> list;
private Tab1Fragment tab1Fragment = new Tab1Fragment();
private Tab2Fragment tab2Fragment = new Tab2Fragment();
private Tab3Fragment tab3Fragment = new Tab3Fragment();
private GetTextFragment getTextFragment = new GetTextFragment();
...
@Override
public View getView(int position, View convertView, ViewGroup parent){
final View result;
if(convertView == null){
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_item, parent, false);
}else{
result = convertView;
}
if(getCount() != 0) {
String itemId = getItem(position);
final TextView tv1 = result.findViewById(R.id.text_home_item);
tv1.setTag(itemId);
tv1.setText(addLawMap.getMapString().get(itemId));
result.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
final String tag = tv1.getTag().toString();
AlertDialog.Builder builder2 = new AlertDialog.Builder(mcontext);
builder2.setTitle(R.string.choose_fragment)
.setItems(R.array.choose_fragment, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(mcontext, R.string.after_set_fulltext_toast, duration);
switch (which) {
case 0:
tab1Fragment.setFullText(getTextFragment.getFileStream(tag));
tab1Fragment.updateUi();
toast.show();
break;
...
}
}
});
AlertDialog dialog2 = builder2.create();
dialog2.show();
}
});
return result;
}else {
return result;
}
}
}

标签1片段.java

public class Tab1Fragment extends Fragment {
private static final String ARG_COUNT = "param1";
private Integer counter;
private Handler mHandler = new Handler(Looper.getMainLooper());
private String fullText;
private int numSetText;
private int numTab1;
@NonNull TextView textView;

@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.i("TAB1_FRAGMENT", "/onAttach");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
counter = getArguments().getInt(ARG_COUNT);
}
numTab1 = 0;
numSetText = 0;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container, Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
Log.i("TAB1_FRAGMENT", "/onCreateView");
return inflater.inflate(R.layout.fragment_tab1, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
TextView textView = (TextView) getView().findViewById(R.id.tab1_textview);
if(fullText == null){
ImageView imageView = (ImageView) getView().findViewById(R.id.none_added);
imageView.setVisibility(View.VISIBLE);
textView.setVisibility(View.INVISIBLE);
}else{
textView.setText(fullText);
}
Log.i("TAB1_FRAGMENT", "/onViewCreated");
}
public void setFullText(String fullText){
this.fullText = fullText;
Log.i("TAB1_FRAGMENT", "/setFullText()");
++ numSetText;
}
public void updateUi(){
if(fullText != null){
mHandler.post(new Runnable() {
@Override
public void run() {
textView.setText(fullText);
++numTab1;
}
});
}
}

@Override
public void onPause() {
super.onPause();
Log.i("TAB1_FRAGMENT", "/onPause");
}
@Override
public void onStart(){
super.onStart();
Log.i("TAB1_FRAGMENT", "/onStart");
}
@Override
public void onResume(){
super.onResume();
Log.i("TAB1_FRAGMENT", "/onResume");
}
@Override
public void onDetach(){
super.onDetach();
mHandler = null;
}

fragment_tab1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/none_added"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/tab_none_added"
android:visibility="gone"/>
<TextView
android:id="@+id/tab1_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingExtra="3dp"/>
</LinearLayout>

日志猫

2020-06-08 22:52:25.855 9668-9668/com.example.morina.mylaw I/TAB1_FRAGMENT: /setFullText()
2020-06-08 22:52:25.881 9668-9668/com.example.morina.mylaw W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
2020-06-08 22:52:25.882 9668-9668/com.example.morina.mylaw E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.morina.mylaw, PID: 9668
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.morina.mylaw.Tab1Fragment$1.run(Tab1Fragment.java:82)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

在HomeFragment中.java你有R.id.text_home而不是R.id.tab1_textview。只要确保您的身份证正确即可。

您只是实例新Tab1Fragment但不将其呈现到屏幕上。使用fragment.isInLayout()处理其视图时,请确保片段在布局中。

相关内容

  • 没有找到相关文章

最新更新