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.
Related
I am working on an App that use camera for face recognition. But i have some problem when i resume my app the the app always crash.
This is my code Onresume Activity:
#Override
public synchronized void onResume() {
// LOGGER.d("onResume " + this);
super.onResume();;
handlerThread = new HandlerThread("inference");
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
}
This is my code in CameraActivity:
CameraActivity.java
And this is the class that i used for recognize the face that extends from cameraActivity:
Detector Activity
I forgot to show the erorr:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.samples.flironecamera, PID: 30923
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.samples.flironecamera.tracking.MultiBoxTracker.trackResults(java.util.List, long)' on a null object reference
at com.samples.flironecamera.DetectorActivity.updateResults(DetectorActivity.java:728)
at com.samples.flironecamera.DetectorActivity.onRestart(DetectorActivity.java:761)
at android.app.Instrumentation.callActivityOnRestart(Instrumentation.java:1443)
at android.app.Activity.performRestart(Activity.java:8067)
at android.app.ActivityThread.performRestartActivity(ActivityThread.java:5188)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:243)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2270)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8125)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
And Another Erorr
java.lang.RuntimeException: Unable to resume activity {com.samples.flironecamera/com.samples.flironecamera.DetectorActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.startPreview()' on a null object reference
In order to reset a user password in my app I decided to create a dialog for the user to enter their email so I could send them a mail to reset the password. The Dialog works and it looks like - that
The code behind creating the dialog:
public void CreateDialogResetPassword ()
{
d = new Dialog(this);
d.setContentView(R.layout.resetpassword_layout);
d.setCancelable(true);
btnResetPassword = (Button)findViewById(R.id.btnResetPassword);
etEmailResetPassword = (EditText)findViewById(R.id.etResetPasswordEmail);
d.setTitle("Reset Password");
d.show();
}
The thing is whenever I add the setOnClickListener to the btnResetPassword and then I click the Forgot Password imageButton the app crashes.
This is the code after adding the setOnClickListener method.
public void CreateDialogResetPassword ()
{
d = new Dialog(this);
d.setContentView(R.layout.resetpassword_layout);
d.setCancelable(true);
btnResetPassword = (Button)findViewById(R.id.btnResetPassword);
btnResetPassword.setOnClickListener(this);
etEmailResetPassword = (EditText)findViewById(R.id.etResetPasswordEmail);
d.setTitle("Reset Password");
d.show();
}
What happens after adding the setOnClickListener and then clicking on the Forgot Pasword imagebutton which should open the dialog is this basically crashing the app and going to home page.
And this is how the onClick looks like -
#Override
public void onClick(View v) {
if (v==btnLogin)
{
login();
}
if (v == btnSignUpMain) {
CreateDialogSignUp();
}
if (v == imageBtnSingUp) {
register();
}
if (v==imageBtnResetPassword)
{
CreateDialogResetPassword();
}
You can ignore most of the code in the onClick I don't know how much that matters but decided to add it anyway.
Logcat -
2021-05-06 17:18:15.429 9175-9175/com.example.recyclub I/AssistStructure: Flattened final assist data: 1992 bytes, containing 1 windows, 12 views
2021-05-06 17:18:18.295 9175-9175/com.example.recyclub D/AndroidRuntime: Shutting down VM
2021-05-06 17:18:18.296 9175-9175/com.example.recyclub E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.recyclub, PID: 9175
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.recyclub.MainActivity.CreateDialogResetPassword(MainActivity.java:143)
at com.example.recyclub.MainActivity.onClick(MainActivity.java:207)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2021-05-06 17:18:18.333 9175-9175/com.example.recyclub I/Process: Sending signal. PID: 9175 SIG: 9
Apparently the only problem was in this code where the Dialog is being created
Button btnSubmitResetPassword = (Button)findViewById(R.id.btnSubmitResetPassword);
Needed to get reference from the dialog itself and the correct code is this -
Button btnSubmitResetPassword = (Button)d.findViewById(R.id.btnSubmitResetPassword);
Now it is referenced in a proper way.
everytime I'm trying to start my app it crashes immediately on this part of code.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!isOnline()) {
// textViewTemperature.setText(Data);
Toast.makeText(getApplicationContext(), "Cannot connect to network, data will be restore from file with last downloaded data...", Toast.LENGTH_LONG).show();
} else {
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
weather = parser.getWeatherForLocation(editText.getText() + "");
runOnUiThread(new Runnable() {
#Override
public void run() {
editText.setText(weather.getCity() + ", " + weather.getCountry()); }
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
}
}
});
This is the full logcat from this error:
12-29 10:06:36.376 3524-3524/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.piotr.pogoda, PID: 3524
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.piotr.pogoda/com.example.piotr.pogoda.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.piotr.pogoda.MainActivity.onCreate(MainActivity.java:136)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
I can see the error but I don't get what does it mean that I'm trying to use a method on a null object reference. The button does not exist or what?
Initialize your button, either by findViewById(R.id.btn) or programatically button = new Button(getApplicationContext())
From your crash log, button view is null. May be your view initialization has some issues. Please check the code.
You need to initialize the button
Button btn = (Button) findViewById()
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.)
I have an activity with a viewpager containing 2 fragments, inside each fragment contains a list, from this list a button can be pressed that creates a new fragment in the viewpager, this works fine until I create a new activity and close it, after the new activity is closed and I return to the previous activity pressing the button to create a new fragment again now gives a classcastexception, almost as if the closed activity is still open, but it is not, I have tried searching this issue and cant find any examples of this being encountered by anyone else which makes me wonder how this could be happening, I will post some code examples below, any help will go a long way thanks.
Click listener in ListAdapter:
holder.rlStatusContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//add new comment fragment and set page
try {
((HomeFragmentActivity) activity).addCommentFragment(data.get(finalPosition));
} catch (Exception e) {
e.printStackTrace();
}
}
});
Method in activity:
public void addCommentFragment(ArrayList<Object> fragmentExtras) {
list.add(NavigationFragment.newInstance(9, getApplicationContext(), this, fragmentExtras));
adapter.notifyDataSetChanged();
pager.setCurrentItem(list.size());
}
Error received:
12-13 13:37:50.177 17262-17262/test.test.com.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: test.test.com.test, PID: 17262
java.lang.ClassCastException: test.test.com.test.activities.NewPostActivity cannot be cast to test.test.com.test.activities.HomeFragmentActivity
at test.test.com.test.adapters.ListHomeAdapter$4.onClick(ListHomeAdapter.java:462)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)