为什么在自定义视图中创建EditText的引用会产生空指针异常

时间:2012-08-19 23:35:20

标签: android nullpointerexception android-edittext android-custom-view

我试图在我的CustomView中获取EditText的值,但是引用它会产生NullPointer异常...请帮助!!!

这是我的ImageActivity的xml文件..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/buttonDraw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Draw Rect" >
    </Button>

    <Button
        android:id="@+id/buttonText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Make Text" >
    </Button>

    <Button
        android:id="@+id/buttonExit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Exit" >
    </Button>
</LinearLayout>

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFffff" >

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ImageView>
</FrameLayout>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/editText"             <!--This is my EditText which gives Exception-->
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Value Here!!!" >
    </EditText>

    <Button
        android:id="@+id/buttonSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Save" >
    </Button>
</LinearLayout>

这是我的ImageActivity ...

public class ImageActivity extends Activity {

DrawPanel dview;
DrawText dtext;
EditText et;
FrameLayout fm;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);

    Bundle bundle = getIntent().getExtras();
    final String value = bundle.getString("key");
    Bitmap bmp = BitmapFactory.decodeFile(value);
    ImageView imageView = (ImageView) findViewById(R.id.imgView);
    imageView.setImageBitmap(bmp);

    Button bd = (Button) findViewById(R.id.buttonDraw);
    Button bt = (Button) findViewById(R.id.buttonText);
    Button be = (Button) findViewById(R.id.buttonExit);

    fm = (FrameLayout) findViewById(R.id.main_view);
    dview = new DrawPanel(this);
    dtext = new DrawText(this);
    et = (EditText) findViewById(R.id.editText);
    String text = et.getText().toString();
    System.out.println(text);
    bd.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            fm.addView(dview);

        }
    });

    bt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            et.requestFocus();
            fm.addView(dtext);

        }
    });

    et.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // dtext.invalidate();

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    be.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            finish();

        }
    });
}

}

这是CustomView DrawText类,其中EditText的引用给出了NullPointer异常。

public class DrawText extends View implements OnTouchListener{

static int xp = 0, yp = 0;
static int margrecth = 30;
static int margrectw = 40;
static int xn = 100, yn = 100;
static int flag = 0;
static int scrolrect = 0;
static int scrolleftrect = 0;
static int scrolrightrect = 0;
static int scroltoprect = 0;
static int scrolbottomrect = 0;
int rectr = xn + margrectw, rectl = xn - margrectw, rectb = yn + margrecth,
        rectt = yn - margrecth;
Paint paint; // using this ,we can draw on canvas

Rectangle Drect;
ResizeBall lball;
ResizeBall tball;
ResizeBall rball;
ResizeBall bball;
EditText et;
final LinearLayout ll;
String dstext = null;

public DrawText(Context context) {
    super(context);
    paint = new Paint();
    paint.setAntiAlias(true); // setting the paint color

    Drect = new Rectangle(rectl, rectt, rectr, rectb);
    lball = new ResizeBall(rectl, yn, 5);
    tball = new ResizeBall(xn, rectt, 5);
    rball = new ResizeBall(rectr, yn, 5);
    bball = new ResizeBall(xn, rectb, 5);
    ll = (LinearLayout) findViewById(R.id.linear_view);
    // to make it focusable so that it will receive touch events properly
    setFocusable(true);
    // adding touch listener to this view
    this.setOnTouchListener(this);
}

// overriding the View's onDraw(..) method
public void onDraw(Canvas canvas) {
    et = (EditText) ll.findViewById(R.id.editText);
    String text = et.getText().toString();
    System.out.println(text);
    if (flag == 0) {
        DrawAll(canvas);
    } else if (flag == 1) {

        if (scrolrect == 1) {
            DrawAll(canvas);
        } else if (scrolrightrect == 1) {
            DrawAll(canvas);
        } else if (scrolleftrect == 1) {
            DrawAll(canvas);
        } else if (scroltoprect == 1) {
            DrawAll(canvas);
        } else if (scrolbottomrect == 1) {
            DrawAll(canvas);
        }
    }

}

private void DrawAll(Canvas canvas) {
    //et = (EditText) findViewById(R.id.editText);

    paint.setStrokeWidth(4);
    paint.setColor(Color.BLACK);
    canvas.drawRect(Drect.getLeft(), Drect.getTop(), Drect.getRight(),Drect.getBottom(),          paint); // mainRect
    paint.setStrokeWidth(0);
    paint.setColor(Color.WHITE);
    canvas.drawRect(Drect.getLeft()+4, Drect.getTop()+4, Drect.getRight()-4,Drect.getBottom()-4, paint);
    paint.setColor(Color.BLACK);
    canvas.drawText("et.getText().toString()", Drect.getLeft()+10, Drect.getTop()+15, paint);
    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(5);
    canvas.drawCircle(lball.getCenterx(), lball.getCentery(), 5, paint); // resizeRect
                                                                            // left
    canvas.drawCircle(tball.getCenterx(), tball.getCentery(), 5, paint); // resizeRect
                                                                            // right
    canvas.drawCircle(rball.getCenterx(), rball.getCentery(), 5, paint); // resizeRect
                                                                            // top
    canvas.drawCircle(bball.getCenterx(), bball.getCentery(), 5, paint); // resizeRect
                                                                            // bottom

}

// this is the interface method of "OnTouchListener"
public boolean onTouch(View view, MotionEvent event) {
          //code not included as it is irrelevant 
          //to current problem as far as my understanding

    return true;
}

}

以下是我的LogCat ....

08-20 04:57:11.550: E/AndroidRuntime(10960): FATAL EXCEPTION: main
08-20 04:57:11.550: E/AndroidRuntime(10960): java.lang.NullPointerException
08-20 04:57:11.550: E/AndroidRuntime(10960):    at com.nexapp.imgedit.DrawText.onDraw(DrawText.java:57)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.View.draw(View.java:6880)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.View.draw(View.java:6883)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.View.draw(View.java:6883)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.View.draw(View.java:6883)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.ViewRoot.draw(ViewRoot.java:1522)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.os.Looper.loop(Looper.java:123)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at android.app.ActivityThread.main(ActivityThread.java:3683)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at java.lang.reflect.Method.invokeNative(Native Method)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at java.lang.reflect.Method.invoke(Method.java:507)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-20 04:57:11.550: E/AndroidRuntime(10960):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

定义DrawText类的构造函数时,传递活动:

public DrawText(Activity activity) {
    super(activity);
    // your existing initialization code
    ll = (LinearLayout)activity.findViewById(R.id.linear_view);
}

现在你应该能够从ll。

中获取TextEdit