我找不出錯誤發生的位置。這裏是堆棧跟蹤:NullPointerException。無法創建活動
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
這是我嘗試在我的MainActivity開始活動:
empty
是按鈕(相當長和空 - 它沒有內容)。我刷我的手指留下來創建新的意圖 - 菜單
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);
}
});
這是活動的,我嘗試創建,菜單:
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"));
}
});
}
}
感謝您的幫助,對不起,我在編程非常初學者。
添加XML佈局 – hasan83
只是curious-你爲什麼不使用抽屜式導航欄顯示,而不是在一個整體的其他顯示菜單活動? – DavidH