I can't figure out where error is occurring. Here is the stack trace:
08-16 18:00:36.455: E/AndroidRuntime(22834): FATAL EXCEPTION: main
08-16 18:00:36.455: E/AndroidRuntime(22834): Process: org.example.calcu, PID: 22834
08-16 18:00:36.455: E/AndroidRuntime(22834): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.calcu/org.example.calcu.Menu}: java.lang.NullPointerException
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.app.ActivityThread.access$800(ActivityThread.java:144)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.os.Handler.dispatchMessage(Handler.java:102)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.os.Looper.loop(Looper.java:136)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.app.ActivityThread.main(ActivityThread.java:5158)
08-16 18:00:36.455: E/AndroidRuntime(22834): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 18:00:36.455: E/AndroidRuntime(22834): at java.lang.reflect.Method.invoke(Method.java:515)
08-16 18:00:36.455: E/AndroidRuntime(22834): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
08-16 18:00:36.455: E/AndroidRuntime(22834): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
08-16 18:00:36.455: E/AndroidRuntime(22834): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
08-16 18:00:36.455: E/AndroidRuntime(22834): at dalvik.system.NativeStart.main(Native Method)
08-16 18:00:36.455: E/AndroidRuntime(22834): Caused by: java.lang.NullPointerException
08-16 18:00:36.455: E/AndroidRuntime(22834): at org.example.calcu.Menu.onCreate(Menu.java:79)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.app.Activity.performCreate(Activity.java:6084)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
08-16 18:00:36.455: E/AndroidRuntime(22834): ... 12 more
This is where I try to start activity in my MainActivity:
empty
is Button (fairly long and empty - no text in it). I swipe my finger left to create new intent - Menu
empty = (Button) findViewById(R.id.empty);
empty.setOnTouchListener(new OnSwipeTouchListener(this){
@Override
public void onSwipeLeft() {
Intent ent;
ent = new Intent(MainActivity.this, Menu.class);
startActivity(ent);
}
});
This is activity that I try to create, Menu:
package org.example.calcu;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class Menu extends MainActivity{
Button blueTheme;
View menu;
class OnSwipeTouchListener implements OnTouchListener{
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("LOG", "menu opened!");
setContentView(R.layout.menu);
menu = (View) findViewById(R.layout.menu);
menu.setOnTouchListener(new OnSwipeTouchListener(this){ /* this is line 79 */
@Override
public void onSwipeRight(){
setContentView(R.layout.activity_main);
}
});
blueTheme = (Button) findViewById(R.id.blueTheme);
blueTheme.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
one.setBackgroundColor(Color.parseColor("#90CAF9"));
}
});
}
}
Thanks for your help, and sorry I am pretty beginner in programming.
menu = (View) findViewById(R.layout.menu);
findViewById
takes an id as parameter not a layout.
The could should be:
menu = (View) findViewById(R.id.menu);
but, of course you also should have a view
with id menu
in your xml layout
.
To finish your activity and get back to the previous one:
menu.setOnTouchListener(new OnSwipeTouchListener(this){
@Override public void onSwipeRight()
{
Menu.this.finish();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.