2015-01-16 9 views
11

का आह्वान करने का प्रयास मैं Timer और TimerTask कक्षाओं का उपयोग करके एक साधारण टाइमर बनाने की कोशिश कर रहा हूं। मैं निम्नलिखित त्रुटि प्राप्त हो रही:वर्चुअल विधि

<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"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="0:00" 
    android:id="@+id/timer" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="54dp" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Start/Pause" 
    android:id="@+id/start" 
    android:layout_below="@+id/timer" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="119dp" 
    android:onClick="startB"/> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Stop/Reset" 
    android:id="@+id/stop" 
    android:layout_alignBottom="@+id/start" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 
</RelativeLayout> 

हर समाधान मैं इस त्रुटि को देखा है:

package com.austinheitmann.stopwatch; 

import android.os.CountDownTimer; 
import android.os.SystemClock; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.TextView; 

import java.util.Timer; 
import java.util.TimerTask; 


public class MainActivity extends ActionBarActivity { 

TextView timerf; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
timerf = (TextView) findViewById(R.id.timer); 
} 

int i=0; 




TimerTask time = new TimerTask() { 
    public void run() { 
     i++; 
     int k = i/60; 
     int j = i%60; 
     if (j>9) { 
      timerf.setText("seconds remaining: " + k + ":" + j); 
     }else { 
      timerf.setText("seconds remaining: " + k + ":0" + j); 
     } 
     Log.d("Uhh", "Sec:" + i); 
    } 
}; 

Timer timers = new Timer(); 

public void startB(View view) { 
timers.schedule(time, 100000000, 1000); 
} 

@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 फ़ाइल (लेआउट) है:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 

यहाँ मेरी कोड है लेआउट के साथ करना है, लेकिन मैं वैसे भी इसे बदल नहीं रहा हूँ। मैं startb कक्षा में TimerTask पर भी स्थानांतरित हो गया हूं और एक ही त्रुटि प्राप्त कर रहा हूं। मैं इस त्रुटि को कैसे ठीक कर सकता हूं?

यहाँ पूरे त्रुटि मैं logcat समाशोधन (मैं डाल ** जहां त्रुटि पर होता है) के बाद एक रन से प्राप्त होते हैं:

01-16 12:27:21.008 2455-2455/com.austinheitmann.stopwatch I/art﹕ Not late-enabling -Xcheck:jni (already on) 
01-16 12:27:21.119 2455-2455/com.austinheitmann.stopwatch D/AndroidRuntime﹕ Shutting down VM 
    --------- beginning of crash 
01-16 12:27:21.123 2455-2455/com.austinheitmann.stopwatch E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.austinheitmann.stopwatch, PID: 2455 
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.austinheitmann.stopwatch/com.austinheitmann.stopwatch.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
      at android.app.ActivityThread.access$800(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      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:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 
      at android.app.Activity.findViewById(Activity.java:2071) 
      at com.austinheitmann.stopwatch.MainActivity.<init>(MainActivity.java:25) 
      at java.lang.reflect.Constructor.newInstance(Native Method) 
      at java.lang.Class.newInstance(Class.java:1572) 
      at android.app.Instrumentation.newActivity(Instrumentation.java:1065) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            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:899) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
01-16 12:44:57.778 2582-2582/com.austinheitmann.stopwatch D/AndroidRuntime﹕ Shutting down VM 
01-16 12:44:57.778 2582-2582/com.austinheitmann.stopwatch E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.austinheitmann.stopwatch, PID: 2582 
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.austinheitmann.stopwatch/com.austinheitmann.stopwatch.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
      at android.app.ActivityThread.access$800(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      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:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 
      at android.app.Activity.findViewById(Activity.java:2071) 
      at com.austinheitmann.stopwatch.MainActivity.<init>(MainActivity.java:25) 
      at java.lang.reflect.Constructor.newInstance(Native Method) 
      at java.lang.Class.newInstance(Class.java:1572) 
      at android.app.Instrumentation.newActivity(Instrumentation.java:1065) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            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:899) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
01-16 12:46:25.059 2641-2641/com.austinheitmann.stopwatch D/AndroidRuntime﹕ Shutting down VM 
01-16 12:46:25.059 2641-2641/com.austinheitmann.stopwatch E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.austinheitmann.stopwatch, PID: 2641 
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.austinheitmann.stopwatch/com.austinheitmann.stopwatch.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
      at android.app.ActivityThread.access$800(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      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:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 
      at android.app.Activity.findViewById(Activity.java:2071) 
      at com.austinheitmann.stopwatch.MainActivity.<init>(MainActivity.java:25) 
      at java.lang.reflect.Constructor.newInstance(Native Method) 
      at java.lang.Class.newInstance(Class.java:1572) 
      at android.app.Instrumentation.newActivity(Instrumentation.java:1065) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            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:899) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
01-16 12:47:50.630 2702-2702/com.austinheitmann.stopwatch D/AndroidRuntime﹕ Shutting down VM 
01-16 12:47:50.630 2702-2702/com.austinheitmann.stopwatch E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.austinheitmann.stopwatch, PID: 2702 
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.austinheitmann.stopwatch/com.austinheitmann.stopwatch.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
      at android.app.ActivityThread.access$800(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      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:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    **Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference** 
      at android.app.Activity.findViewById(Activity.java:2071) 
      at com.austinheitmann.stopwatch.MainActivity.<init>(MainActivity.java:25) 
      at java.lang.reflect.Constructor.newInstance(Native Method) 
      at java.lang.Class.newInstance(Class.java:1572) 
      at android.app.Instrumentation.newActivity(Instrumentation.java:1065) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at  android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            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:899) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
+0

के बाद आप Simas 'जवाब का पालन किया है, ऐसा 'स्वच्छ project' 'बिल्ड' मेनू के तहत, और एमुलेटर को पुनरारंभ करें। उसके बाद अपवाद मेरी स्थिति में चला गया। – progonkpa

उत्तर

29

आप findViewById की कोशिश कर रहे गतिविधि के संदर्भ से पहले तैयार है।

आप में भी अन्य तरीकों में timerf तक पहुँचने के लिए, एक वर्ग के सदस्य बनाने लेकिन प्रारंभ करना चाहते हैं onCreatesetContentView के बाद:

TextView timerf; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    timerf = (TextView) findViewById(R.id.timer); 
} 

अद्यतन:

मैं नोटिस नहीं किया था अन्य क्लास सदस्य वेरिएबल्स जिन्हें आप तत्काल कर रहे हैं। वे timerf संदर्भित कर रहे हैं जो शून्य।

आपको उन्हें onCreate में भी प्रारंभ करना होगा। यहाँ पूर्ण कोड है कि काम करना चाहिए है:

package com.austinheitmann.stopwatch; 

import android.os.CountDownTimer; 
import android.os.SystemClock; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.TextView; 

import java.util.Timer; 
import java.util.TimerTask; 


public class MainActivity extends ActionBarActivity { 

    Timer timers = new Timer(); 
    TextView timerf; 
    TimerTask time; 
    int i=0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     timerf = (TextView) findViewById(R.id.timer); 

     time = new TimerTask() { 
      public void run() { 
       i++; 
       int k = i/60; 
       int j = i%60; 
       if (j>9) { 
        timerf.setText("seconds remaining: " + k + ":" + j); 
       }else { 
        timerf.setText("seconds remaining: " + k + ":0" + j); 
       } 
       Log.d("Uhh", "Sec:" + i); 
      } 
     }; 
    } 

    public void startB(View view) { 
     timers.schedule(time, 100000000, 1000); 
    } 

    @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); 
    } 
} 
+0

मैंने आपके द्वारा किए गए परिवर्तन किए हैं लेकिन मुझे अभी भी एक ही त्रुटि मिल रही है, प्रश्न में कोड आपकी प्रतिक्रिया के साथ अपडेट किया गया है। – aheit

+0

@heit अपना लेआउट पोस्ट करें (activity_main.xml) – Simas

+0

मैंने गतिविधि_main.xml – aheit

-3

पहले की आप निर्माता का उपयोग public MainActivity() और Timer timer; और long startTime; long stopTime घोषित Gobble चर

public MainActivity() { 
    timer = new Timer(); 
    startTime = System.currentTimeMillis(); 
    stopTime = 0; 
} 
+1

उनमें से प्रत्येक आवृत्ति चर को प्रारंभ करने के लिए एक कन्स्ट्रक्टर की आवश्यकता नहीं है, न ही आपको वास्तव में एंड्रॉइड गतिविधियों के लिए एक कन्स्ट्रक्टर घोषित करना चाहिए। –

+1

'गोबल चर' क्या है? यदि यह 'वैश्विक' के लिए एक टाइपो है, तो जावा में कोई ग्लोबल्स नहीं है; ओपी शायद सदस्यों का मतलब है? –

संबंधित मुद्दे