不幸的是,秒表已停止在模拟器中

时间:2014-05-21 07:27:48

标签: android eclipse android-emulator

我是Android和Android的新手现在我在我的应用程序中遇到了奇怪的问题。它在控制台中没有给我任何错误,但它在模拟器中说秒表已停止工作......

我的MainActivity.java

import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;

public class MainActivity extends ActionBarActivity {

    Chronometer chrono = new Chronometer(this);
    Button strt, stop, reset;


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

        chrono=(Chronometer)findViewById(R.id.chronometer1);
        strt=(Button)findViewById(R.id.button1);
        stop=(Button)findViewById(R.id.button2);
        reset=(Button)findViewById(R.id.button3);

        strt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                chrono.start();
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                chrono.stop();

            }
        });

        reset.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                chrono.setBase(SystemClock.elapsedRealtime());
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

fragment_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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.stopwatch.MainActivity$PlaceholderFragment" >

        <Chronometer
            android:id="@+id/chronometer1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="61dp"
            android:text="Chronometer"
            android:textSize="@dimen/abc_action_bar_stacked_max_height" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/chronometer1"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="28dp"
            android:text="Start" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button1"
            android:layout_below="@+id/button1"
            android:layout_marginTop="37dp"
            android:text="Stop" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button2"
            android:layout_below="@+id/button2"
            android:layout_marginTop="40dp"
            android:text="Reset" />

</RelativeLayout>

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.stopwatch.MainActivity"
    tools:ignore="MergeRootFrame" />

当我在模拟器中运行应用程序时,log cat显示了这个......

05-21 03:08:54.440: D/AndroidRuntime(1368): Shutting down VM
05-21 03:08:54.440: W/dalvikvm(1368): threadid=1: thread exiting with uncaught exception (group=0xb2a92ba8)
05-21 03:08:54.460: E/AndroidRuntime(1368): FATAL EXCEPTION: main
05-21 03:08:54.460: E/AndroidRuntime(1368): Process: com.example.stopwatch, PID: 1368
05-21 03:08:54.460: E/AndroidRuntime(1368): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.stopwatch/com.example.stopwatch.MainActivity}: java.lang.NullPointerException
05-21 03:08:54.460: E/AndroidRuntime(1368):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at android.os.Looper.loop(Looper.java:136)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at java.lang.reflect.Method.invokeNative(Native Method)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at java.lang.reflect.Method.invoke(Method.java:515)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at dalvik.system.NativeStart.main(Native Method)
05-21 03:08:54.460: E/AndroidRuntime(1368): Caused by: java.lang.NullPointerException
05-21 03:08:54.460: E/AndroidRuntime(1368):     at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at android.view.View.<init>(View.java:3438)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at android.view.View.<init>(View.java:3505)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at android.widget.TextView.<init>(TextView.java:623)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at android.widget.Chronometer.<init>(Chronometer.java:100)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at android.widget.Chronometer.<init>(Chronometer.java:84)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at com.example.stopwatch.MainActivity.<init>(MainActivity.java:13)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at java.lang.Class.newInstanceImpl(Native Method)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at java.lang.Class.newInstance(Class.java:1208)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
05-21 03:08:54.460: E/AndroidRuntime(1368):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
05-21 03:08:54.460: E/AndroidRuntime(1368):     ... 11 more
05-21 03:13:55.020: I/Process(1368): Sending signal. PID: 1368 SIG: 9

我不知道问题是什么。所以如果有人能帮助我,我会很感激。

2 个答案:

答案 0 :(得分:0)

在onCreate方法之前,您无法执行以下代码:

Chronometer chrono = new Chronometer(this);

this是类的上下文,在调用onCreate之前它是null。

因此您的代码必须是:

public class MainActivity extends ActionBarActivity {

    Chronometer chrono;
    Button strt, stop, reset;


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

        chrono =  = new Chronometer(this);  // you can remove that too as  Raghunandan said in comment

        // other code

我认为你也有片段类,因为chrono存在于fragment_main你必须在Fragment类中完成这项工作或者在activity_main layout

中移动它

答案 1 :(得分:0)

试试这个..

更改此

setContentView(R.layout.activity_main);

setContentView(R.layout.fragment_main);

由于ButtonChronometer属于fragment_main.xml,因此setContentView应引用fragment_main.xml而不是activity_main.xml