大家我在将日期从日历传递到我的 Create_Event.class 时遇到问题。最初日期 textView 显示“文本”,然后当我单击“选择日期”按钮时,日历页面将显示,并且日期应该传递给最初显示“文本”的 textView。我尝试使用intent.putExtra 将日期从日历页面传递到Create_Events 页面进行编码,但由于NullPointerException,我遇到了强制关闭错误。谁能帮我解决这个问题?非常感谢。
供参考,代码:
日历.java
@Override
public void onClick(View view)
{
String date_month_year = (String)view.getTag();
selectedDayMonthYearButton.setText(new StringBuilder().append("Selected:").append(date_month_year));
Intent k = new Intent(Calendar_Event.this, Create_Events.class);
k.putExtra("passdate", date_month_year);
startActivity(k);
Create_Event.java
Intent intent = this.getIntent();
if(intent == null)
{
String strdata = intent.getExtras().getString("passdate");
TextView txtDate = (TextView) findViewById(R.id.textDate);
txtDate.setText(strdata);
}
else
{
}
***最初 if(intent == null) 是 if(intent != null),我认为改变它会起作用,但它没有。:P
更新了logcat:
09-07 12:03:28.177: E/AndroidRuntime(1033): FATAL EXCEPTION: main
09-07 12:03:28.177: E/AndroidRuntime(1033): java.lang.RuntimeException: Unable to start activity ComponentInfo{main.page/main.page.Create_Events}: java.lang.NullPointerException
09-07 12:03:28.177: E/AndroidRuntime(1033): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-07 12:03:28.177: E/AndroidRuntime(1033): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-07 12:03:28.177: E/AndroidRuntime(1033): at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-07 12:03:28.177: E/AndroidRuntime(1033): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-07 12:03:28.177: E/AndroidRuntime(1033): at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 12:03:28.177: E/AndroidRuntime(1033): at android.os.Looper.loop(Looper.java:137)
09-07 12:03:28.177: E/AndroidRuntime(1033): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-07 12:03:28.177: E/AndroidRuntime(1033): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 12:03:28.177: E/AndroidRuntime(1033): at java.lang.reflect.Method.invoke(Method.java:511)
09-07 12:03:28.177: E/AndroidRuntime(1033): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-07 12:03:28.177: E/AndroidRuntime(1033): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-07 12:03:28.177: E/AndroidRuntime(1033): at dalvik.system.NativeStart.main(Native Method)
09-07 12:03:28.177: E/AndroidRuntime(1033): Caused by: java.lang.NullPointerException
09-07 12:03:28.177: E/AndroidRuntime(1033): at main.page.Create_Events.onCreate(Create_Events.java:261)
09-07 12:03:28.177: E/AndroidRuntime(1033): at android.app.Activity.performCreate(Activity.java:4465)
09-07 12:03:28.177: E/AndroidRuntime(1033): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-07 12:03:28.177: E/AndroidRuntime(1033): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
任何帮助将不胜感激!再次感谢。
更新:
Bundle bundle = getIntent().getExtras();
if(bundle != null)
{
String date = bundle.getString("date");
TextView txtDate = (TextView) findViewById(R.id.textDate);
txtDate.setText(date);
}