Android:按钮无法打开新活动?

时间:2014-07-31 16:02:13

标签: android android-intent

在我的应用程序中,我设置了从菜单屏幕打开特定活动的意图。

每个按钮都会打开一个新的活动。它们都是按照一个按钮的方式工作,单击时没有做任何事情。

我做错了什么?我查了一下:

-manifest

-xml

- 双活动

我看不出问题。此外,没有logcat错误。

完整菜单活动:

package com.example.brianapp;

import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View.OnClickListener;

public class Search extends ActionBarActivity implements View.OnClickListener {

    TextView instruction;
    Button date;
    Button game;
    Button med;
    Button att;
    Button score;

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

        // setting up vars
        instruction = (TextView) findViewById(R.id.tvSearchHome);
        date = (Button) findViewById(R.id.btnSearchHomeDate);
        game = (Button) findViewById(R.id.btnSearchHomeGame);
        med = (Button) findViewById(R.id.btnSearchHomeMedValues);
        att = (Button) findViewById(R.id.btnSearchHomeAttValues);
        game = (Button) findViewById(R.id.btnSearchHomeScore);

        // set on click listeners for the buttons
        date.setOnClickListener(this);
        game.setOnClickListener(this);
        med.setOnClickListener(this);
        att.setOnClickListener(this);
        game.setOnClickListener(this);

    }

    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.btnSearchHomeDate:

            Intent openDateSearch = new Intent(
                    "com.example.brianapp.SearchDate");
            // Start activity
            startActivity(openDateSearch);

            break;

        case R.id.btnSearchHomeGame:

            // change this to make sure it opens the med screen
            Intent openGameSearch = new Intent(
                                "com.example.brianapp.SearchGame");
            // Start activity
            startActivity(openGameSearch);

            break;  

        case R.id.btnSearchHomeMedValues:

            // change this to make sure it opens the med screen
            Intent openMedSearch = new Intent(
                    "com.example.brianapp.MeditationSearchHome");
            // Start activity
            startActivity(openMedSearch);

            break;

        case R.id.btnSearchHomeAttValues:

            // change this to make sure it opens the med screen
            Intent openAttSearch = new Intent("com.example.brianapp.AttentionSearchHome");
            // Start activity
            startActivity(openAttSearch);
            break;

        case R.id.btnSearchHomeScore:

            // change this to make sure it opens the med screen
            Intent openScoreSearch = new Intent(
                    "com.example.brianapp.SearchScore");
            // Start activity
            startActivity(openScoreSearch);

            break;

        }// switch end

    }

}

具体案例说明工作:

case R.id.btnSearchHomeGame:

                // change this to make sure it opens the med screen
                Intent openGameSearch = new Intent(
                                    "com.example.brianapp.SearchGame");
                // Start activity
                startActivity(openGameSearch);

                break;  

按钮应该打开的活动:

package com.example.brianapp;

import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.method.ScrollingMovementMethod;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;
import android.content.Intent;

public class SearchGame extends ActionBarActivity implements View.OnClickListener {

    // declare vars
    TextView instruction;
    TextView tvResults;
    DatabaseHelper db = new DatabaseHelper(this);
    Button maths;
    Button memory;
    Button stroop;
    String log;
    List<Score> results;

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

        // set up vars
        instruction = (TextView) findViewById(R.id.tvSearchGame1);
        tvResults = (TextView) findViewById(R.id.tvSearchGameResults1);
        tvResults.setMovementMethod(new ScrollingMovementMethod());
        maths = (Button) findViewById(R.id.btnSearchGameMaths);
        memory = (Button) findViewById(R.id.btnSearchGameMemory);
        stroop = (Button) findViewById(R.id.btnSearchGameStroop);

        // set on click listener for btns
        maths.setOnClickListener(this);
        memory.setOnClickListener(this);
        stroop.setOnClickListener(this);
    }

    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.btnSearchGameMaths:

            // just change the expression below
            results = db.getMathsGame();

            // showing all the scores in the database that match the query in
            // Logcat (Trace code)
            for (Score s : results) {
                log = " Name: " + s.getName() + " Meditation: "
                        + s.getMeditation() + " Max: " + s.getMax() + "Score: "
                        + s.getScore() + " Date: " + s.getDate();

                // Writing Contacts to log
                Log.d("Details: ", log);

                // hides the keyboard from screen once btn is pressed
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(getCurrentFocus()
                        .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

            }

            tvResults.setText(listToString(results));

            break;

        case R.id.btnSearchGameMemory:


            //results = db.getSpecificAverageAtt(avgValToSearch);

            // showing all the scores in the database that match the query in
            // Logcat (Trace code)
            for (Score s : results) {
                log = " Name: " + s.getName() + " Meditation: "
                        + s.getMeditation() + " Max: " + s.getMax() + "Score: "
                        + s.getScore() + " Date: " + s.getDate();

                // Writing Contacts to log
                Log.d("Details: ", log);

                // hides the keyboard from screen once btn is pressed
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(getCurrentFocus()
                        .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

            }

            tvResults.setText(listToString(results));

            break;

        case R.id.btnSearchGameStroop:


            //results = db.getSpecificAverageAtt(avgValToSearch);

            // showing all the scores in the database that match the query in
            // Logcat (Trace code)
            for (Score s : results) {
                log = " Name: " + s.getName() + " Meditation: "
                        + s.getMeditation() + " Max: " + s.getMax() + "Score: "
                        + s.getScore() + " Date: " + s.getDate();

                // Writing Contacts to log
                Log.d("Details: ", log);

                // hides the keyboard from screen once btn is pressed
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(getCurrentFocus()
                        .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

            }

            tvResults.setText(listToString(results));

        }
    }//end of onClick()


    /**
     * Method used to format the output of a List
     * so it is more presentable to the user
     * @param list
     * @return
     */
    public static String listToString(List<?> list) {
        String result = "";
        for (int i = 0; i < list.size(); i++) {
            result += "" + list.get(i) + "\n\n";
        }
        return result;
    }

}

Manifest的相应部分:

<activity
            android:name="com.example.brianapp.Search"
            android:label="@string/title_activity_search" >
            <intent-filter>
                <action android:name="com.example.brianapp.Search" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>


            <activity
                android:name="com.example.brianapp.SearchGame"
                android:label="@string/title_activity_search_game" >
                <intent-filter>
                    <action android:name="com.example.brianapp.SearchGame" />

                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

2 个答案:

答案 0 :(得分:2)

你覆盖游戏按钮的参考:

 game = (Button) findViewById(R.id.btnSearchHomeGame);

 game = (Button) findViewById(R.id.btnSearchHomeScore);

你应该把它改成:

 game = (Button) findViewById(R.id.btnSearchHomeGame);

 score= (Button) findViewById(R.id.btnSearchHomeScore);

答案 1 :(得分:0)

改变这个:

Intent openGameSearch = new Intent("com.example.brianapp.SearchGame");

到此:

Intent openGameSearch = new Intent(this,
                                   com.example.brianapp.SearchGame.class);