getExtras导致应用程序崩溃

时间:2014-01-15 21:38:17

标签: java android listview bundle

我正在开发一个Android App,它允许我在CreateActivity中创建一个Event,然后通过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;
        }
    }  

}

CREATEACTIVITY

 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)

4 个答案:

答案 0 :(得分:1)

我认为问题是你没有初始化lstEvents。 让我们在onCreate()方法中尝试lstEvents = new ArrayList<Event>()

答案 1 :(得分:1)

我发现这个问题是由于最初打开应用程序时尝试在尚未放置的值上设置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");
    }

答案 2 :(得分:0)

您应该在onCreate()方法中声明所有变量!

public class WebsiteActivity extends AppCompatActivity {

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_website);

        WebView webView;
        Intent intent = getIntent();
        String website = intent.getStringExtra("key");

        //Open website
        webView = findViewById(R.id.webViewId);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl(website);

    }
}

答案 3 :(得分:-1)

我的猜测就是这一行:

   Integer id = eventItem.get_Id();

get_Id()返回null,基于您没有在新的Event()调用中初始化该字段的观察,并且您还没有养成初始化变量实例的习惯。

鉴于此,您将传入null到view.setId(id)调用。初始化时,请不要将其初始化为0,因为它不是视图的有效ID。你可以使用NO_ID或正数。

此外: 在onCreate() 您可以检查findViewById(R.id.add)是否返回View而不是null。这将导致在正确的代码行附近出现NullPointerException。