在意图之间传递信息时出错

时间:2016-01-30 19:53:03

标签: android android-intent

我是android开发的新手,目前我正在使用marshmallow 6.0 api 23,sdk工具24.4.1开发android studio 1.5。我只是通过明确的意图测试传递额外信息,但每次运行后我都有

E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaca5a0c0 

在我的logcat中。我想我没有编码错误。我点击按钮后启动了第二个活动。因此Intent切换正在工作,但没有信息传递到那里。那么我怎么能解决这个奇怪的问题帮助将不胜感激

这是我的代码:

package project.kalyan.intentdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Main_intent_Activity extends AppCompatActivity implements View.OnClickListener
{


   TextView tv;
   EditText ed;
   Button sendme;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_intent_);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();





            }
        });

        tv=(TextView)findViewById(R.id.firstIntent);
        sendme=(Button)findViewById(R.id.button);
        sendme.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main_intent_, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v)
    {
        if(v.getId()==R.id.button)
        {
            Intent intent=new Intent(Main_intent_Activity.this,Activity_2.class);
            ed=(EditText)findViewById(R.id.intenttext);
            String passingtext=ed.getText().toString();
            intent.putExtra("Firstmessage:",passingtext);

            startActivity(intent);
        }


    }
}

我在Intent参数中尝试了getApplicationContext()但没有改变

我的活动2代码是

package project.kalyan.intentdemo;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class Activity_2 extends AppCompatActivity {

    TextView activetext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        activetext=(TextView)findViewById(R.id.activity2text);
        Bundle information=getIntent().getExtras();
        if(information==null)
        {
            Log.d("State is :","Nothing is passed from main Activity");
            return;
        }
        String s=information.getString("Firstmessage");
        activetext.setText(s);

    }

}

1 个答案:

答案 0 :(得分:0)

您的密钥为"Firstmessage:"而不是"Firstmessage",您需要使用第二个活动中从意图返回的捆绑包中的相同密钥

String s=information.getString("Firstmessage:");

另外请查看您的logcat,因为很可能这不是导致错误的throwable