我的记事本代码有问题。我用保存按钮制作了一个记事本,它首先显示了我们在单击按钮时输入的字符串。为此,我制作了一个名为“ 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);
}
});
例外是;
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)
最佳答案
为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);
}