intent问题 ,编译没有问题,但是运行程序总是崩溃

指间流沙 发布于 2015/11/18 22:46
阅读 313
收藏 0
package com.example.testitent;


import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;


public class MainActivity extends ActionBarActivity {
private Button button = null;
String s = "cbdskjba";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("s",s);
intent.putExtra("bundle",bundle);
intent.setClass(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});



package com.example.testitent;


import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;


public class MainActivity extends ActionBarActivity {
private Button button = null;
String s = "cbdskjba";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("s",s);
intent.putExtra("bundle",bundle);
intent.setClass(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});



加载中
0
九离天
九离天

虽然没有 SecondActivity 代码,目测你是在 SecondActivity.onCreate() 里这样写的:

Bundle bundle = new Bundle();
bundle = getIntent().getExtras();

那么,传递的时候,在MainActivity里要这样:

intent.putExtras(bundle);

而不是:

intent.putExtra("bundle",bundle);

0
monroe
monroe

倒是把 SecondActivity 的代码贴出来呀。

在 SecondActivity 的 30 行,有空指针异常。

0
指间流沙
指间流沙
package com.example.testitent;


import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.os.Build;


public class SecondActivity extends ActionBarActivity {
private EditText editText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText)findViewById(R.id.edit);
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        editText.setText(bundle.getString("s"));
        
        
    }


}

0
指间流沙
指间流沙

引用来自“九离天”的评论

虽然没有 SecondActivity 代码,目测你是在 SecondActivity.onCreate() 里这样写的:

Bundle bundle = new Bundle();
bundle = getIntent().getExtras();

那么,传递的时候,在MainActivity里要这样:

intent.putExtras(bundle);

而不是:

intent.putExtra("bundle",bundle);

public class SecondActivity extends ActionBarActivity {
private EditText editText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText)findViewById(R.id.edit);
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        editText.setText(bundle.getString("s"));
        
        
    }


}

返回顶部
顶部