按按钮转到新屏幕

时间:2014-04-20 19:52:49

标签: android

这是我第一次在Android上开发。我试图创建一个按钮,按下时应该打开一个新屏幕。我尝试了Android教程,并阅读了本网站上的一些问题,但我还在努力。对不起,如果问题对他人来说是多余的。

到目前为止我得到了这个:

MainActivity.java

package com.example.squashbot;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {


        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }

        public void scoreGame (View view)
        {
            Intent intent = new Intent(getActivity(), ScoreNewGame.class);

            startActivity(intent);
        }
    }

}

fragment_main.xml

<LinearLayout 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:orientation="horizontal" >

    <Button 
        android:id="@+id/score_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/score_button"
        android:onClick="scoreGame"/>

</LinearLayout>

ScoreNewGame.java

package com.example.squashbot;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class ScoreNewGame extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        setContentView(R.layout.activity_score_new_game);

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_score_new_game,
                    container, false);
            return rootView;
        }
    }

}

fragment_score_new_game.xml

<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:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

我对此很新,所以要温柔。 Thanx提前获得任何帮助,非常感谢

2 个答案:

答案 0 :(得分:1)

如果按下按钮,应用程序会崩溃?然后,它是因为方法得分游戏android:onClick="scoreGame"属性(在Button fragment_main.xml 必须到附加到您的活动而不是Fragment,如下所示:

    //...

    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }

    }
    // end of PlaceholderFragment

    public void scoreGame (View view) {
        Intent intent = new Intent(this, ScoreNewGame.class);
        startActivity(intent);
    }

}
// end of MainActivity  

注意:你应该发布堆栈跟踪,它会更好地帮助你。希望这会有所帮助。

答案 1 :(得分:0)

您的代码看起来一切正常,请检查您是否在根文件夹中的AndriodManifest文件中注册了fragment_score_new_game主机活动。看一下这段代码

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