Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
An Activity
is not fully initialized and ready to look up views until after setContentView(...)
is called in onCreate()
.
Only declare the fields like the following:
private EditText usernameField, passwordField;
private TextView error;
private ProgressBar progress;
and then assign the values in onCreate
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameField = (EditText)findViewById(R.id.username);
passwordField = (EditText)findViewById(R.id.password);
error = (TextView)findViewById(R.id.error);
progress = (ProgressBar)findViewById(R.id.progress);
}
Might not be part of the problem but as an extra bit of advice a Timer
runs the TimerTask
on a background thread and that should be avoided in this case. Replace the Timer
with a Handler
instead to run it on the UI thread.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
}, 1500);
Error I cant Solve:Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
You have to assign the values of the view in OnCreate
after the setContentView
.
Declare the fields outside:
private TextView ;
private Button; etc..
Place the below lines in OnCreate
txtquestion = findViewById(R.id.txtquestion);
answer1 = findViewById(R.id.btnans1);
answer2 = findViewById(R.id.btnans2);
answer3 = findViewById(R.id.btnans3);
answer4 = findViewById(R.id.btnans4);
Hope it helps!
Error: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
You cannot have this initialization here:
LineChart lineChart = (LineChart) findViewById(R.id.chart);
You can declare your variable there like:
LineChart lineChart;
And then in onCreate
AFTER calling setContentView
put the initialization like:
lineChart = (LineChart) findViewById(R.id.chart);
Java Null Pointer Exception on 'android.view.Window$Callback android.view.Window.getCallback()'
I guess you are getting that error because you are instantiating a new MainActivity
object:
mMainActivity = new MainActivity();
In case of Fragments
, you can call getActivity()
to get the reference of the activity that is hosting your Fragment
. Something like:
private MainActivity mMainActivity;
....
mMainActivity = (MainActivity) getActivity();
Or, you don't even need to cast to MainActivity
since you are invoking a common method (available on Activity
)
private Activity mMainActivity;
....
mMainActivity = getActivity();
java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference, while its not null
You can't do this
public void info(){
CoatsJackets sendInfo = new CoatsJackets();
sendInfo.getInfo(name);
}
You must do someting like this
public void info(CoatsJackets sendInfo){
sendInfo.getInfo(name);
}
And in your activity do something like this
WeatherConditions recievingInfo = new WeatherConditions();
recievingInfo.info(CoatsJackets.this);
But you maybe will have a crash if the name of weather conditions is null at this line
text.setText(recievedInfo);
Attempt to invoke virtual method [Null object reference]
Just taking a look at your code, it looks like you are doing many things wrong. Such as this line
MainActivity a = new MainActivity();
You can't instantiate lifecycle components like Services, Activities, Fragments etc this way. Android provides you these components when you call start methods etc. The activity that is created as a result of the above line doesn't go through the lifecycle calls that an activity that is started via using startActivity. As a result, the setContentView of the activity is never called and you get this error. You have to remove this line and implement this properly and the error will go away.
Also just to add, you are actually creating a new instance of the activity when one is already running when you need to call the methods on the old instance that was started.
Edit: Try like this.
MainActivity activity;
List<mList> mData;
Dialog myDialog;
private MediaPlayer mediaPlayer;
private int playbackPosition=0;
private LinearLayout menu_dialog;
public RecyclerViewAdapter(MainActivity activity, List<mList> mData) {
this.activity = activity;
this.mData = mData;
}
And then
vHolder.menu_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
menu_dialog = v.getRootView().findViewById(R.id.menu_dialog);
menu_dialog.setVisibility(v.VISIBLE);
activity.initAudio(v.getContext(), mData.get(vHolder.getAdapterPosition()).getURL());
activity.setMargins(v.getRootView().findViewById(R.id.viewpager_id), 0,0,0,100);
}
});
Also remove static from the main activity functions
Related Topics
View'S Getwidth() and Getheight() Returns 0
Can't Create Directory in Android 10
Cannot Create or Edit Android Virtual Devices (Avd) from Eclipse, Adt 22.6
Limit Jvm Process Memory on Ubuntu
Non-Static Variable Cannot Be Referenced from a Static Context
How to Create a Java String from the Contents of a File
Integer Division: How to Produce a Double
How to Handle Infinite Loop Caused by Invalid Input (Inputmismatchexception) Using Scanner
How to Set Environment Variables from Java
When Is the Finalize() Method Called in Java
How to Get Url from Firebase Storage Getdownloadurl
How to Return a Boolean from Asynctask
Must Override a Superclass Method' Errors After Importing a Project into Eclipse
Why Maven Uses Jdk 1.6 But My Java -Version Is 1.7
Sort a Map≪Key, Value≫ by Values
What Is a Serialversionuid and Why Should I Use It
Element Myelement Is Not Clickable At Point (X, Y)... Other Element Would Receive the Click
Infinite Recursion With Jackson Json and Hibernate JPA Issue