0

I have came across a tutorial on how to show data taken from user in one activity to be displayed on another activity and I tried to come up with my own version (from my very limited knowledge).

Here is the code for the first activity which accepts data through

package kk.screen;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class ScreenActivity extends Activity {
/** Called when the activity is first created. */

EditText inputName;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void btnclick(View view) {

    inputName = (EditText)findViewById(R.id.name);
    Intent nextscreen = new Intent(this, newscreen.class);

    //Sending value to the next activity

    nextscreen.putExtra("name",inputName.getText().toString());
    startActivity(nextscreen);
 }
}

And here is the code for the next Activity which should be activated upon the click of the button whose id is "btnclick" :

package kk.screen;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class newscreen extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newscreen);

TextView txtName = (TextView) findViewById(R.id.txtName);

Intent i=getIntent();

//Receiving the data
String name = i.getStringExtra("name");

//Display received data
txtName.setText(name);

 }
}

The problem is that upon the clicking of the button, the app just crashes and I go back to my home screen.

What could be the problem?? Should I be using OnClickListener() ?? (That seems to be the major difference between my approach and the tutorial's approach)

krtkush
  • 1,378
  • 4
  • 23
  • 46

1 Answers1

1

I'm betting you're getting a NoClassDefFoundError because you don't have your 2nd Activity in your AndroidManifest.xml file, see this question:

Add a new activity to the AndroidManifest?

Also, from Google's documentation: "All activities must be represented by elements in the manifest file"

http://developer.android.com/guide/topics/manifest/activity-element.html

Community
  • 1
  • 1
Sam Dozor
  • 40,335
  • 6
  • 42
  • 42