Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 不幸的是,秒表在模拟器中停止了_Android_Eclipse_Android Emulator - Fatal编程技术网

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

Android 不幸的是,秒表在模拟器中停止了,android,eclipse,android-emulator,Android,Eclipse,Android Emulator,我对安卓非常陌生&现在我的应用程序出现了奇怪的问题。它在控制台中没有给我任何错误,但在模拟器中说秒表已经停止工作 my 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

我对安卓非常陌生&现在我的应用程序出现了奇怪的问题。它在控制台中没有给我任何错误,但在模拟器中说秒表已经停止工作

my 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" />
当我在emulator中运行应用程序时,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

我不知道问题出在哪里。因此,如果有人能帮助我,我将不胜感激。

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

Chronometer chrono = new Chronometer(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
我想你也有fragment类,因为chrono存在于fragment\u main上,你必须在fragment类中完成这项工作,或者在activity\u main布局中移动它

改变这个

setContentView(R.layout.activity_main);


因为按钮和计时器属于fragment_main.xml,所以setContentView应该引用fragment_main.xml而不是activity_main.xml。

可以删除它。因为它是在onCreate中初始化的。看起来视图也属于片段布局。工具:context=com.example.stopwatch.MainActivity$PlaceholderFragmentwelcome,很高兴提供帮助。如果这有助于您勾选答案以帮助其他人,那么您是否有机会实例化活动类??
setContentView(R.layout.fragment_main);