Android Studio 中的新活动无法启动

Posted

技术标签:

【中文标题】Android Studio 中的新活动无法启动【英文标题】:New activity in Android Studio wont start 【发布时间】:2016-01-05 04:09:03 【问题描述】:

在创建我的应用程序时,我遇到了一个问题,即在更改为第二个活动后,我似乎无法更改为第三个。我尝试将函数一直修改为对活动的简单调用;但是我仍然遇到可以单击模拟器中的按钮但没有任何反应的问题。想法?

Java:

public class NewGameActivity extends AppCompatActivity 

    public Spinner players, bestOf;
    public Button startButton;

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

        players = (Spinner) findViewById(R.id.players);
            Integer[] items1 = new Integer[]2,3,4,5,6,7,8,9,10;
            ArrayAdapter<Integer> adapter1 = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item, items1);
            players.setAdapter(adapter1);

        bestOf = (Spinner) findViewById(R.id.bestOf);
            Integer[] items2 = new Integer[]1,3,5,7,9,11,13,15,17,19,21,23,25;
            ArrayAdapter<Integer> adapter2 = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item, items2);
            bestOf.setAdapter(adapter2);

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

        startButton.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View v) 

            
        );
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_new_game, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) 
            return true;
        

        return super.onOptionsItemSelected(item);
    

    public void sendMessageStart(View view) 
        int playerNum = Integer.valueOf(players.getSelectedItem().toString());
        int games = Integer.valueOf(bestOf.getSelectedItem().toString());

        RadioGroup group;
        group = (RadioGroup) findViewById(R.id.group);

        int selectedId = group.getCheckedRadioButtonId();

        Bundle bundle = new Bundle();
        bundle.putInt("players", playerNum);
        bundle.putInt("games", games);

        if (selectedId == 0) 
            Intent intent = new Intent(this, ThreeSignGameActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        
        else 
            Intent intent = new Intent(this, FiveSignGameActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        

    

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_
android:layout_ android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="royrowland.zerosum.NewGameActivity"
android:background="#0169B2">

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/newGamePlayers"
    android:id="@+id/textView4"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    android:layout_marginTop="30dp"
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:typeface="sans"
    android:textSize="40sp"
    />

<Spinner
    android:layout_
    android:layout_
    android:id="@+id/players"
    android:layout_alignTop="@+id/textView4"
    android:layout_toRightOf="@+id/textView4"
    android:layout_toEndOf="@+id/textView4"
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:typeface="sans"/>

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/newGameStyle"
    android:id="@+id/textView6"
    android:layout_marginTop="60dp"
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:typeface="sans"
    android:textSize="40sp"
    android:layout_below="@+id/textView4"
    android:layout_alignLeft="@+id/textView4"
    android:layout_alignStart="@+id/textView4" />

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="horizontal"
    android:layout_below="@+id/textView6"
    android:layout_centerHorizontal="true"
    android:checkedButton="@+id/radio3"
    android:id="@+id/group">
    <RadioButton
        android:layout_
        android:layout_
        android:text="@string/radio3"
        android:id="@+id/radio3"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:typeface="sans"
        android:textSize="20sp"/>

    <RadioButton
        android:layout_
        android:layout_
        android:text="@string/radio5"
        android:id="@+id/radio5"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:typeface="sans"
        android:textSize="20sp" />
</RadioGroup>

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/newGameBestOf"
    android:id="@+id/textView7"
    android:layout_below="@+id/textView6"
    android:layout_alignLeft="@+id/textView6"
    android:layout_alignStart="@+id/textView6"
    android:layout_marginTop="80dp"
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:typeface="sans"
    android:textSize="40sp"/>

<Spinner
    android:layout_
    android:layout_
    android:id="@+id/bestOf"
    android:layout_alignTop="@+id/textView7"
    android:layout_toRightOf="@+id/textView7"
    android:layout_toEndOf="@+id/textView7"
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:typeface="sans"
    />

<Button
    android:layout_
    android:layout_
    android:text="@string/newGameButtonText"
    android:id="@+id/startButton"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:typeface="sans"
    android:textSize="30sp"
    android:onClick="sendMessageStart"/>

【问题讨论】:

请粘贴你的logcat 但是你的 onClick(View v) 方法是空的 【参考方案1】:

XML 包含 android:onClick="sendMessageStart" 并且您已经在其方法 sendMessageStart 中实现了您的逻辑,并且您还获得了 Button 对象并为该 Button 设置了一个侦听器。startButton =(Button)findViewById(R.id.startButton);

尝试删除此代码

    startButton.setOnClickListener(new View.OnClickListener() 

        @Override
        public void onClick(View v) 

        
    );

【讨论】:

以上是关于Android Studio 中的新活动无法启动的主要内容,如果未能解决你的问题,请参考以下文章

无法识别启动活动:升级到 Android Studio 4.0 后未找到默认活动

Android Studio无法启动活动性ComponentInfo

Android Studio:无法启动活动 java.lang.NullPointerException [重复]

安装后 Android Studio 无法启动 [重复]

在Android studio中使用活动模板时,我无法添加新的Java类吗?

无法回到 Android Studio 中的主要活动