2013-01-06 1 views
0

Je suis en train de coder un programme androïde qui organise trois mots par ordre alphabétique en utilisant deux activités simples. Mais dans la deuxième activité (sortie), soit le TextView ou les intentions ne fonctionnent pas.La sortie ne s'affiche pas. Le code java pour la première activité (entrée) est le suivant: `package com.example.names;Pourquoi le TextView ne fonctionne-t-il pas?

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class Input extends Activity { 
EditText t1,t2,t3; 
final Context context=this; 

String n[]=new String[100]; 
String t[]=new String[100]; 

String a1,a2,a3; 
Button alpha; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.input); 
    t1=(EditText)findViewById(R.id.editText1); 
    t2=(EditText)findViewById(R.id.editText2); 
    t3=(EditText)findViewById(R.id.editText3); 
n[0]=t1.getText().toString(); 
n[1]=t2.getText().toString(); 
n[2]=t3.getText().toString(); 
for(int j=0;j<2;j++) 
    { 
     for(int k=j+1;k<3;k++) 
    { 
    if(n[j].compareToIgnoreCase(n[k])>0) 
     { 
     String temp=n[j]; 
     n[j]=n[k]; 
     n[k]=temp; 
     } 
    } 
    } 
    a1=n[0]; 
    a2=n[1]; 
    a3=n[2]; 
    alpha=(Button)findViewById(R.id.button1); 
    alpha.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Intent i=new Intent(Input.this,Output.class); 
     i.putExtra("x", a1+a2+a3); 

      Toast.makeText(Input.this, "Method OnClick has been invoked",Toast.LENGTH_SHORT).show(); 
      startActivity(i);  
    } 
});}}` 

Et le code java pour la deuxième activité est la suivante:

package com.example.names; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Output extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    TextView p1=new TextView(this); 

    Bundle extras=getIntent().getExtras(); 

    if (extras!=null){ 
String ans=extras.getString("x"); 
    p1.setText(ans); 
    p1.setTextSize(50); 
    Toast.makeText(Output.this, "All lines have been executed",Toast.LENGTH_LONG).show(); 
    setContentView(p1); 

}};} 

Pourquoi pas la sortie s'affiche alors ??? L'application ne plante pas également.

Répondre

0

Changez votre code avec ce code ci-dessous.

Récupère la valeur d'edittext quand on clique sur le bouton.

Votre activité d'entrée ressemble à ceci.

public class Input extends Activity { 
    EditText t1,t2,t3; 
    final Context context=this; 

    String n[]=new String[100]; 
    String t[]=new String[100]; 

    String a1,a2,a3; 
    Button alpha; 

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

     t1=(EditText)findViewById(R.id.editText1); 
     t2=(EditText)findViewById(R.id.editText2); 
     t3=(EditText)findViewById(R.id.editText3); 


     alpha=(Button)findViewById(R.id.button1); 
     alpha.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      n[0]=t1.getText().toString(); 
      n[1]=t2.getText().toString(); 
      n[2]=t3.getText().toString(); 
      for(int j=0;j<2;j++) 
       { 
        for(int k=j+1;k<3;k++) 
       { 
       if(n[j].compareToIgnoreCase(n[k])>0) 
        { 
        String temp=n[j]; 
        n[j]=n[k]; 
        n[k]=temp; 


        } 
       } 
       } 
       a1=n[0]; 
       a2=n[1]; 
       a3=n[2]; 
      Intent i=new Intent(Input.this,Output.class); 
      i.putExtra("x", a1+a2+a3); 

       Toast.makeText(Input.this, "Method OnClick has been invoked",Toast.LENGTH_SHORT).show(); 
       startActivity(i);  
     } 
    });}} 

Votre activité de sortie est correcte, elle ne fonctionne pas dans cette activité.

+0

Accepter la réponse si utile .. Je pense que cela fonctionnera bien. –

Questions connexes