gpt4 book ai didi

java - getExtras 导致应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 06:19:27 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,它允许我在 CreateActivity 中创建一个事件,然后通过 Bundle 和 putExtras() 将数据从 CreateActivity 传递到 MainActivity。

看起来我使用的 .getExtras() 或 getString() 是错误的。 一旦实现 b.getString("TITLE") 函数,应用就会崩溃。

public class MainActivity extends FragmentActivity implements OnClickListener {

ListView listView;
int lastIndex = -1;
ArrayList<Event> lstEvents;

// detail view
TextView tvTitle, tvTime, tvDate;
ImageView img;
View vw_master;

boolean _isBack = true;

ImageButton add;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

// get list view
listView = (ListView) findViewById(R.id.listViewFragment);
lstEvents = new ArrayList<Event>();

// // get detail controls
tvTitle = (TextView) findViewById(R.id.textViewTitle);
tvDate = (TextView) findViewById(R.id.textViewDate);
tvTime = (TextView) findViewById(R.id.textViewTime);

Bundle b = this.getIntent().getExtras();
b.getString("TITLE");



add = (ImageButton) findViewById(R.id.add);
add.setOnClickListener(this);

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.add:
Intent intent = new Intent(this, CreateActivity.class);
startActivity(intent);
break;
}
}

}

创建 Activity

 public class CreateActivity extends Activity implements OnClickListener {

EditText etTitle;
Button btDate;
Button btTime;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

// /onclicklistener
findViewById(R.id.btn_confirm).setOnClickListener(this);

// edittexts and buttons
btDate = (Button) findViewById(R.id.btn_date);
etTitle = (EditText) findViewById(R.id.editTextTitle);
btTime = (Button) findViewById(R.id.btn_time);
}

// Will be called via the onClick attribute
// of the buttons in main.xml
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_confirm:
String title = etTitle.getText().toString();
String time = btTime.getText().toString();
String date = btDate.getText().toString();

Log.e("LOG", title);
Log.e("LOG", time);
Log.e("LOG", date);



Bundle newBundle = new Bundle();
newBundle.putString("TITLE", title);
newBundle.putString("TIME", time);
newBundle.putString("DATE", date);



Intent intent = new Intent(this, MainActivity.class);
intent.putExtras(newBundle);
startActivity(intent);
break;
}

}

public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
}

public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}

}

错误日志:

03-03 15:44:07.117: E/AndroidRuntime(15202): FATAL EXCEPTION: main
03-03 15:44:07.117: E/AndroidRuntime(15202): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.datetracker/com.example.datetracker.MainActivity}: java.lang.NullPointerException
03-03 15:44:07.117: E/AndroidRuntime(15202): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
03-03 15:44:07.117: E/AndroidRuntime(15202): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
03-03 15:44:07.117: E/AndroidRuntime(15202): at android.app.ActivityThread.access$600(ActivityThread.java:146)
03-03 15:44:07.117: E/AndroidRuntime(15202): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
03-03 15:44:07.117: E/AndroidRuntime(15202): at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 15:44:07.117: E/AndroidRuntime(15202): at android.os.Looper.loop(Looper.java:137)
03-03 15:44:07.117: E/AndroidRuntime(15202): at android.app.ActivityThread.main(ActivityThread.java:5168)
03-03 15:44:07.117: E/AndroidRuntime(15202): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 15:44:07.117: E/AndroidRuntime(15202): at java.lang.reflect.Method.invoke(Method.java:511)
03-03 15:44:07.117: E/AndroidRuntime(15202): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
03-03 15:44:07.117: E/AndroidRuntime(15202): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
03-03 15:44:07.117: E/AndroidRuntime(15202): at dalvik.system.NativeStart.main(Native Method)
03-03 15:44:07.117: E/AndroidRuntime(15202): Caused by: java.lang.NullPointerException
03-03 15:44:07.117: E/AndroidRuntime(15202): at com.example.datetracker.MainActivity.onCreate(MainActivity.java:47)
03-03 15:44:07.117: E/AndroidRuntime(15202): at android.app.Activity.performCreate(Activity.java:5200)
03-03 15:44:07.117: E/AndroidRuntime(15202): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-03 15:44:07.117: E/AndroidRuntime(15202): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)

最佳答案

我发现问题是由以下事实引起的:当应用程序最初打开时,它试图对尚未放置的值进行 getExtras() 。

我用它作为一个简单的修复来检查首次启动时是否有任何额外的内容。

  Intent intent = getIntent();
if (intent.getExtras() == null) {
// Do first time stuff here
} else {
// Do stuff with intent data here
Bundle b = getIntent().getExtras();
title = b.getString("TITLE");
time = b.getString("TIME");
date = b.getString("DATE");
    }

关于java - getExtras 导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21148765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com