Tuesday, February 25, 2014

Why point BUTTON Button program suddenly shut down?


This is the first Main.java
 package com.example.task2; 

import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity {

private static final String TAG = "Main";
protected TextView editText1;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
Log.v (TAG, "onCreate");

Button button1 = (Button) findViewById (R.id.button1);

button1.setOnClickListener (new OnClickListener () {
public void onClick (View v)
{
String a = editText1.getText () toString ();.
Bundle bundle = new Bundle ();
Intent intent = new Intent ();
bundle.putString ("a", a);
intent.putExtras (bundle);
intent.setClass (Main.this, OtherActivity.class);

startActivity (intent);
}

});

}

}


This is the second OtherActivity.java

 package com.example.task2; 

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;

public class OtherActivity extends Activity {

private static final String TAG = "OtherActivity";
private TextView textView;
public void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.other);
Log.v (TAG, "onCreate");

Intent intent = getIntent ();
Bundle bundle = intent.getExtras ();
String str = bundle.getString ("a");
textView = (TextView) findViewById (R.id.three);
textView.setText (str);

Button button = (Button) findViewById (R.id.button2);
button.setOnClickListener (new Button.OnClickListener ()
{

@ Override
public void onClick (View v) {
/ / TODO Auto-generated method stub
Intent intent = new Intent ();
intent.setClass (OtherActivity.this, Main.class);
startActivity (intent);

}

});

}

}


xml file is correct<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
The first class editText1 the landlord has no value, and see what you mean type should be EditText, rather than TextView
 private EditText editText1; 

final EditText editText1 = (EditText) findViewById (R.id.editText1);

Reply:
To correct:
 private EditText editText1; 
editText1 = (EditText) findViewById (R.id.editText1);


Reply:
reference to the second floor ncepu307 reply:
to correct:
 private EditText editText1; 
editText1 = (EditText) findViewById (R.id.editText1);


Thank you very much, did not tune out tune for a long time, and now finally the way, thank you

No comments:

Post a Comment