我试图乱七八糟地添加一个简单的秒表后遇到了错误。有人能指出我正确的方向吗?我对Java的经验很少,而我现在才开始学习。
我想创建一个增量计数器,一旦增加1,它也开始计算时间。
错误我得到了:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.countingapp/com.example.android.countingapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object referenc"
我的代码:
package com.example.android.countingapp;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Chronometer;
public class MainActivity extends AppCompatActivity {
TextView showValue;
TextView showTimeValue;
int counter = 0;
Chronometer simpleChronometer = (Chronometer) findViewById(R.id.TimeValue); // initiate a chronometer
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
showValue = (TextView) findViewById(R.id.CountValue);
showTimeValue = (TextView) findViewById(R.id.TimeValue);
}
public void CountIncrease (View view){
// On each click this button will increase
counter ++;
showValue.setText(Integer.toString(counter));
simpleChronometer.start(); // start a chronometer
}
public void CountDecrease (View view){
// On each click this button will increase
counter --;
showValue.setText(Integer.toString(counter));
}
public void CountReset (View view){
// On each click this button will increase
counter = 0;
showValue.setText(Integer.toString(counter));
}
@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);
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.android.countingapp.MainActivity"
tools:showIn="@layout/activity_main">
<Button
android:text="DecreaseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="CountDecrease"
android:id="@+id/button2"
android:layout_alignBaseline="@+id/button"
android:layout_alignBottom="@+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="@+id/button3"
android:layout_toEndOf="@+id/button3" />
<Button
android:text="ResetButton"
android:onClick="CountReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
android:id="@+id/button3" />
<Button
android:text="IncreaseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="CountIncrease"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/button3"
android:layout_toStartOf="@+id/button3" />
<TextView
android:text="CountDisplay"
android:id ="@+id/CountValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:layout_above="@+id/button2"
android:layout_centerHorizontal="true" />
<Chronometer
android:text="TimeDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/CountValue"
android:layout_centerHorizontal="true"
android:layout_marginBottom="46dp"
android:id="@+id/TimeValue" />
</RelativeLayout>
答案 0 :(得分:3)
这一行
Chronometer simpleChronometer = (Chronometer) findViewById(R.id.TimeValue);
应该放在你的onCreate方法里面,
setContentView(R.layout.activity_main);
这是因为setContentView方法告诉活动要使用哪个布局,因此它允许它使用findViewById方法使用id来查找视图。
以下是它的外观
public class MainActivity extends AppCompatActivity {
TextView showValue;
TextView showTimeValue;
int counter = 0;
//Declare here the simpleChonometer object
Chronometer simpleChronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initiate here the simpleChronometer object
simpleChronometer = (Chronometer) findViewById(R.id.TimeValue); // initiate a chronometer
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
showValue = (TextView) findViewById(R.id.CountValue);
showTimeValue = (TextView) findViewById(R.id.TimeValue);
}
...
答案 1 :(得分:1)
将Chronometer初始化为简单的计时器后,将您的
simpleChronometer =
(Chronometer) findViewById(R.id.TimeValue); // initiate a chronometer
在onCreate()下,就像你为floatingActionButton做的那样。