从首选项菜单项启动活动不起作用

时间:2017-10-22 17:20:17

标签: java android

尝试制作初级水平数独游戏

使用Android Studio 2.3.3,API 25

Game的主要活动java文件(GameActivity.java)

package oscorp.sudoku;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;

public class GameActivity extends AppCompatActivity implements OnClickListener {

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

    View continueButton = findViewById(R.id.continueButton);
    continueButton.setOnClickListener(this);

    View newGameButton = findViewById(R.id.newGameButton);
    newGameButton.setOnClickListener(this);

    View aboutButton = findViewById(R.id.aboutButton);
    aboutButton.setOnClickListener(this);

    View exitButton = findViewById(R.id.exitButton);
    exitButton.setOnClickListener(this);

}

public void onClick(View v){
        switch (v.getId()){
            case R.id.aboutButton:
                Intent i = new Intent(this, About.class);
                startActivity(i);
                break;

            case R.id.newGameButton:
                openNewGameDialog();
                break;

        }
}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}


public boolean onCreateItemSelected(MenuItem item){
    switch (item.getItemId()){
        case R.id.settings:
            startActivity(new Intent(this, Prefs.class));
            return true;
    }
    return false;
}


private static final String TAG = "Sudoku";

public void openNewGameDialog(){

    new AlertDialog.Builder(this).setTitle(R.string.new_game_title)
            .setItems(R.array.difficulty, new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialogInterface, int i){
                    startGame(i);
                }
            });

}

private void startGame(int i){
    Log.d(TAG, "Clicked on" + i);
}

}

游戏布局(activity_game.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="40dp"
android:background="@color/colorPrimaryDark">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:background="@color/colorPrimaryDark">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/title_text"
        android:layout_marginBottom="40dp"
        android:textSize="30sp"
        android:textColor="@color/colorAccent"/>
    <Button
        android:id="@+id/continueButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/continueButton_text"/>
    <Button
        android:id="@+id/newGameButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/new_gameButton_text"/>
    <Button
        android:id="@+id/aboutButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/aboutButton_text"/>
    <Button
        android:id="@+id/exitButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/exitButton_text"/>

</LinearLayout>

</LinearLayout>

Game的Pref文件(Prefs.java)

package oscorp.sudoku;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;

public class Prefs extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}

public static class MyPreferenceFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
    }

}

}

settings.xml文件

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

<CheckBoxPreference
    android:key="music"
    android:title="@string/music_title"
    android:summary="@string/music_summary"
    android:defaultValue="true" />

<CheckBoxPreference
    android:key="hints"
    android:title="@string/hints_title"
    android:summary="@string/hints_summary"
    android:defaultValue="true" />

</PreferenceScreen>

选择菜单项设置时要显示的array.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="difficulty">
    <item>@string/easy_label</item>
    <item>@string/medium_label</item>
    <item>@string/hard_label</item>
</array>
</resources>

我已经上传了所有必要的文件。 当我进入具有设置选项的溢出菜单并且在选择该选项时没有发生任何事情时测试应用程序时,settings.xml文件应该被充气并启动。

同样在点击“新游戏”按钮时,没有发生任何事情,但应该膨胀并启动array.xml文件。

Android Studio没有显示任何错误或警告,但GameActivity.java文件中的onOptionsItemSelected()方法有一个未使用的方法警报。

不知道如何解决这个问题。

0 个答案:

没有答案