Java 我的android应用程序在点击按钮时关闭(可能是新手错误)

Java 我的android应用程序在点击按钮时关闭(可能是新手错误),java,android,android-edittext,textview,Java,Android,Android Edittext,Textview,我正在学习Android编程的基础知识 我跟随一个在线课程创建了这个 这是一个非常简单的应用程序,在点击一个按钮后,我被困在打开一个新的活动中 emulator返回的错误是“不幸的是,JogoNumeros已停止” 我的代码如下: MainActivity.java: package com.example.carneiro.jogonumeros; import android.os.Bundle; import android.support.v7.app.AppCompatActivit

我正在学习Android编程的基础知识

我跟随一个在线课程创建了这个

这是一个非常简单的应用程序,在点击一个按钮后,我被困在打开一个新的活动中

emulator返回的错误是“不幸的是,JogoNumeros已停止”

我的代码如下:

MainActivity.java:

package com.example.carneiro.jogonumeros;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
MainActivityFragment.java:

package com.example.carneiro.jogonumeros;

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

public class MainActivityFragment extends Fragment {

    public MainActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        Button btnNumberGuessGame = (Button) rootView.findViewById(R.id.btnNumberGuessGame);

        btnNumberGuessGame.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), NumberGuessGame.class);
                getActivity().startActivity(intent);
            }
        });
        return rootView;
    }
}
package com.example.carneiro.jogonumeros;

import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.AlertDialog;

public class NumberGuessGameFragment extends Fragment {

    Button btnNew = null;
    Button btnGuess = null;
    EditText inputGuess = null;
    TextView textMessage = null;
    Integer tentativas = 0;

    boolean gameFinished = false;
    int secretNumber = 0;

    public NumberGuessGameFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView =inflater.inflate(R.layout.fragment_number_guess_game, container, false);

        btnNew = (Button) rootView.findViewById(R.id.btnNew);

        btnNew.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick (View view) {
                actionNewGame();
            }
        });

        btnGuess = (Button) rootView.findViewById(R.id.btnGuess);

        btnGuess.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick (View view) {
                processGuess();
            }
        });

        inputGuess = (EditText) rootView.findViewById(R.id.inputGuess);

        textMessage = (TextView) rootView.findViewById(R.id.textMessage);

        newGame();
        return rootView;
    }

    private void processGuess(){
        String strGuess = inputGuess.getText().toString();
        inputGuess.setText("");
        if (strGuess.length() == 0)
            return;

        int guess = Integer.valueOf(strGuess);

        if (guess > secretNumber) {
            textMessage.setText(R.string.message_is_smaller);
            tentativas++;
        } else if (guess < secretNumber){
            textMessage.setText(R.string.message_is_bigger);
            tentativas++;
        } else {
            tentativas++;
            textMessage.setText(R.string.message_right_1 + tentativas + R.string.message_right_2);
            gameFinished = true;
        }

    }

    private void actionNewGame(){
        if (gameFinished) {
            newGame();
            return;
        }

        new AlertDialog.Builder(getActivity())
                .setMessage(R.string.confirm_new_game)
                .setCancelable(false)
                .setNegativeButton(R.string.nao, null)
                .setPositiveButton(R.string.sim, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        NumberGuessGameFragment.this.newGame();
                    }
                });
    }

    private void newGame(){
        secretNumber = (int) (Math.random() * 100);
        textMessage.setText("");
        gameFinished = false;

    }

}
NumberGuessGame.java:

package com.example.carneiro.jogonumeros;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;


public class NumberGuessGame extends AppCompatActivity {

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


    @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_number_guess_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);
    }
}
NumberGuessGameFragment.java:

package com.example.carneiro.jogonumeros;

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

public class MainActivityFragment extends Fragment {

    public MainActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        Button btnNumberGuessGame = (Button) rootView.findViewById(R.id.btnNumberGuessGame);

        btnNumberGuessGame.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), NumberGuessGame.class);
                getActivity().startActivity(intent);
            }
        });
        return rootView;
    }
}
package com.example.carneiro.jogonumeros;

import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.AlertDialog;

public class NumberGuessGameFragment extends Fragment {

    Button btnNew = null;
    Button btnGuess = null;
    EditText inputGuess = null;
    TextView textMessage = null;
    Integer tentativas = 0;

    boolean gameFinished = false;
    int secretNumber = 0;

    public NumberGuessGameFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView =inflater.inflate(R.layout.fragment_number_guess_game, container, false);

        btnNew = (Button) rootView.findViewById(R.id.btnNew);

        btnNew.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick (View view) {
                actionNewGame();
            }
        });

        btnGuess = (Button) rootView.findViewById(R.id.btnGuess);

        btnGuess.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick (View view) {
                processGuess();
            }
        });

        inputGuess = (EditText) rootView.findViewById(R.id.inputGuess);

        textMessage = (TextView) rootView.findViewById(R.id.textMessage);

        newGame();
        return rootView;
    }

    private void processGuess(){
        String strGuess = inputGuess.getText().toString();
        inputGuess.setText("");
        if (strGuess.length() == 0)
            return;

        int guess = Integer.valueOf(strGuess);

        if (guess > secretNumber) {
            textMessage.setText(R.string.message_is_smaller);
            tentativas++;
        } else if (guess < secretNumber){
            textMessage.setText(R.string.message_is_bigger);
            tentativas++;
        } else {
            tentativas++;
            textMessage.setText(R.string.message_right_1 + tentativas + R.string.message_right_2);
            gameFinished = true;
        }

    }

    private void actionNewGame(){
        if (gameFinished) {
            newGame();
            return;
        }

        new AlertDialog.Builder(getActivity())
                .setMessage(R.string.confirm_new_game)
                .setCancelable(false)
                .setNegativeButton(R.string.nao, null)
                .setPositiveButton(R.string.sim, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        NumberGuessGameFragment.this.newGame();
                    }
                });
    }

    private void newGame(){
        secretNumber = (int) (Math.random() * 100);
        textMessage.setText("");
        gameFinished = false;

    }

}
然后,单击按钮:

09-14 15:46:02.425    4570-4570/com.example.carneiro.jogonumeros D/AndroidRuntime﹕ Shutting down VM
09-14 15:46:02.425    4570-4570/com.example.carneiro.jogonumeros E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.carneiro.jogonumeros, PID: 4570
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.carneiro.jogonumeros/com.example.carneiro.jogonumeros.NumberGuessGame}: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
            at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
            at com.example.carneiro.jogonumeros.NumberGuessGame.onCreate(NumberGuessGame.java:14)
            at android.app.Activity.performCreate(Activity.java:6237)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
            at com.example.carneiro.jogonumeros.NumberGuessGame.onCreate(NumberGuessGame.java:14)
            at android.app.Activity.performCreate(Activity.java:6237)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
            at com.example.carneiro.jogonumeros.NumberGuessGameFragment.onCreateView(NumberGuessGameFragment.java:55)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:995)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1185)
            at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1287)
            at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2243)
            at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
            at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:278)
            at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:78)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
            at com.example.carneiro.jogonumeros.NumberGuessGame.onCreate(NumberGuessGame.java:14)
            at android.app.Activity.performCreate(Activity.java:6237)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-14 15:46:04.425    4570-4570/com.example.carneiro.jogonumeros I/Process﹕ Sending signal. PID: 4570 SIG: 9

确保
inputGuess
fragment\u number\u guess\u game
中声明为
EditText
,错误如下:

原因:java.lang.ClassCastException: 无法将android.support.v7.widget.AppCompatTextView强制转换为 android.widget.EditText

以下是导致它的原因:

inputGuess = (EditText) rootView.findViewById(R.id.inputGuess);


要么修改代码使其成为
TextView
,要么修改xml布局使其成为
EditText
。不确定哪一个适合您。

Post
fragment\u main.xml
fragment\u number\u guess\u game.xml
。错误表明您的xml文件有问题。你检查过那个文件了吗?@MarceloCarneiro没问题。任何人都有可能,真的。谢谢你。但是,当我在XML中进行更改时,我选择了另一个告诉我要这样做的答案。