我正在学习有关Android编程的基础知识。
我按照在线课程创建了这个。
这是一个非常简单的应用程序,在点击按钮后我无法打开新活动。
我的模拟器返回的错误是“不幸的是,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;
}
}
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.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;
}
}
logcat / errors:
09-14 15:45:57.802 4570-4570/com.example.carneiro.jogonumeros I/art﹕ Not late-enabling -Xcheck:jni (already on)
09-14 15:45:57.802 4570-4570/com.example.carneiro.jogonumeros I/art﹕ Late-enabling JIT
09-14 15:45:57.806 4570-4570/com.example.carneiro.jogonumeros I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000
09-14 15:45:57.858 4570-4570/com.example.carneiro.jogonumeros W/System﹕ ClassLoader referenced unknown path: /data/app/com.example.carneiro.jogonumeros-2/lib/x86
09-14 15:45:58.058 4570-4585/com.example.carneiro.jogonumeros D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
09-14 15:45:58.068 4570-4570/com.example.carneiro.jogonumeros D/﹕ HostConnection::get() New Host Connection established 0xabdefcf0, tid 4570
09-14 15:45:58.188 4570-4585/com.example.carneiro.jogonumeros D/﹕ HostConnection::get() New Host Connection established 0xabdefe70, tid 4585
09-14 15:45:58.207 4570-4585/com.example.carneiro.jogonumeros I/OpenGLRenderer﹕ Initialized EGL, version 1.4
09-14 15:45:58.285 4570-4585/com.example.carneiro.jogonumeros W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-14 15:45:58.285 4570-4585/com.example.carneiro.jogonumeros W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xabebf180, error=EGL_SUCCESS
然后,点击按钮:
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
答案 0 :(得分:0)
确保在inputGuess
中将EditText
声明为fragment_number_guess_game
。
答案 1 :(得分:0)
这是错误:
引起:java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView无法强制转换为 android.widget.EditText
以下是导致它的原因:
inputGuess = (EditText) rootView.findViewById(R.id.inputGuess);
修改代码以使其成为TextView
,或修改xml布局以使视图为EditText
。不确定哪一个适合你。