2017-01-17 5 views
1

So versuche ich eine zweite Aktivität von der ersten Aktivität von einem Klick auf eine Schaltfläche zu öffnen. Die App wird geladen, aber wenn ich versuche, den Knopf zu drücken, um zur nächsten Aktivität zu gehen, passiert nichts. Hier sind meine main.xml, mainactivity.java, androidmanifest.xml, shopactivity.java und shop.xml. Jede Hilfe wird sehr geschätzt.Erfolglose Eröffnung zweite Aktivität

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:text="@string/counter" 
     android:textColor="#000000"/> 
    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView1" 
     android:text="@string/gems" 
     android:textColor="#000000"/> 
    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/textView1" 
     android:layout_alignBaseline="@+id/textView1" 
     android:clickable="false" 
     android:cursorVisible="false" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:textColor="#000000"/> 
    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/textView2" 
     android:layout_alignBaseline="@+id/textView2" 
     android:clickable="false" 
     android:cursorVisible="false" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:textColor="#000000"/> 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="310dp" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:background="#000000" 
     android:text="@string/button" 
     android:textColor="#ffffff"/> 
    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button1" 
     android:background="#ffffff" 
     android:text="@string/button2" 
     android:textColor="#000000" 
     android:onClick="onClickShop"/> 
    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button1" 
     android:layout_alignParentRight="true" 
     android:background="#ffffff" 
     android:text="@string/button3" 
     android:textColor="#000000"/> 
    <Button 
     android:id="@+id/button4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button1" 
     android:layout_centerHorizontal="true" 
     android:layout_toRightOf="@+id/button2" 
     android:layout_toLeftOf="@+id/button3" 
     android:background="#ffffff" 
     android:text="@string/button4" 
     android:textColor="#000000"/> 
</RelativeLayout> 

MainActivity.java

public class MainActivity extends Activity implements OnClickListener{ 

    TextView textView1; 
    TextView textView2; 
    EditText editText1; 
    EditText editText2; 
    Button button1; 
    Button button2; 
    Button button3; 
    Button button4; 
    int counter = 0; 
    int gems = 0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
     {super.onCreate(savedInstanceState); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     setContentView(R.layout.main); 

    textView1 = (TextView)findViewById(R.id.textView1); 
    textView2 = (TextView)findViewById(R.id.textView2); 
    editText1 = (EditText)findViewById(R.id.editText1); 
    editText2 = (EditText)findViewById(R.id.editText2); 
    button1 = (Button)findViewById(R.id.button1); 
    button2 = (Button)findViewById(R.id.button2); 
    button3 = (Button)findViewById(R.id.button3); 
    button4 = (Button)findViewById(R.id.button4); 
    button1.setOnClickListener(this); 
    button2.setOnClickListener(this); 
    button3.setOnClickListener(this); 
    button4.setOnClickListener(this);} 

    @Override 
    protected void onStop() 
     {super.onStop(); 
     SharedPreferences prefs = this.getSharedPreferences("com.bugagullgames.clicker", Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = prefs.edit(); editor.putInt("key", counter); editor.commit();} 

    @Override 
    protected void onStart() 
     {super.onStart(); SharedPreferences prefs = this.getSharedPreferences("com.bugagullgames.clicker", Context.MODE_PRIVATE); 
     int defaultValue = 0; 
     counter = prefs.getInt("key", defaultValue); 
     editText1.setText(Integer.toString(counter));} 

    @Override 
    public void onClick(View v) 
     {if (v == button1) 
     {counter++;  editText1.setText(Integer.toString(counter));}} 

    public void onClickShop(View v) 
     {if (v == button2) 
    { 
     Intent intent = new Intent(MainActivity.this, ShopActivity.class); 
     startActivity(intent); 
    }}} 

<?xml version="1.0" encoding="utf-8"?> 
<manifest  xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.bugagullgames.clicker" > 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action  android:name="android.intent.action.MAIN" /> 

       <category  android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".ShopActivity" 
      android:label="@string/app_name">   
     </activity> 
    </application> 

</manifest> 

public class ShopActivity extends MainActivity 
{ 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.shop); 
}} 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:text="Welcome to the shop" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true"/> 

</RelativeLayout> 

Antwort

0

hallo @Bugagull Ihre "ShopActivity.class" in Ihrem menifest definieren.

<activity 
     android:name=".ShopActivity" 
     android:label="@string/app_name"> 

</activity> 

und ersetzen Sie diese.

public class ShopActivity extends Activity 
    { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.shop); 
     } 
} 
+0

Wenn Sie möchten, dann akzeptieren Sie es. @ BugMag –

+0

Ich habe es gerade in meinem Manifest definiert. Leider hat es mein Problem nicht gelöst. – Bugagull

+0

nach dem Hinzufügen, welches Problem Sie konfrontiert? Können Sie Fehler senden? –

4

Sie das Überschreiben der button2 Onclick Zuhörer. Entfernen Sie diese Codezeile:

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

Und die xml onclick Eigenschaft, die Sie android:onClick="onClickShop" funktionieren sollte konfiguriert.

+0

Diese Antwort löste mein Problem, danke. – Bugagull

0

Wenn Sie eine XML-Methode verwenden möchten.

Ihre Deklarationsmethode muss so sein. public void Methodenname (View v)

ist es wichtig, dieses Argument zu setzen.

0

überschreiben die Schaltfläche2 onclick Listener. Programmatisch diese Show;

button2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       onClickShop(v); 
      } 
     }); 
0

ersetzen {if (v == button2) für {if (v == button2.getView()) oder versuchen von ID zu finden. Ich empfehle Verwendung:

button2.setOnclickListener(new OnClickListener { 

    // TO DO 

}) 
Verwandte Themen