0

這個問題的標題描述了我的問題。我試圖修復這一段時間,我厭倦了搜索,並希望得到一些幫助。它曾經工作,直到我添加處理程序和亞軍。我試圖製作一個應用程序,它是一個簡單的時鐘應用程序,用戶可以在任何時候設置它們。無論何時我運行應用程序,它都會打開,然後表示無法正常啓動。這是由另一個活動設置的活動,然後它會更改它接收的值,它還會在文本視圖中顯示這些值。爲什麼我得到一個空指針異常?

package CoopFun.Clocks; 

    import android.os.Bundle; 
    import android.os.Handler; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.view.Window; 
    import android.widget.TextView; 
    import android.content.Intent; 
    import android.app.Activity; 

public class ClockMain extends Activity { 

int Hours; 
int Minutes; 
int Seconds; 

String TimeOfDayS; 

TextView HoursMainV; 
TextView MinutesMainV; 
TextView SecondsMainV; 
TextView TimeOfDayMainV; 

Timer oneSecond; 



public void onCreate(Bundle bundle) { 
    super.onCreate(bundle); 
    setContentView(R.layout.clock_main); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 


    Bundle extras = getIntent().getExtras(); 
     if (extras == null) { 
      return; 
     } 
    int Hour = extras.getInt("HoursS"); 
    Hour = Hours; 
    int Minute = extras.getInt("MinutesS"); 
    Minute = Minutes; 
    int Second = extras.getInt("SecondsS"); 
    Second = Seconds; 
    String TimeOfDaySs = extras.getString("TimeOfDayS"); 
    TimeOfDaySs = TimeOfDayS; 

    HoursMainV = (TextView) findViewById(R.id.HoursMainV); 
    HoursMainV.setText(""+Hour); 

    MinutesMainV = (TextView) findViewById(R.id.MinutesMainV); 
    MinutesMainV.setText(":"+Minute); 

    SecondsMainV = (TextView) findViewById(R.id.SecondsMainV); 
    SecondsMainV.setText(":"+Second); 

    TimeOfDayMainV = (TextView) findViewById(R.id.TimeOfDayMainV); 
    TimeOfDayMainV.setText(" "+TimeOfDaySs); 
    final Handler handler=new Handler(); 
    final Runnable r = new Runnable() 
    { 
     public void run() 
     { 
      ++Seconds; 
      if(Seconds == 60){ 
       ++Minutes; 
       Seconds = 0; 
       if(Minutes == 60) { 
        ++Hours; 
        Minutes = 0; 
        if(Hours == 12){ 
         if(TimeOfDayS.equals("AM")) { 
          TimeOfDayS = "PM"; 
         } else{ 
          TimeOfDayS = "AM"; 
         } 
         Hours = 0; 
        } 
       } 
      } 
      HoursMainV.append(""+Hours); 
      if(Minutes <=9) { 
       MinutesMainV.append(":0"+Minutes); 
      } else { 
       MinutesMainV.append(":"+Minutes); 
      } 
      if(Seconds <=9) { 
       SecondsMainV.append(":0"+Seconds); 
      } else { 
       SecondsMainV.append(":"+Seconds); 
      } 
      TimeOfDayMainV.append(" " + TimeOfDayS); 
      handler.postDelayed(this, 1000); 
     } 
    }; 

    handler.postDelayed(r, 1000); 
} 
} 

XML:

 <?xml version="1.0" encoding="utf-8"?> 
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
     <TextView 
    android:textSize="50dip" 
      android:id="@+id/HoursMainV" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="25dip"></TextView> 
      <TextView 
      android:textSize="50dip" 
     android:id="@+id/MinutesMainV" 
      android:layout_width="wrap_content" 
     android:layout_height="wrap_content"></TextView> 
     <TextView 
     android:textSize="50dip" 
     android:id="@+id/SecondsMainV" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"></TextView> 
     <TextView 
     android:textSize="50dip" 
     android:id="@+id/TimeOfDayMainV" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"></TextView> 
    </LinearLayout> 

清單:

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="CoopFun.Clocks" 
    android:versionCode="1" 
    android:versionName="1.0"> 


<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".Clock" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     </activity> 
     <activity android:name=".ClockMain"> 
    </activity> 

</application> 
</manifest> 

謝謝。

回答

1
int Hour = extras.getInt("HoursS"); 
Hour = Hours; 
int Minute = extras.getInt("MinutesS"); 
Minute = Minutes; 
int Second = extras.getInt("SecondsS"); 
Second = Seconds; 
String TimeOfDaySs = extras.getString("TimeOfDayS"); 
TimeOfDaySs = TimeOfDayS; 

更改上面的代碼與此::

int Hour = extras.getInt("HoursS"); 
Hours = Hour; 
int Minute = extras.getInt("MinutesS"); 
Minutes = Minute; 
int Second = extras.getInt("SecondsS"); 
Seconds = Second; 
String TimeOfDaySs = extras.getString("TimeOfDayS"); 
TimeOfDayS = TimeOfDaySs; 

簡單,U都拿到空指針異常bcozü是空值設置爲小時,分鐘,秒和TimeOfDaySs和訪問它們。

乾杯.... !!!