Home > Mobile >  Null pointer exception fragments getView ()
Null pointer exception fragments getView ()

Time:10-11

Activity invokes the fragments of controls and change its properties,
Main activity layout is as follows:
 & lt; ? The XML version="1.0" encoding="utf-8"?> 
XMLNS: app="http://schemas.android.com/apk/res-auto"
XMLNS: tools="http://schemas.android.com/tools"
Android: layout_width="match_parent"
Android: layout_height="match_parent"
Tools: context="MainActivity" & gt;

Android: layout_width="match_parent"
Android: layout_height="match_parent"
Android: orientation="vertical"
The android: id="@ + id/layout_root & gt;"

<frameLayout
Android: layout_width="match_parent"
Android: layout_height="match_parent"
The android: id="@ + id/frame_layout"
Android: layout_weight="1"/& gt;

Android: layout_width="match_parent"
Android: layout_height="wrap_content"
The android: id="@ + id/button_change"
Android: text="change"/& gt;




MainActivity. Java is as follows:
 import androidx. Appcompat. App. AppCompatActivity; 
The import androidx. Fragments. App. Fragments;
The import androidx. Fragments. App. FragmentManager;
The import androidx. Fragments. App. FragmentTransaction;


The import android. OS. Bundle;
The import android. View. The view;
The import android. Widget. The Button;
The import android. Widget. TextView;

Public class MainActivity extends AppCompatActivity {
Private fragments fragments;
Private Button buttonChange;

Private void initFragment (Bundle savedInstanceState) {
If (savedInstanceState==null) {
Fragments=new DemoFragment ();
FragmentManager FragmentManager=getSupportFragmentManager ();
FragmentTransaction FragmentTransaction=fragmentManager. BeginTransaction ();
FragmentTransaction. Replace (R.i d.f rame_layout, fragments);
//fragmentTransaction. The add (). The commit ();
FragmentTransaction.com MIT ();
}
}

Private TextView getFragmentTextView () {
TextView TextView=null;
GetSupportFragmentManager (.) executePendingTransactions ();
TextView=getSupportFragmentManager (.) findFragmentByTag (" DemoFragment "). The getView (). The findViewById (R.i which ext_view);
GetSupportFragmentManager (.) executePendingTransactions ();
Return the textView;
}

@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

InitFragment (savedInstanceState);

ButtonChange=(Button) the findViewById (R.i db utton_change);

ButtonChange. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View v) {
GetFragmentTextView (). The setText (" Content End. ");
}
});

}

}


Debris layout is as follows:
 & lt; ? The XML version="1.0" encoding="utf-8"?> 
Android: orientation="vertical"
Android: layout_width="match_parent"
Android: layout_height="match_parent" & gt;

Android: layout_width="match_parent"
Android: layout_height="wrap_content"
The android: id="@ + id/text_view"
Android: text="Test Start."
Android: gravity="center"/& gt;


DemoFragment. Java is as follows:
 import android. OS. Bundle; 

The import androidx. Fragments. App. Fragments;

The import android. View. LayoutInflater;
The import android. View. The view;
The import android. View. ViewGroup;
The import android. Widget. TextView;

/* *
* A simple {@ link fragments} ttf_subclass.
*/
Public class DemoFragment extends fragments {
Private TextView TextView;

Public DemoFragment () {
//Required empty public constructor
}

@ Override
Public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
The View demoView=inflater. Inflate (R.l ayout. Fragment_demo, container, false);
DemoFragment demoView. SetTag (" ");

TextView=(textView demoView). The findViewById (R.i which ext_view);

Return demoView;
}

}


Click on the button after an error is as follows:
E/AndroidRuntime: FATAL EXCEPTION: the main
Process: com. Sg. Fragmentsupportdemo1, PID: 4575
Java. Lang. NullPointerException: Attempt to invoke virtual method 'android. View. The view androidx. Fragments. The app. The fragments. The getView ()' on a null object reference
At com. Sg. Fragmentsupportdemo1. MainActivity. GetFragmentTextView (MainActivity. Java: 32)
At com. Sg. Fragmentsupportdemo1. MainActivity. Access the $000 (MainActivity. Java: 14)
At com. Sg. Fragmentsupportdemo1. MainActivity $1. The onClick (49) MainActivity. Java:


nullnull
  • Related