应用程序从活动移动到另一个时崩溃

时间:2013-12-11 01:52:48

标签: android

第一项活动:
单击第一个活动的按钮时,它会崩溃

package com.example.android_hw_3;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity 
{
    Button btn; 
    TextView tv; 
    Button btn2;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn2 = (Button) findViewById(R.id.btn2);
        btn = (Button) findViewById(R.id.btn1);
         tv = (TextView)findViewById(R.id.tv1);

         btn.setOnClickListener(new View.OnClickListener() 

         {

            @Override
            public void onClick(View view) {
                show();

            }
        });

         btn2.setOnClickListener(new View.OnClickListener() 
         {


            @Override
            public void onClick(View v) {
                move();

            }
        });
    }



    public void show()

    {
        tv.setText("This is a test ..Please work :$");


    }
    public void move()
    {

        Intent i = new Intent (this,Second.class);
        i.putExtra("value", "this is from first :D ");
        startActivityForResult(i,1);


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

}

第二:

package com.example.android_hw_3;

import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

public class Second extends Activity {
    TextView tv2;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
         super.onCreate(savedInstanceState);
            setContentView(R.layout.second);
            Bundle extra = getIntent().getExtras();
            String val1 = extra.getString("value");
            TextView tv2 = (TextView)findViewById(R.id.tv2);
            tv2.setText(val1);
    }

4 个答案:

答案 0 :(得分:2)

从您的FirstActivity使用startActivityForResult()方法

调用SecondActivity

例如:

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

在SecondActivity中设置要返回FirstActivity的数据,如果您不想返回,请不要设置任何。

例如:在secondActivity中,如果要发回数据:

 Intent returnIntent = new Intent();
 returnIntent.putExtra("result",result);
 setResult(RESULT_OK,returnIntent);     
 finish();

如果您不想返回数据:

Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);        
finish();

现在,在您的FirstActivity类中,为onActivityResult()方法编写以下代码

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  if (requestCode == 1) {

     if(resultCode == RESULT_OK){      
         String result=data.getStringExtra("result");          
     }
     if (resultCode == RESULT_CANCELED) {    
         //Write your code if there's no result
     }
  }
}

希望这有帮助。

答案 1 :(得分:2)

您在startActivityForResult方法中呼叫move()。根据您的情况,您应该使用startActivity(intent),如:

Intent intent = new Intent (MainActivity.this,Second.class); intent.putExtra("value", "this is from first :D ");
        startActivity(i);

请在您的清单中检查您是否应在申请标签

下添加以下代码行 <activity android:name="yourpackagename.Second"> </activity>

希望这对您有所帮助

您也应该看看startActivity()startActivityForResult() herehere

之间的区别

答案 2 :(得分:1)

在第二个练习中,您在为Activity设置布局之前初始化TextView,因此请在setContentView之后将其移动为:

TextView tv2; //<< declare TextView here
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
         tv2 = (TextView)findViewById(R.id.tv2);  // initialize here
          // your code here...
     }

答案 3 :(得分:1)

修复你的第二个活动类。

public class Second extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        Bundle extra = getIntent().getExtras();
        String val1 = extra.getString("value");
        TextView tv2 = (TextView)findViewById(R.id.tv2);
        tv2.setText(val1);
    }
}