Button gives IllegalStateException when user click on it - java

When I start the APK on my phone, everything works so far. Only when the game is over, and I start the game from scratch, comes in the Logcat always an error message and the app ended.
This code is linked via onClick button, so I think there should be somewhere the error somewhere
public void playAgain(View view) {
Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
playAgainButton.setVisibility(View.INVISIBLE);
winnerTextView.setVisibility(View.INVISIBLE);
android.widget.GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
for(int i=0; i<gridLayout.getChildCount(); i++) {
ImageView counter = (ImageView) gridLayout.getChildAt(i);
counter.setImageDrawable(null);
}
for (int i=0; i<gameState.length; i++) {
gameState[i] = 2;
}
activePlayer = 0;
gameActive = true;
}
Instead of the app starting over, it closes and this error message comes:
I/Tag: 6
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: v.example.a3gewinn, PID: 9070
java.lang.IllegalStateException: Could not execute method for
android:onClick
at
androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(A
ppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6615)
at android.view.View.performClickInternal(View.java:6587)
at android.view.View.access$3500(View.java:784)
at android.view.View$PerformClick.run(View.java:26047)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6854)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:4
93)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6615) 
at android.view.View.performClickInternal(View.java:6587) 
at android.view.View.access$3500(View.java:784) 
at android.view.View$PerformClick.run(View.java:26047) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6854)
 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)'
on a null object reference
at v.example.a3gewinn.activity_game.dropIn(activity_game.java:76)
at java.lang.reflect.Method.invoke(Native Method) 
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:6615) 
at android.view.View.performClickInternal(View.java:6587) 
at android.view.View.access$3500(View.java:784) 
at android.view.View$PerformClick.run(View.java:26047) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6854) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860) 
I/Process: Sending signal. PID: 9070 SIG: 9
Disconnected from the target VM, address: 'localhost:8600', transport:
'socket'

Usually, IllegalStateException is used to indicate that "a method has been invoked at an illegal or inappropriate time
In other words, the Java environment or Java application is not in an appropriate state for the requested operation

Related

I created an android wear app, the code compiles correctly but in the emulator it keeps stopping, what's causing it?

Here is the java, it lists no errors when running, I am trying to make the text change every 5 seconds, however app keeps closing and wants me to reopen it.
package cross.and.sword.ptsdp;
import android.os.Bundle;
import android.support.wearable.activity.WearableActivity;
import android.widget.TextView;
public class MainActivity extends WearableActivity {
private TextView mTextView= (TextView) findViewById(R.id.WelText);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text);
// Enables Always-on
setAmbientEnabled();
final TextView textView=(TextView) findViewById(R.id.WelText);
Thread t = new Thread(){
#Override
public void run(){
boolean processing = true;
int number_processed=0;
do {
mTextView.setText("Text 1");
try {
Thread.sleep(500);
if (++number_processed>6)
processing=false;
} catch (InterruptedException e) {
e.printStackTrace( );
} finally {
mTextView.setText("Text 2");
}
} while(processing);
mTextView.setText("Text3");
}
};
};
}
11/28 18:47:36: Launching MainActivity
$ adb install-multiple -r -t C:\Users\Howard\AndroidStudioProjects\PTSDP\app\build\intermediates\split-apk\debug\slices\slice_1.apk C:\Users\Howard\AndroidStudioProjects\PTSDP\app\build\intermediates\split-apk\debug\slices\slice_7.apk C:\Users\Howard\AndroidStudioProjects\PTSDP\app\build\intermediates\split-apk\debug\dep\dependencies.apk C:\Users\Howard\AndroidStudioProjects\PTSDP\app\build\intermediates\split-apk\debug\slices\slice_6.apk C:\Users\Howard\AndroidStudioProjects\PTSDP\app\build\intermediates\resources\instant-run\debug\resources-debug.apk C:\Users\Howard\AndroidStudioProjects\PTSDP\app\build\intermediates\split-apk\debug\slices\slice_5.apk C:\Users\Howard\AndroidStudioProjects\PTSDP\app\build\intermediates\split-apk\debug\slices\slice_0.apk C:\Users\Howard\AndroidStudioProjects\PTSDP\app\build\intermediates\split-apk\debug\slices\slice_3.apk C:\Users\Howard\AndroidStudioProjects\PTSDP\app\build\intermediates\split-apk\debug\slices\slice_2.apk C:\Users\Howard\AndroidStudioProjects\PTSDP\app\build\intermediates\split-apk\debug\slices\slice_9.apk C:\Users\Howard\AndroidStudioProjects\PTSDP\app\build\intermediates\split-apk\debug\slices\slice_4.apk C:\Users\Howard\AndroidStudioProjects\PTSDP\app\build\intermediates\split-apk\debug\slices\slice_8.apk C:\Users\Howard\AndroidStudioProjects\PTSDP\app\build\intermediates\instant-run-apk\debug\app-debug.apk
Split APKs installed in 10 s 353 ms
$ adb shell am start -n "cross.and.sword.ptsdp/cross.and.sword.ptsdp.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Waiting for process to come online
Connected to process 2497 on device Android_Wear_Round_API_26 [emulator-5554]
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/zygote: Not late-enabling -Xcheck:jni (already on)
W/zygote: Unexpected CPU variant for X86 using defaults: x86
I/InstantRun: starting instant run server: is main process
D/AndroidRuntime: Shutting down VM
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: cross.and.sword.ptsdp, PID: 2497
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{cross.and.sword.ptsdp/cross.and.sword.ptsdp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2718)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2563)
at cross.and.sword.ptsdp.MainActivity.(MainActivity.java:9)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1173)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2708)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

Android App crashes when I call onDismissListner

I am trying to update text after a Dialog is dismissed. I am using an OnDismissListner, because the code in my onTextClicked runs only when the textView is clicked, and not when the dialog is dismissed.
public class AddReminder extends AppCompatActivity
{
}
#Override
protected void onCreate(Bundle savedInstanceState) {
//oncreate code
}
public void onDateClick(View view) {
loadMap();
DialogFragment datefragment = new DatePickerFragment();
datefragment.show(getFragmentManager(), "DatePicker");
Dialog dialog = datefragment.getDialog();
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
updateText();
}
});
}
public void updateText(){
TextView date = findViewById(R.id.tv_date);
String text =AddReminder.monthmap.get(DatePickerFragment.month)+" "+DatePickerFragment.day+", "+DatePickerFragment.year;
date.setText(text);
TextView time = findViewById(R.id.tv_time);
String tvdate = TimePickerFragment.hour + ":" + TimePickerFragment.min;
time.setText(tvdate);
}
When I to click the textView with the onDateClicked methodcode, the app crashes with the error:
5-21 21:40:06.121 19350-19350/com.zbot473gmail.reminders E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.zbot473gmail.reminders, PID: 19350
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6256) 
at android.view.View$PerformClick.run(View.java:24701) 
at android.os.Handler.handleCallback(Handler.java:789) 
at android.os.Handler.dispatchMessage(Handler.java:98) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Dialog.setOnDismissListener(android.content.DialogInterface$OnDismissListener)' on a null object reference
at com.zbot473gmail.reminders.AddReminder.onDateClick(AddReminder.java:80)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:6256) 
at android.view.View$PerformClick.run(View.java:24701) 
at android.os.Handler.handleCallback(Handler.java:789) 
at android.os.Handler.dispatchMessage(Handler.java:98) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 
Is there a better way to run code when the dialog is dismissed? I have tried this, but I really don't understand how to solve it.

Swiping Down crashes app

So I'm using the library Swipe by Pwittchen and the code being used is this section:
CoreActivity.java
#Override
public void onSwipedDown(final MotionEvent event) {
if(m_Toolbar.getVisibility() == View.VISIBLE ){
WebShrink.cancel();
}
//WebView Animation (Shrink)
// if(m_WebView.getAnimation().hasStarted() || m_WebView.getAnimation().hasEnded()){
if(WebEnlarge.hasStarted()){
WebShrink = AnimationUtils.loadAnimation(CoreActivity.this,R.anim.m_webview_shrink);
m_WebView.startAnimation(WebShrink);
m_Preference_Toolbar.setVisibility(View.GONE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
m_Toolbar.setVisibility(View.VISIBLE);
}
}, 350);//3 seconds
}
}
Currently I crashes if you swipe down in the moment it is created. Im trying to find a way around it with:
if(m_Toolbar.getVisibility() == View.VISIBLE ){
WebShrink.cancel();
}
but this is not working and am wondering if anyone could help me with this. the ideal situation is that if the toolbar is visible or if WebShrink is already in place then do nothing but I can't find away around it
LogCat
01-18 20:57:23.652 29764-29764/com.equiware.mickeyt.fire E/InputEventReceiver: Exception dispatching input event.
01-18 20:57:23.653 29764-29764/com.equiware.mickeyt.fire D/AndroidRuntime: Shutting down VM
01-18 20:57:23.654 29764-29764/com.equiware.mickeyt.fire E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.equiware.mickeyt.fire, PID: 29764
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.animation.Animation.cancel()' on a null object reference
at com.equiware.mickeyt.fire.CoreActivity$2.onSwipedDown(CoreActivity.java:161)
at com.github.pwittchen.swipe.library.rx2.Swipe.onActionUp(Swipe.java:138)
at com.github.pwittchen.swipe.library.rx2.Swipe.dispatchTouchEvent(Swipe.java:101)
at com.equiware.mickeyt.fire.CoreActivity.dispatchTouchEvent(CoreActivity.java:216)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:374)
at android.view.View.dispatchPointerEvent(View.java:10177)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4628)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4496)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3947)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4000)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3966)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3974)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3947)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4000)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3966)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4095)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3974)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4152)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3947)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4000)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3966)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3974)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3947)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6437)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6411)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6372)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6571)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6519)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6594)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:613)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.animation.Animation.cancel()' on a null object reference at com.equiware.mickeyt.fire.CoreActivity$2.onSwipedDown(CoreActivity.java:161)
Line 161 seems to be:
WebShrink.cancel();
WebShrink is probably null.
You should load the animation outside onSwipeDown method, maybe you can place this in the activity's onCreate:
WebShrink = AnimationUtils.loadAnimation(CoreActivity.this,R.anim.m_webview_shrink);
Then it will be instantiated before onSwipeDown is called.

Android Studio, Google Maps Search Button is not working

i worte an Android Application with google Maps integration.
I found a tutorial in youtube and did it in that way like in the video to search a location with button click.
I have just simple plaintext and a button for search. The Button is set to the onclick method onSearch() you can find code bellow.
When i press on that button, the app is crashing. What is wrong in the Code??
Thank you in advance:
public void onSearch(View v){
//Textfield in google maps activity
EditText location_tf=(EditText) findViewById(R.id.TFadress);
//Put the written input into a string to work with
String location = location_tf.getText().toString();
//List for Geocoder
List<Address> addressList= null;
if(location!= null || !location.equals("")){
Geocoder geocoder = new Geocoder(this);
try {
addressList=geocoder.getFromLocationName(location,1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng= new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Searched Location"));
//MovetheCamera to the searched location with 18 zoom
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
}
}
Error Log:
08-18 13:25:36.182 2619-2619/com.group6.travlhoe E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.group6.travlhoe, PID: 2619
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:5336)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5331)
at android.view.View.performClick(View.java:6256) 
at android.view.View$PerformClick.run(View.java:24697) 
at android.os.Handler.handleCallback(Handler.java:789) 
at android.os.Handler.dispatchMessage(Handler.java:98) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at com.group6.TakeOff.MapsActivity.onMapSearch(MapsActivity.java:84)
at java.lang.reflect.Method.invoke(Native Method) 
at android.view.View$DeclaredOnClickListener.onClick(View.java:5331) 
at android.view.View.performClick(View.java:6256) 
at android.view.View$PerformClick.run(View.java:24697) 
at android.os.Handler.handleCallback(Handler.java:789) 
at android.os.Handler.dispatchMessage(Handler.java:98) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

NullPointerException (GameActivity does not open)

Please check whats the problem with my code here :
Main Activity :
I'm using Android Studio. Import statements are managed for me. It won't be necessary to write them here
public class MainActivity extends Activity implements View.OnClickListener {
SharedPreferences prefs;
String dataName = "MyData";
String intName = "MyString";
int defaultInt = 0;
public static int highScore;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonPlay = (Button) findViewById(R.id.btnPlay);
TextView textHighScore = (TextView) findViewById(R.id.textHighScore);
buttonPlay.setOnClickListener(this);
prefs = getSharedPreferences(dataName, MODE_PRIVATE);
highScore = prefs.getInt(intName, defaultInt);
textHighScore.setText("High Score: " + highScore);
}
#Override
public void onClick(View view) {
Intent intent;
intent = new Intent(this, GameActivity.class);
startActivity(intent);
}
}
When I run this application Main Activity is loaded but when I hit play button application closes. I have checked the play button's ID is O.K. I had the same problem in GameActivity but at last I figured out that the problem was with the ID but this time I think problem is with the this I used in Intent();. Activity fails to load.
Exception :
06-24 01:34:05.196 12826-12826/com.cruzerblade.memorygame E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.cruzerblade.memorygame, PID: 12826
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.cruzerblade.memorygame/com.cruzerblade.memorygame.GameActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:87)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
at android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:75)
at com.cruzerblade.memorygame.GameActivity.<init>(GameActivity.java:55)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Please help me out! It took me too much time still I couldn't figure out the problem.
Quoting from your logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources android.content.Context.getResources()'
on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:87)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
at android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:75)
at com.cruzerblade.memorygame.GameActivity.<init>(GameActivity.java:55)
This last line is the only line in the trace that is your code:
Look on line 55 of GameActivity.java. It is calling loadAnimation. There is something wrong with the animation it is trying to load (probably a missing resource ID, but it could also be a null or invalid context argument for loadAnimation.)

Categories

Resources