我有一些我在Android Studio中工作的代码。我们的目标是创建一个具有计数器的应用程序,当您按下开始按钮时,计数器会快速上升,一旦达到所需的输入,您按下一个停止按钮,它会记录您与所需值的距离。它是测试反应时间等的。问题是每当我在手机或模拟器上运行应用程序时,应用程序会立即崩溃并发出错误"不幸的是,定时器测试已停止工作。"有谁知道问题可能是什么?
MainActivity.java中的代码:
package com.ultraman11.timertest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public int numCounter = 0;
public boolean counting = false;
TextView counter = (TextView) findViewById(R.id.counter);
TextView difference = (TextView) findViewById(R.id.diff);
EditText numInput = (EditText) findViewById(R.id.numInput);
public int userInt = Integer.parseInt(numInput.getText().toString());
public int differenceInt = Math.abs(numCounter - userInt);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void resetButton(View v){
numCounter = 0;
difference.setText("Difference: ");
counter.setText(Integer.toString(0));
}
public void startButton(View v){
counting = true;
while(counting){
numCounter++;
}
}
public void stopButton(View v){
counting = false;
difference.setText("Difference: " + Integer.toString(differenceInt));
}
@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_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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.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: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=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="@+id/button"
android:nestedScrollingEnabled="false"
android:onClick="resetButton"
android:layout_below="@+id/numInput"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/button2"
android:onClick="startButton"
android:layout_below="@+id/numInput"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:layout_marginTop="112dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="@+id/counter"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/numInput"
android:inputType="number"
android:layout_below="@+id/counter"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:layout_marginTop="31dp"
android:layout_alignRight="@+id/button"
android:layout_alignEnd="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:id="@+id/button3"
android:layout_marginTop="23dp"
android:layout_below="@+id/button2"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2"
android:onClick="stopButton" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Difference: "
android:id="@+id/diff"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="43dp" />
</RelativeLayout>
Logcat错误
09-10 17:33:45.103: D/AndroidRuntime(3445): Shutting down VM
09-10 17:33:45.104: E/AndroidRuntime(3445): FATAL EXCEPTION: main
09-10 17:33:45.104: E/AndroidRuntime(3445): Process: com.ultraman11.timertest, PID: 3445
09-10 17:33:45.104: E/AndroidRuntime(3445): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ultraman11.timertest/com.ultraman11.timertest.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
09-10 17:33:45.104: E/AndroidRuntime(3445): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
09-10 17:33:45.104: E/AndroidRuntime(3445): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
09-10 17:33:45.104: E/AndroidRuntime(3445): at android.app.ActivityThread.access$800(ActivityThread.java:151)
09-10 17:33:45.104: E/AndroidRuntime(3445): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
09-10 17:33:45.104: E/AndroidRuntime(3445): at android.os.Handler.dispatchMessage(Handler.java:102)
09-10 17:33:45.104: E/AndroidRuntime(3445): at android.os.Looper.loop(Looper.java:135)
09-10 17:33:45.104: E/AndroidRuntime(3445): at android.app.ActivityThread.main(ActivityThread.java:5254)
09-10 17:33:45.104: E/AndroidRuntime(3445): at java.lang.reflect.Method.invoke(Native Method)
09-10 17:33:45.104: E/AndroidRuntime(3445): at java.lang.reflect.Method.invoke(Method.java:372)
09-10 17:33:45.104: E/AndroidRuntime(3445): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
09-10 17:33:45.104: E/AndroidRuntime(3445): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
09-10 17:33:45.104: E/AndroidRuntime(3445): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
09-10 17:33:45.104: E/AndroidRuntime(3445): at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:124)
09-10 17:33:45.104: E/AndroidRuntime(3445): at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
09-10 17:33:45.104: E/AndroidRuntime(3445): at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
09-10 17:33:45.104: E/AndroidRuntime(3445): at com.ultraman11.timertest.MainActivity.onCreate(MainActivity.java:26)
09-10 17:33:45.104: E/AndroidRuntime(3445): at android.app.Activity.performCreate(Activity.java:5990)
09-10 17:33:45.104: E/AndroidRuntime(3445): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
09-10 17:33:45.104: E/AndroidRuntime(3445): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
09-10 17:33:45.104: E/AndroidRuntime(3445): ... 10 more
09-10 17:33:47.163: I/Process(3445): Sending signal. PID: 3445 SIG: 9
答案 0 :(得分:1)
当你点击开始时,你立即进入循环,因为计数是真的所以它只是将numCounter增加到infty。
,而不是使用像AsyncTask这样的东西包围增量的东西编辑: 抱歉,我没有明确表示它会立即崩溃,请确保在调用onCreate之前在onCreate方法中初始化你的内容... setContentView()
答案 1 :(得分:0)
在调用findViewById()
之前,您无法使用onCreate()
。你需要改变:
TextView counter;
TextView difference;
EditText numInput;
并将其设置为onCreate()
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = (TextView) findViewById(R.id.counter);
difference = (TextView) findViewById(R.id.diff);
numInput = (EditText) findViewById(R.id.numInput);
}
答案 2 :(得分:0)
您必须在 onCreate() 活动方法中初始化您的观点。我已编辑您的java文件。试试这个。
package com.ultraman11.timertest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public int numCounter = 0;
public boolean counting = false;
TextView counter;
TextView difference;
EditText numInput;
public int userInt
public int differenceInt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = (TextView) findViewById(R.id.counter);
difference = (TextView) findViewById(R.id.diff);
numInput = (EditText) findViewById(R.id.numInput);
try{
userInt = Integer.parseInt(numInput.getText().toString());
}Catch(Exception e){
userInt = 0;
}
differenceInt = Math.abs(numCounter - userInt);
}
public void resetButton(View v){
numCounter = 0;
difference.setText("Difference: ");
counter.setText(Integer.toString(0));
}
public void startButton(View v){
counting = true;
while(counting){
numCounter++;
}
}
public void stopButton(View v){
counting = false;
difference.setText("Difference: " + Integer.toString(differenceInt));
}
@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_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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
更新代码
package com.ultraman11.timertest;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
public int numCounter = 0;
public boolean counting = false;
TextView counter;
TextView difference;
EditText numInput;
public int userInt;
public int differenceInt;
TimerTask timertask = null;
Timer timer = null ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = (TextView) findViewById(R.id.counter);
difference = (TextView) findViewById(R.id.diff);
numInput = (EditText) findViewById(R.id.numInput);
}
public void resetButton(View v){
numCounter = 0;
difference.setText("Difference: ");
counter.setText(Integer.toString(0));
if(timertask!=null){
timertask.cancel();
timertask = null;
}
if(timer!=null){
timer.cancel();
timer = null;
}
}
public void startButton(View v){
if(timertask!=null){
timertask.cancel();
timertask = null;
}
if(timer!=null){
timer.cancel();
timer = null;
}
timer = new Timer();
timertask = new TimerTask() {
@Override
public void run() {
numCounter++;
runOnUiThread(new Runnable() {
public void run() {
counter.setText(Integer.toString(numCounter));
}
});
}
};
timer.scheduleAtFixedRate(timertask, 0, 1000);
}
public void stopButton(View v){
if(timertask!=null){
timertask.cancel();
timertask = null;
}
if(timer!=null){
timer.cancel();
timer = null;
}
try{
userInt = Integer.parseInt(numInput.getText().toString());
}catch(Exception e){
userInt = 0;
}
differenceInt = Math.abs(numCounter - userInt);
difference.setText("Difference: " + Integer.toString(differenceInt));
}
@Override
protected void onDestroy() {
if(timertask!=null){
timertask.cancel();
timertask = null;
}
if(timer!=null){
timer.cancel();
timer = null;
}
super.onDestroy();
}
}
答案 3 :(得分:0)
这是一个很多人所犯的简单错误。查看LogCat,它会告诉您需要使用Theme.AppCompat或类似的东西。 AppCompat需要AppCompat。它与设备主题不兼容。 您所要做的就是打开AndroidManifest.xml并执行以下操作:
<application
...
android:theme="@style/AppTheme">
...
</application>
在/res/values/styles.xml中添加:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat"/>
<style name="AppTheme" parent="AppBaseTheme">
<!-- Put any customizations here. -->
</style>
</resources>