2017-01-17 3 views
1

ボタンをクリックして最初のアクティビティから2番目のアクティビティを開こうとしています。アプリケーションが読み込まれますが、次にボタンを押して次のアクティビティに移動しようとすると何も起こりません。ここにmain.xml、mainactivity.java、androidmanifest.xml、shopactivity.java、shop.xmlがあります。どんな助けでも大歓迎です。オープニング第2アクティビティが失敗しました

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> 

答えて

0

こんにちはあなたmenifestであなたの "ShopActivity.class" を定義@Bugagull: は、私が使用することをお勧めします。

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

</activity> 

と置き換えてください。

+0

を受け入れてください。@ Bugagull –

+0

私はマニフェストでこれを定義しました。残念ながら、それは私の問題を解決しませんでした。 – Bugagull

+0

あなたは直面している問題を追加した後にエラーを送信できますか? –

4

あなたはbutton2をonclickのリスナーをオーバーライドしています。

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

そして、あなたはandroid:onClick="onClickShop"が動作するはず構成されたXML onclickプロパティ:このコード行を削除します。

+0

この回答は私の問題を解決しました。ありがとうございます。あなたが好きなら、 – Bugagull

0

xmlメソッドを使用する場合は、

あなたの宣言方法は、そのようにする必要があります。 public void methodname(vを参照)

この引数は重要です。

0

ボタン2をクリックしてリスナーをオーバーライドします。プログラム的なこのショー;

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

{if (v == button2.getView())ため{if (v == button2)を交換するか、IDで検索してみてください。

button2.setOnclickListener(new OnClickListener { 

    // TO DO 

}) 
関連する問題