【问题标题】:Why am I getting a null pointer exception?为什么我得到一个空指针异常?
【发布时间】:2023-11-01 03:54:01
【问题描述】:

这个问题的标题描述了我的问题。我已经尝试修复这个问题一段时间了,我厌倦了搜索,希望得到一些帮助。在我添加 Handler 和 runner 之前,它一直有效。我正在尝试制作一个简单的时钟应用程序,用户可以将其设置为他们想要的任何时间。每当我去运行应用程序时,它会打开,然后说它无法正常启动。这是一个由另一个活动设置的活动,然后它会更改它接收到的值,它还会在文本视图中显示这些值。

   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>

谢谢。

【问题讨论】:

    标签: android-emulator textview android-manifest handler runnable


    【解决方案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;
    

    简单地说,你得到空指针异常,因为你正在将空值设置为 Hour、Minute、Second 和 TimeOfDaySs 并访问它们。

    干杯....!!!

    【讨论】: