Android:Sting返回空指针异常

时间:2013-09-28 06:01:33

标签: java android eclipse nullpointerexception android-edittext

我的记事本代码有问题。我用保存按钮制作了一个记事本,首先显示我们在点击按钮时输入的字符串。为此我创建了一个名为“LinedEditText.java”的类,但是当我运行我的代码时,我得到了一个名为“Null Pointer exception”的异常。这是我的代码,请让我知道我该怎么办?提前谢谢。

MainActivity.java

 public class NotePadActivity extends Activity {

        LinedEditText noteTxt;


        Button remind, save, cancel;
            String str="";


        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.notepad);

noteTxt=(LinedEditText)findViewById(R.id.noteTxt);


            save = (Button) findViewById(R.id.saveBtn);
            cancel = (Button) findViewById(R.id.cancelBtn);

            save.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    String str = (noteTxt.getText()).toString();
                    Log.i("Notepad String:", "" + str);
                }
            });

LinedEditText.java

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;

public class LinedEditText extends EditText {
    private Rect mRect;
    private Paint mPaint;

    // we need this constructor for LayoutInflater
    public LinedEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        // mPaint.setColor(R.color.edit_note_line); // SET YOUR OWN COLOR HERE
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // int count = getLineCount();

        int height = getHeight();
        int line_height = getLineHeight();

        int count = height / line_height;

        if (getLineCount() > count)
            count = getLineCount();// for long text with scrolling

        Rect r = mRect;
        Paint paint = mPaint;
        int baseline = getLineBounds(0, r);// first line

        for (int i = 0; i < count; i++) {

            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
            baseline += getLineHeight();// next line
        }

        super.onDraw(canvas);
    }
}

notepad.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/320x480x2"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/dateTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/lin" >
    </TextView>

    <LinearLayout
        android:id="@+id/lin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/noteTxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="25dp"
            android:layout_weight="0.7"
            android:text="Note"
            android:textAlignment="center"
            android:textColor="@android:color/white"
            android:textSize="20dp" >
        </TextView>

        <Button
            android:id="@+id/reminderBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="25dp"
            android:layout_marginTop="25dp"
            android:layout_weight="0.3"
            android:background="#78C7C7"
            android:text="Set Reminder" >
        </Button>
    </LinearLayout>

    <com.ashish.ashish.LinedEditText
        android:id="@+id/noteTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lin"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:layout_marginTop="25dp"
        android:gravity="start"
        android:height="200dp" >
    </com.ashish.ashish.LinedEditText>

    <LinearLayout
        android:id="@+id/lin2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/noteTxt"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/saveBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="10dp"
            android:layout_weight="0.5"
            android:background="#78C7C7"
            android:text="Save" >
        </Button>

        <Button
            android:id="@+id/cancelBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="10dp"
            android:layout_weight="0.5"
            android:background="#78C7C7"
            android:text="Cancel" >
        </Button>
    </LinearLayout>

</RelativeLayout>

MainActivity.java

save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String str = noteTxt.getText().toString();// GETTING EXCEPTION HERE
                Log.i("Notepad String:", "" + str);
            }
        });

EXCEPTION是;

09-28 11:26:57.494: E/AndroidRuntime(507): FATAL EXCEPTION: main
09-28 11:26:57.494: E/AndroidRuntime(507): java.lang.NullPointerException
09-28 11:26:57.494: E/AndroidRuntime(507):  at com..NotePadActivity$2.onClick(NotePadActivity.java:49)
09-28 11:26:57.494: E/AndroidRuntime(507):  at android.view.View.performClick(View.java:2485)
09-28 11:26:57.494: E/AndroidRuntime(507):  at android.view.View$PerformClick.run(View.java:9080)
09-28 11:26:57.494: E/AndroidRuntime(507):  at android.os.Handler.handleCallback(Handler.java:587)
09-28 11:26:57.494: E/AndroidRuntime(507):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-28 11:26:57.494: E/AndroidRuntime(507):  at android.os.Looper.loop(Looper.java:123)
09-28 11:26:57.494: E/AndroidRuntime(507):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-28 11:26:57.494: E/AndroidRuntime(507):  at java.lang.reflect.Method.invokeNative(Native Method)
09-28 11:26:57.494: E/AndroidRuntime(507):  at java.lang.reflect.Method.invoke(Method.java:507)
09-28 11:26:57.494: E/AndroidRuntime(507):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-28 11:26:57.494: E/AndroidRuntime(507):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-28 11:26:57.494: E/AndroidRuntime(507):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

为LinedEditText类添加所有三个默认构造函数。它可以解决你的问题。

public LinedEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

}

public LinedEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    mRect = new Rect();
    mPaint = new Paint();
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);

}

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

}

答案 1 :(得分:0)

    you already defined id noteTxt for TextView here.

      <TextView
                android:id="@+id/noteTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dp"
                android:layout_marginTop="25dp"
                android:layout_weight="0.7"
                android:text="Note"
                android:textAlignment="center"
                android:textColor="@android:color/white"
                android:textSize="20dp" >
            </TextView>

     So change id for LinedEditText view..

     <com.kalnirnay.kalnirnay.LinedEditText
            android:id="@+id/noteEdt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/lin"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="25dp"
            android:gravity="start"
            android:height="200dp" >
        </com.kalnirnay.kalnirnay.LinedEditText>
same in code..
   LinedEditText noteEdt;
                  noteEdt=(LinedEditText)findViewById(R.id.noteEdt);