2013-10-31 67 views
-2

我想用我的自定義視圖類來做addView。但它根本不起作用。請看看我的代碼和錯誤日誌。我希望你能幫助我。Android添加自定義視圖不起作用

我已經做了一個內部的costum意見。現在我想將它添加到我的addView()佈局XML所以它給出了一個空指針異常

代碼:

package com.example.les14; 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Rect; 
import android.os.Bundle; 
import android.support.v4.view.ViewPager.LayoutParams; 
import android.util.DisplayMetrics; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.LinearLayout; 
import android.widget.RadioGroup; 
import android.widget.RadioGroup.OnCheckedChangeListener; 


public class MainActivity extends Activity implements OnTouchListener{ 
    MyView myView; 
    int numberOfFingers = 0; 
    float oldX[] = new float[2], oldY[] = new float[2]; 
    Rect rectangle = new Rect(0, 0, 100, 100); 
    DisplayMetrics metrics = new DisplayMetrics(); 
    String radioButtonSelected = ""; 
    RadioGroup radioGroup; 
    int checkedRadioButton = 1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     MyView myView = new MyView(this); 
     LinearLayout layout = (LinearLayout) findViewById(R.id.mainlayout); 
     layout.addView(myView); 
     myView.setOnTouchListener(this); 
    // radioGroup = (RadioGroup) findViewById(R.id.radioGroup1); 

     getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     //radioGroup.setOnCheckedChangeListener(this); 

     setContentView(R.layout.activity_main); 


    } 
// @Override 
// public void onCheckedChanged(RadioGroup radioGroup, int arg1) { 
     // TODO Auto-generated method stub 

    //} 
    void radio(){ 


     int checkedRadioButton = radioGroup.getCheckedRadioButtonId(); 

     switch (checkedRadioButton) { 
     case R.id.radio0 : radioButtonSelected = "AO1"; 
             break; 
     case R.id.radio1 : radioButtonSelected = "AO2"; 
            break; 

    } 

    } 
    public boolean onTouch(View view, MotionEvent event){ 
     switch(event.getActionMasked()) { 
     case MotionEvent.ACTION_DOWN: 
      numberOfFingers = 1; 
      oldX[0] = event.getX(0); 
      oldY[0] = event.getY(0); 
      break; 
     case MotionEvent.ACTION_POINTER_DOWN: 
      numberOfFingers = 2; 
      oldX[1] = event.getX(1); 
      oldY[1] = event.getY(1); 
      break; 
     case MotionEvent.ACTION_MOVE: 
      handleMove(event); 
      break; 
     case MotionEvent.ACTION_POINTER_UP: 
     case MotionEvent.ACTION_UP: 
      numberOfFingers--; 
      break; 
     } 

     view.invalidate(); 
     return true; 
    } 

    public class MyView extends View { 
     Paint redPaint = new Paint(); 

     public MyView(Context context) { 
      super(context); 


      redPaint.setColor(Color.RED); 
     } 


     public void onDraw(Canvas canvas){ 

      canvas.drawRect(rectangle, redPaint); 
     } 





    } 

    float newX[] = new float[2], newY[] = new float[2]; 
    int xChange[] = new int[2], yChange[] = new int[2]; 
    int diffX, diffY; 
    int newLeft = rectangle.left, newTop = rectangle.top, 
      newRight = rectangle.right, 
      newBottom = rectangle.bottom; 

    void handleMove(MotionEvent event) { 
     newX[0] = Math.round(event.getX(0)); 
     newY[0] = Math.round(event.getY(0)); 
     xChange[0] = Math.round(newX[0] - oldX[0]); 
     yChange[0] = Math.round(newY[0] - oldY[0]); 
     oldX[0] = newX[0]; 
     oldY[0] = newY[0]; 

     switch (numberOfFingers) { 
     case 1: 

      newLeft = rectangle.left + xChange[0]; 
      newTop = rectangle.top + yChange[0]; 
      newRight = rectangle.right + xChange[0]; 
      newBottom = rectangle.bottom + yChange[0]; 
      if(newLeft < 0 || newRight > metrics.widthPixels){ 
       newLeft = rectangle.left; 
       newRight = rectangle.right; 
      } 
      if(newTop < 0 || newBottom > metrics.heightPixels){ 
       newTop = rectangle.top; 
       newBottom = rectangle.bottom; 
      } 
      rectangle = new Rect(newLeft, newTop, newRight, newBottom); 
      break; 

     case 2: 
      newX[1] = Math.round(event.getX(1)); 
      newY[1] = Math.round(event.getY(1)); 

      diffX = 
        Math.abs(Math.round(newX[1] - newX[0])) 
         - Math.abs(Math.round(oldX[1] - oldX[0])); 
      diffY = 
        Math.abs(Math.round(newY[1] - newY[0])) 
         - Math.abs(Math.round(oldY[1] - oldY[0])); 
      oldX[1] = newX[1]; 
      oldY[1] = newY[1]; 

      newLeft = rectangle.left - diffX/2; 
      newTop = rectangle.top - diffY/2; 
      newRight = rectangle.right + diffX/2; 
      newBottom = rectangle.bottom + diffY/2; 
      rectangle = new Rect(newLeft, newTop, newRight, newBottom); 
      break; 
     } 
    } 



} 

,這裏是我的錯誤日誌:

10-31 04:45:17.663: E/AndroidRuntime(908): FATAL EXCEPTION: main 
10-31 04:45:17.663: E/AndroidRuntime(908): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.les14/com.example.les14.MainActivity}: java.lang.NullPointerException 
10-31 04:45:17.663: E/AndroidRuntime(908): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
10-31 04:45:17.663: E/AndroidRuntime(908): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
10-31 04:45:17.663: E/AndroidRuntime(908): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
10-31 04:45:17.663: E/AndroidRuntime(908): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
10-31 04:45:17.663: E/AndroidRuntime(908): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-31 04:45:17.663: E/AndroidRuntime(908): at android.os.Looper.loop(Looper.java:137) 
10-31 04:45:17.663: E/AndroidRuntime(908): at android.app.ActivityThread.main(ActivityThread.java:5103) 
10-31 04:45:17.663: E/AndroidRuntime(908): at java.lang.reflect.Method.invokeNative(Native Method) 
10-31 04:45:17.663: E/AndroidRuntime(908): at java.lang.reflect.Method.invoke(Method.java:525) 
10-31 04:45:17.663: E/AndroidRuntime(908): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
10-31 04:45:17.663: E/AndroidRuntime(908): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
10-31 04:45:17.663: E/AndroidRuntime(908): at dalvik.system.NativeStart.main(Native Method) 
10-31 04:45:17.663: E/AndroidRuntime(908): Caused by: java.lang.NullPointerException 
10-31 04:45:17.663: E/AndroidRuntime(908): at com.example.les14.MainActivity.onCreate(MainActivity.java:35) 
10-31 04:45:17.663: E/AndroidRuntime(908): at android.app.Activity.performCreate(Activity.java:5133) 
10-31 04:45:17.663: E/AndroidRuntime(908): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
10-31 04:45:17.663: E/AndroidRuntime(908): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
10-31 04:45:17.663: E/AndroidRuntime(908): ... 11 more 
10-31 04:50:18.133: I/Process(908): Sending signal. PID: 908 SIG: 9 
10-31 05:01:08.913: D/gralloc_goldfish(952): Emulator without GPU emulation detected. 
+2

你應該複製/過去的logcat的內容,而不是發佈的圖像 – Blackbelt

+0

奧凱張貼在代碼文本 – PatrickH

回答

3

移動你頂部有setContentView。您應該在執行findViewById之前設置內容視圖。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    MyView myView = new MyView(this); 
    LinearLayout layout = (LinearLayout) findViewById(R.id.mainlayout); 
    layout.addView(myView); 
    myView.setOnTouchListener(this); 
    // radioGroup = (RadioGroup) findViewById(R.id.radioGroup1); 

    getWindowManager().getDefaultDisplay().getMetrics(metrics); 
    //radioGroup.setOnCheckedChangeListener(this); 
} 
+0

太好了!謝謝!但爲什麼這個解決方案,爲什麼我必須把它放在最前面呢? – PatrickH

+0

@PatrickH boz you'findViewById'尋找一個視圖,在當前的infalted佈局中標識id。如果你試圖初始化你的NPE – Raghunandan

+0

好吧,現在我明白了。非常感謝你,我會盡快接受你的回答(分鐘或5分鐘) – PatrickH