首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在Activity内部实例化Chronometer对象

无法在Activity内部实例化Chronometer对象
EN

Stack Overflow用户
提问于 2016-10-25 01:18:08
回答 1查看 194关注 0票数 0

我有一个活动,在我的通知中,有一个停止chronometer.The点击事件的按钮,但当我调用方法stopButtonClick()来停止时钟时,我得到了错误。

代码语言:javascript
复制
    public class Chronometers extends Activity {

    private long timeWhenStopped = 0;
    private boolean stopClicked;
    private Chronometer chs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        chs=(Chronometer)this.findViewById(R.id.ch);
        NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.icas);           
        RemoteViews remoteViews = new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.widget);
        remoteViews.setChronometer(R.id.ch, SystemClock.elapsedRealtime(), null, true);
        builder.setContent(remoteViews);

        Intent buttonsIntent = new Intent(this,StartupReceiver.class);
        buttonsIntent.putExtra("do_action","play");
        PendingIntent as21 = PendingIntent.getBroadcast(this, 0, buttonsIntent, 0);
        remoteViews.setOnClickPendingIntent(R.id.button3, as21);
        nm.notify(0, builder.build());
    }

    private void showElapsedTime()  {
        long elapsedMillis = SystemClock.elapsedRealtime() - chs.getBase();  
    }

    // the method for when we press the 'start' button
    public void startButtonClick() {
        int stoppedMilliseconds = 0;
        String chronoText = chs.getText().toString();
        String array[] = chronoText.split(":");
        if (array.length == 2) {
            stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
                    + Integer.parseInt(array[1]) * 1000;
        } else if (array.length == 3) {
            stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000
                    + Integer.parseInt(array[1]) * 60 * 1000
                    + Integer.parseInt(array[2]) * 1000;
        }
        chs.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
        chs.start();
    }

    // the method for when we press the 'stop' button
    public void stopButtonClick() {
        if (!stopClicked) {
            chs.stop();
            showElapsedTime();
        }

    }

    public static class StartupReceiver extends BroadcastReceiver {

        Context ctx;

        @Override
        public void onReceive(Context ctx, Intent intent)  {
            // TODO Auto-generated method stub
            this.ctx = ctx;
            Toast.makeText(ctx,"mak pro cina",Toast.LENGTH_SHORT).show();
            Intent i = new Intent(ctx,NotifyActivityHandler.class);
            i.putExtra("do_action","play");
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ctx.startActivity(i);
            System.out.println("controll will be here ");
        }
    }

    public static class NotifyActivityHandler extends Activity {
        String action;
        int k = 0;

        protected void onCreate( Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            Intent abs=getIntent();
            action = abs.getStringExtra("do_action");
            System.out.println(action);
            if (action.equals("play"))
            {
                System.out.println("patrpooooooooooooo");
                new Chronometers().stopButtonClick();
            }
            else
            {
                System.out.println("mahitreee");
            }
            Toast.makeText(this,"jasgh",Toast.LENGTH_SHORT).show();
        }

    }
}

logcat中的错误是

代码语言:javascript
复制
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.home.outdoor/com.example.home.outdoor.Chronometers$NotifyActivityHandler}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Chronometer.stop()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2326)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:147)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5264)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)

我已经创建了计时器的对象,但它仍然给了这个error.Please提前帮助我out.Thanks!!

EN

回答 1

Stack Overflow用户

发布于 2016-10-25 01:28:00

您需要使用setContentView(R.layout.my_layout)设置您的内容布局,my_layout是您创建的布局。之后,您可以使用findViewById

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40223981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档