我正在尝试使用自定义视图类执行addView。但它根本不起作用。请查看我的代码和错误日志。我希望你能帮助我。
我做了一个内在的观点。现在我想用addView()将它添加到我的布局xml中,因此它提供了一个nullpointer异常
代码:
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.
答案 0 :(得分: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);
}