2017-12-26 8 views
1

Nach dem Importieren von Ansicht und Schaltfläche. Ich habe ein Objekt für Button erstellt, in diesem Fall "thomasButton".Android Studio, Schaltfläche Objekt nicht erkannt,

Aber Fehler angegeben, das Feld ‚thomasButton“ nicht verwendet wird, obwohl ich es in der nächsten Zeile genannt habe.

Nach einer Weile fand ich, dass das Feld, wenn ich es in einem anderen Rahmen setzte erkannt werden kann. Wie dies, aber immer noch das Programm wont (Absturz beim Start) laufen Haben Sie Jungs wissen, was ist der richtige Weg für eine Schaltfläche setOnLongClickListener

package com.example.thoma.event; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

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

public void displayMessage(View v){ 
    TextView thomasText = (TextView)findViewById(R.id.txt_View); 
    thomasText.setText(R.string.rsc_Text2);x 
} 

Button thomasButton = (Button)findViewById(R.id.btn_Change); 
// weird but I can only use Button object within an inner scope 
{ 
    thomasButton.setOnLongClickListener(new View.OnLongClickListener() { 
     public boolean onLongClick(View v) { 
      TextView thomasText = (TextView) findViewById(R.id.txt_View); 
      thomasText.setText("Artificial"); 
      // it will return false if long click wasnt long enough 
      // and normal click will be called 
      return true; 
     } 
    }); 
} 
} 

Crash:.? Leider hat App

+0

können Sie den gesamten Code teilen? – R2R

+0

Was ist der Absturz? – AbhayBohra

+0

Teilen Vollständiger Code –

Antwort

2

Taste initialisieren in gestoppt onCreate() überprüfen bel OW-Code. findViewById ist teuer, so wird es besser sein, wenn Sie diese ids so minimal wie möglich

package com.example.thoma.event; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button thomasButton = (Button)findViewById(R.id.btn_Change); 

    TextView thomasText = (TextView) findViewById(R.id.txt_View); 

    thomasButton.setOnLongClickListener(new View.OnLongClickListener() { 
     public boolean onLongClick(View v) { 
      thomasText.setText("Artificial"); 

      // it will return false if long click wasnt long enough 
      // and normal click will be called 

      return true; 
     } 
    }); 

} 

public void displayMessage(View v){ 
    TextView thomasText = (TextView)findViewById(R.id.txt_View); 
    thomasText.setText(R.string.rsc_Text2); 
} 


} 
+1

Vielen Dank !! es löste das Problem! –

+0

glücklich, Ihnen zu helfen :) – Omi

0

Versuchen Sie finden diese

import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.TextView; 

    public class MainActivity extends AppCompatActivity { 

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

     Button thomasButton = (Button)findViewById(R.id.btn_Change); 
     thomasButton.setOnLongClickListener(new View.OnLongClickListener() { 
       @Override 
       public boolean onLongClick(View v) { 
        TextView thomasText = (TextView) findViewById(R.id.txt_View); 
        thomasText.setText("Artificial"); 
        return false; 
       } 
      }); 
    } 


    } 
+0

Vielen Dank für Ihre Zeit auch R2R! –

1
package com.example.thoma.event; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 
TextView thomasText; 
Button thomasButton; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    thomasText = (TextView)findViewById(R.id.txt_View); 
     thomasButton = (Button)findViewById(R.id.btn_Change); 
     thomasButton.setOnLongClickListener(new View.OnLongClickListener() { 
     public boolean onLongClick(View v) { 
      thomasText.setText("Artificial"); 
      // it will return false if long click wasnt long enough 
      // and normal click will be called 
      return true; 
     } 
    }); 

} 

public void displayMessage(View v){ 
    thomasText.setText(R.string.rsc_Text2); 
} 



} 
Verwandte Themen