2012-11-08 96 views
0

我爲表面view.there創建了一個TapGestureDetector類,我發現了長觸摸事件,我想開始一個新的activity.But它不工作。意圖沒有啓動。有一個空指針異常。我已經明確了清單中的行爲以及意圖。但有一個我無法識別的錯誤。新的意圖不會從onLongPress開始?

public class TapGestureDetector extends GestureDetector.SimpleOnGestureListener { 

int touchX; 
int touchY; 
public static String block_name; 
Context ctx = custom.ctx; 
ArrayList<Rect> rectangles = storeMap.GetArrayList(); 


@Override 
public void onLongPress(MotionEvent event) { 

//Detect touched X.Y 
    touchX = (int) event.getX(); 
    touchY = (int) event.getY(); 
//Go through a set of rectangles and identify the touched rectangle 
    for (int i = 0; i < rectangles.size(); i++) { 

     if (rectangles.get(i).contains(touchX, touchY)) { 

      rectangles.get(i).describeContents(); 
      String Selected_rect = String.valueOf(rectangles.get(i)); 
      Store_Identified_Block.Store(Selected_rect); 
      getSelectedCoordinates(); 


      //Start a new intent 
      Intent myIntent = new Intent("android.intent.action.zoomlevel"); 
      myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

      //This is not working 
      try { 
       ctx.startActivity(myIntent); 
      } catch (Exception e) { 

       Log.d("onLongPress(MotionEvent event)", e.toString()); 
       e.printStackTrace(); 
      } 



      break; 
     } 
    } 

super.onLongPress(event); 
} 
+2

後從logcat的堆棧跟蹤,並指示在相關代碼行數。 –

+0

什麼是custom.ctx? – drulabs

+1

我會假設custom.ctx顯然是空的 – njzk2

回答

0

解決它:)

try { 

       Intent mm=new Intent(finalmainvclass.maincontaxt,zoomlevel.class); 
       mm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       finalmainvclass.maincontaxt.startActivity(mm); 

      } catch (Exception e) { 

       Log.d("MYLOG", "Error is "+e.toString()); 
       e.printStackTrace(); 
      }