2016-11-23 3 views
0

Es gibt eine EditText und eine Button innerhalb eines Activity nach einigen Text innerhalb eines EditText Eingabe und Button wird eine Methode aufgerufen und übergibt Gehalt an EditText ein Verfahren wie unten:kann den editView-Inhalt nicht an eine Methode übergeben, warum?

public class MainActivity extends AppCompatActivity { 

    TextView txt_title; 
    Button btn_send; 
    EditText edt_notification; 
    public final static String NOTIFICATION_DATA="NOTIFICATION_DATA"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     txt_title= (TextView) findViewById(R.id.txt_title); 
     edt_notification= (EditText) findViewById(R.id.edt_notification); 
     btn_send= (Button) findViewById(R.id.btn_send); 

     btn_send.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       SendNotification(Calendar.getInstance().getTimeInMillis(), edt_notification.getText().toString()); 

      } 
     }); 


    } 

    private void SendNotification(long timeInMillis, String s) { 
     Toast.makeText(this, s, Toast.LENGTH_SHORT).show(); 

    } 

} 


<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="ir.wikichera.badamchi.sendnotification.MainActivity"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/txt_title" 
    android:text="Notification Text:" 
    android:textSize="18sp" 
    android:textStyle="normal|bold" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="textPersonName" 
    android:ems="10" 
    android:layout_below="@+id/txt_title" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="10dp" 
    android:id="@+id/edt_notification" 
    android:hint="put your text here" 
    android:singleLine="false" /> 

<Button 
    android:text="Send" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/edt_notification" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="17dp" 
    android:id="@+id/btn_send" /> 
</RelativeLayout> 

Das Problem ist variabel "s" gibt mir die ganze Zeit "Kann lokale Variable nicht finden" "". Warum? Dies ist ein einfaches Problem, aber ich kann es nicht verstehen. bitte erkläre?

enter image description here

+0

versuchen SendNotification (Calendar.getInstance(). GetTimeInMillis(), MainActivity.this.edt_notification.getText(). ToString()); – mWhitley

+0

können Sie Layout-XML auch einfügen? – NehaK

+0

müssen Sie den Code debuggen und setzen xml –

Antwort

2

Möglicherweise verwenden Sie AndroidStudio, das instant run aktiviert hat. Versuchen Sie es nach dem Ausschalten. Denn wenn es aktiviert ist, finden unsere Codeänderungen manchmal nicht statt. Versuchen Sie es nach dem Ausschalten:

Datei → Einstellungen → Build, Ausführung, Bereitstellung → Instant Run und deaktivieren Sie Instant Run aktivieren.

+0

Vielen Dank, das hat mein Problem gelöst. – Patzu

-1

Mein Arbeits Code:

public class TestAcitivity extends Activity { 
TextView txt_title; 
Button btn_send; 
EditText edt_notification; 
public final static String NOTIFICATION_DATA="NOTIFICATION_DATA"; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_test_acitivity); 

    txt_title= (TextView) findViewById(R.id.txt_title); 
    edt_notification= (EditText) findViewById(R.id.edt_notification); 
    btn_send= (Button) findViewById(R.id.btn_send); 

    btn_send.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      SendNotification(System.currentTimeMillis(), edt_notification.getText().toString()); 

     } 
    }); 


} 

private void SendNotification(long timeInMillis, String s) { 
    txt_title.setText(s); 
    Toast.makeText(TestAcitivity.this, s, Toast.LENGTH_SHORT).show(); 

} 

}

dies für mich gearbeitet.

+0

Total falsche Antwort! – Piyush

+0

@Piyush bitte sagen was falsch ist. – Varma460

+0

Es handelt sich nicht um ein Kontextreferenzproblem. –

Verwandte Themen