2017-03-26 1 views
0

ich alsderzeit keine Schaltfläche klicken zum Laufen bringen

meine Java-Datei haben
import android.content.Context; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 

public class ThemesActivity extends AppCompatActivity{ 
SharedPreferences sharedPreferences; 

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

@Override 
protected void onStart() { 
    super.onStart(); 
    setContentView(R.layout.activity_themes); 
    sharedPreferences = getApplicationContext(). 
getSharedPreferences("MyNumbers", Context.MODE_PRIVATE); 
    int bg = sharedPreferences.getInt("background_resource", 
R.color.backYellow); 
    getWindow().setBackgroundDrawableResource(bg); 
} 

public void yellowClick(View view){ 
    sharedPreferences = getApplicationContext(). 
getSharedPreferences("MyNumbers", Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 
    editor.putInt("background_resource", R.color.backYellow); 
    editor.putInt("button_resource", R.color.buttonYellow); 
    editor.apply(); 
    getWindow().setBackgroundDrawableResource(R.color.backYellow); 
} 

public void redClick(View view){ 
    sharedPreferences = getApplicationContext(). 
getSharedPreferences("MyNumbers", Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 
    editor.putInt("background_resource", R.color.backRed); 
    editor.putInt("button_resource", R.color.buttonRed); 
    editor.apply(); 
    getWindow().setBackgroundDrawableResource(R.color.backRed); 
} 

public void blueClick(View view){ 
    sharedPreferences =  getApplicationContext(). 
getSharedPreferences("MyNumbers", Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 
    editor.putInt("background_resource", R.color.backBlue); 
    editor.putInt("button_resource", R.color.buttonBlue); 
    editor.apply(); 
    getWindow().setBackgroundDrawableResource(R.color.backBlue); 
} 

public void greenClick(View view){ 
    sharedPreferences = getApplicationContext(). 
getSharedPreferences("MyNumbers", Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 
    editor.putInt("background_resource", R.color.backGreen); 
    editor.putInt("button_resource", R.color.buttonGreen); 
    editor.apply(); 
    getWindow().setBackgroundDrawableResource(R.color.backGreen); 
} 

} 

und Ressource-Datei wie

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@android:color/holo_orange_light" 
android:gravity="center_horizontal" 
android:orientation="vertical" 
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="com.example.dhritiman.any.ThemesActivity"> 

<!-- Login progress --> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:id="@+id/linearLayout2"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin"> 

     <Button 
      android:id="@+id/blueButton" 
      android:layout_width="match_parent" 
      android:layout_height="100dp" 
      android:layout_weight="1" 
      android:background="@android:color/holo_blue_dark" 
      android:onClick="blueClick" 
      android:text="Blue" 
      android:visibility="visible" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin"> 

     <Button 
      android:id="@+id/yellowButton" 
      android:layout_width="match_parent" 
      android:layout_height="100dp" 
      android:layout_weight="1" 
      android:background="@android:color/holo_orange_dark" 
      android:onClick="yellowClick" 
      android:text="Yellow" 
      android:visibility="visible" /> 
    </LinearLayout> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:orientation="horizontal" 
    android:layout_below="@+id/linearLayout2" 
    android:layout_alignParentStart="true"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin"> 

     <Button 
      android:id="@+id/redButton" 
      android:layout_width="match_parent" 
      android:layout_height="100dp" 
      android:layout_weight="1" 
      android:background="@android:color/holo_red_dark" 
      android:contextClickable="false" 
      android:linksClickable="false" 
      android:onClick="redClick" 
      android:text="Red" 
      android:visibility="visible" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin"> 

     <Button 
      android:id="@+id/greenButton" 
      android:layout_width="match_parent" 
      android:layout_height="100dp" 
      android:layout_weight="1" 
      android:background="@android:color/holo_green_dark" 
      android:onClick="greenClick" 
      android:text="Green" 
      android:visibility="visible" /> 
    </LinearLayout> 

</LinearLayout> 

</RelativeLayout> 

Bitte helfen Sie, meine logcat ist Prozess Zeiger 0 und Prozess Zeiger 1 I zeigt bedeutet, dass es nur Touch-Eingabe und nicht Knopfklick-Eingabe dauert, wenn die Schaltfläche geklickt wird Ich habe alles überprüft, aber nicht in der Lage, das Problem zu finden. Vielen Dank im Voraus

+0

Sie haben keinen Verweis auf den Button selbst - Sie müssen etwas hinzufügen - 'Button btn = findViewById (R.id.Ihr_Button_Name);'. Und warum benutzt man 'onStart()' und legt nicht den ganzen Code auf 'onCreate()'? – TDG

+0

Ihre Frage ist nicht sehr klar, aber wenn ich Ihnen richtig folge, entfernen Sie 'android: background =" @ android: color/holo_orange_light "' aus dem '' in Ihrem Layout-XML. –

Antwort

0

Stellen Sie sicher, dass Sie Ihre Ansichten und Layouts in onCreate() initialisieren, die bei der Erstellung der Aktivität aufgerufen wird, onStart() wird aufgerufen, wenn die Aktivität für den Benutzer sichtbar ist.

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_themes); 
    // add here 
    sharedPreferences = getApplicationContext().getSharedPreferences("MyNumbers", Context.MODE_PRIVATE); 
    int bg = sharedPreferences.getInt("background_resource",R.color.backYellow); 
    getWindow().setBackgroundDrawableResource(bg); 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    // move to onCreate 
} 
+0

kannst du mir sagen, was meinst du damit, meine Ansichten zu initialisieren? –

+0

setzen Sie Ihre Layout-Datei in 'onCreate()'! – W4R10CK

0

Ihre Onclick wird nur Log jede custome Nachricht auf, dass gut funktionieren, aber u einfach Ihre setContentView (R.layout.activity_themes) innerhalb onCreate als Code Arbeit initialisieren jetzt auch, aber init alle View-Komponente innerhalb onCreate initialisieren sollte Methode

Vielen Dank!

Verwandte Themen