我需要一些android编程帮助。所以onbutton点击我在icite方法中调用。基本上,当单击按钮时,所有文本框文本都将转换为字符串到新变量上。之后,我将所有变量组合成一个名为Total的字符串。然后我将标签textView10的文本更改为字符串Total。但它只是崩溃了我的应用程序。你知道我可能做错了什么吗? 我的logcat:
11-17 03:02:43.652: W/dalvikvm(1952): threadid=1: thread exiting with uncaught exception (group=0xb1f60648)
11-17 03:02:43.662: E/AndroidRuntime(1952): FATAL EXCEPTION: main
11-17 03:02:43.662: E/AndroidRuntime(1952): java.lang.NullPointerException
11-17 03:02:43.662: E/AndroidRuntime(1952): at com.example.randomapplication.citationsPage$1.onClick(citationsPage.java:59)
11-17 03:02:43.662: E/AndroidRuntime(1952): at android.view.View.performClick(View.java:4240)
11-17 03:02:43.662: E/AndroidRuntime(1952): at android.view.View$PerformClick.run(View.java:17721)
11-17 03:02:43.662: E/AndroidRuntime(1952): at android.os.Handler.handleCallback(Handler.java:730)
11-17 03:02:43.662: E/AndroidRuntime(1952): at android.os.Handler.dispatchMessage(Handler.java:92)
11-17 03:02:43.662: E/AndroidRuntime(1952): at android.os.Looper.loop(Looper.java:137)
11-17 03:02:43.662: E/AndroidRuntime(1952): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-17 03:02:43.662: E/AndroidRuntime(1952): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 03:02:43.662: E/AndroidRuntime(1952): at java.lang.reflect.Method.invoke(Method.java:525)
11-17 03:02:43.662: E/AndroidRuntime(1952): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-17 03:02:43.662: E/AndroidRuntime(1952): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-17 03:02:43.662: E/AndroidRuntime(1952): at dalvik.system.NativeStart.main(Native Method)
11-17 03:02:45.552: D/dalvikvm(1982): GC_FOR_ALLOC freed 92K, 11% free 2541K/2832K, paused 8ms, total 9ms
11-17 03:02:45.552: I/dalvikvm-heap(1982): Grow heap (frag case) to 3.277MB for 635808-byte allocation
11-17 03:02:45.562: D/dalvikvm(1982): GC_FOR_ALLOC freed <1K, 9% free 3162K/3456K, paused 11ms, total 11ms
11-17 03:02:45.572: D/dalvikvm(1982): GC_FOR_ALLOC freed <1K, 9% free 3162K/3456K, paused 4ms, total 4ms
11-17 03:02:45.572: I/dalvikvm-heap(1982): Grow heap (frag case) to 3.753MB for 500412-byte allocation
11-17 03:02:45.592: D/dalvikvm(1982): GC_FOR_ALLOC freed <1K, 8% free 3650K/3948K, paused 14ms, total 15ms
11-17 03:02:45.662: D/libEGL(1982): loaded /system/lib/egl/libEGL_emulation.so
11-17 03:02:45.662: D/(1982): HostConnection::get() New Host Connection established 0xb7acdfa0, tid 1982
11-17 03:02:45.672: D/libEGL(1982): loaded /system/lib/egl/libGLESv1_CM_emulation.so
11-17 03:02:45.682: D/libEGL(1982): loaded /system/lib/egl/libGLESv2_emulation.so
11-17 03:02:45.692: W/EGL_emulation(1982): eglSurfaceAttrib not implemented
11-17 03:02:45.702: D/OpenGLRenderer(1982): Enabling debug mode 0
代码
package com.example.randomapplication;
import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class citationsPage extends Activity {
EditText editText1;
EditText author;
EditText articleTitle;
EditText webTitle;
EditText editText2;
EditText webDay;
EditText webMonth;
EditText webYears;
TextView textView10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.citations);
final Button citationButton = (Button) findViewById(R.id.button10);
citationButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
// Gets date from the calendar!
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
String month1 = String.valueOf(month);
String day1 = String.valueOf(day);
String year1 = String.valueOf(year);
editText1 = (EditText) findViewById(R.id.editText1);
author = (EditText) findViewById(R.id.author);
articleTitle = (EditText) findViewById(R.id.articleTitle);
webTitle = (EditText) findViewById(R.id.webTitle);
webDay = (EditText) findViewById(R.id.webDay);
webMonth = (EditText) findViewById(R.id.webMonth);
webYears = (EditText) findViewById(R.id.webYears);
String authorFirst1 = editText1.getText().toString();
String authorLast1 = author.getText().toString();
String articleTitle1 = articleTitle.getText().toString();
String website1 = webTitle.getText().toString();
String Publisher1 = editText2.getText().toString();
String month2 = webMonth.getText().toString();
String day2 = webDay.getText().toString();
String year2 = webYears.getText().toString();
TextView citationText = (TextView) findViewById(R.id.textView10);
String Total = authorLast1+","+" "+authorFirst1+"."+" "+"\""+articleTitle1+"."+"\""+" "+website1+"."+" "+Publisher1+","+" "+day2+" "+month2+" "+year2+"."+" "+"Web"+"."+" "+day+" "+month+"."+" "+year+".";
citationText.setText(authorFirst1);
}
});
}
@SuppressLint("NewApi") public void iCite() {
Calendar cal = Calendar.getInstance();
// Gets date from the calendar!
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
String month1 = String.valueOf(month);
String day1 = String.valueOf(day);
String year1 = String.valueOf(year);
editText1 = (EditText) findViewById(R.id.editText1);
author = (EditText) findViewById(R.id.author);
articleTitle = (EditText) findViewById(R.id.articleTitle);
webTitle = (EditText) findViewById(R.id.webTitle);
webDay = (EditText) findViewById(R.id.webDay);
webMonth = (EditText) findViewById(R.id.webMonth);
webYears = (EditText) findViewById(R.id.webYears);
String authorFirst1 = editText1.getText().toString();
String authorLast1 = author.getText().toString();
String articleTitle1 = articleTitle.getText().toString();
String website1 = webTitle.getText().toString();
String Publisher1 = editText2.getText().toString();
String month2 = webMonth.getText().toString();
String day2 = webDay.getText().toString();
String year2 = webYears.getText().toString();
TextView citationText = (TextView) findViewById(R.id.textView10);
String Total = authorLast1+","+" "+authorFirst1+"."+" "+"\""+articleTitle1+"."+"\""+" "+website1+"."+" "+Publisher1+","+" "+day2+" "+month2+" "+year2+"."+" "+"Web"+"."+" "+day+" "+month+"."+" "+year+".";
citationText.setText(Total);
/*ClipboardManager clipboard = (ClipboardManager)
getSystemService(Context.CLIPBOARD_SERVICE);
Uri copyUri = Uri.parse(authorLast1+","+" "+authorFirst1+"."+" "+"\""+articleTitle1+"."+"\""+" "+website1+"."+" "+Publisher1+","+" "+day2+" "+month2+" "+year2+"."+" "+"Web"+"."+" "+day+" "+month+"."+" "+year+".");
//ClipData clip = ClipData.newPlainText(Total + "");
ClipData clip = ClipData.newUri(getContentResolver(),"URI",copyUri);
*/
}
}
答案 0 :(得分:1)
您的观看次数会导致nullpointerexception
,因为它们是R.layout.citations
非按钮视图的一部分。因此,将以下视图定义到onclick外部
editText1 = (EditText) findViewById(R.id.editText1);
author = (EditText) findViewById(R.id.author);
articleTitle = (EditText) findViewById(R.id.articleTitle);
webTitle = (EditText) findViewById(R.id.webTitle);
webDay = (EditText) findViewById(R.id.webDay);
webMonth = (EditText) findViewById(R.id.webMonth);
webYears = (EditText) findViewById(R.id.webYears);
TextView citationText = (TextView) findViewById(R.id.textView10);
你也忘了初始化editText2,所以也把它作为
初始化 editText2 = (EditText) findViewById(R.id.editText2);
即。将onCreate方法重写为
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.citations);
final Button citationButton = (Button) findViewById(R.id.button10);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
author = (EditText) findViewById(R.id.author);
articleTitle = (EditText) findViewById(R.id.articleTitle);
webTitle = (EditText) findViewById(R.id.webTitle);
webDay = (EditText) findViewById(R.id.webDay);
webMonth = (EditText) findViewById(R.id.webMonth);
webYears = (EditText) findViewById(R.id.webYears);
final TextView citationText = (TextView) findViewById(R.id.textView10);
citationButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
// Gets date from the calendar!
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
String month1 = String.valueOf(month);
String day1 = String.valueOf(day);
String year1 = String.valueOf(year);
String authorFirst1 = editText1.getText().toString();
String authorLast1 = author.getText().toString();
String articleTitle1 = articleTitle.getText().toString();
String website1 = webTitle.getText().toString();
String Publisher1 = editText2.getText().toString();
String month2 = webMonth.getText().toString();
String day2 = webDay.getText().toString();
String year2 = webYears.getText().toString();
String Total = authorLast1+","+" "+authorFirst1+"."+" "+"\""+articleTitle1+"."+"\""+" "+website1+"."+" "+Publisher1+","+" "+day2+" "+month2+" "+year2+"."+" "+"Web"+"."+" "+day+" "+month+"."+" "+year+".";
citationText.setText(authorFirst1);
}
});
答案 1 :(得分:1)
我同意@ Giru-Bhai,组件视图必须在创建视图时定义,在OnCreate()
方法中,而不是在任何其他方法内,因此这个块必须只有一次:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.citations);
editText1 = (EditText) findViewById(R.id.editText1);
author = (EditText) findViewById(R.id.author);
articleTitle = (EditText) findViewById(R.id.articleTitle);
webTitle = (EditText) findViewById(R.id.webTitle);
webDay = (EditText) findViewById(R.id.webDay);
webMonth = (EditText) findViewById(R.id.webMonth);
webYears = (EditText) findViewById(R.id.webYears);
现在,在线
String Total = authorLast1+","+" "+authorFirst1+"."+" "+"\""+articleTitle1+"."+"\""+" "+website1+"."+" "+Publisher1+","+" "+day2+" "+month2+" "+year2+"."+" "+"Web"+"."+" "+day+" "+month+"."+" "+year+".";
citationText.setText(authorFirst1);
我认为将标签CitationText的内容设置为authorFirst1时会出现错误,也可能会改进连接。
String Total = authorLast1+","+" "+authorFirst1+"."+" "+"\""+articleTitle1+"."+"\""+" "+website1+"."+" "+Publisher1+","+" "+day2+" "+month2+" "+year2+"."+" "+"Web"+"."+" "+day1+" "+month1+"."+" "+year1+".";
citationText.setText(authorFirst1);
String Total = authorLast1+", "+authorFirst1+". \""+articleTitle1+".\" "+website1+"."+Publisher1+", "+day2+" "+month2+" "+year2+". "+"Web"+". "+day1+" "+month1+". "+year1+".";
citationText.setText(Total);
如果您不使用数组,则获得NullPointerException的唯一原因是当您寻址代码无法识别的视图时。一旦开始编写OnCreate()
方法,就必须将所有视图声明为布局的一部分。