日期&时间应用程序无法启动

时间:2014-03-25 09:26:34

标签: java android datetime

我创建了一个应用程序,您可以单击按钮,它应该在EditText字段中显示当前日期和时间。问题是应用程序在启动时崩溃。有人可以帮助我吗?

package de.example.datumzeit;

公共类MainActivity扩展了Activity实现OnClickListener {

EditText etdate, ettime;
String mytime, mydate;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button datum_einfügen = (Button) findViewById(R.id.btngo);
    etdate = (EditText) findViewById(R.id.etdate);
    ettime = (EditText) findViewById(R.id.ettime);
    datum_einfügen.setOnClickListener(this);

    // time
    mytime = java.text.DateFormat.getTimeInstance().format(Calendar.getInstance().getTime());

    // date
    mydate =java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
}

@Override
public void onClick(View view){
    etdate.setText(mydate); 
    ettime.setText(mytime);
}

}

}

logcat的:

03-25 10:30:38.298: E/AndroidRuntime(25676): FATAL EXCEPTION: main

03-25 10:30:38.298: E/AndroidRuntime(25676): Process: de.example.datum_zeit, PID: 25676

03-25 10:30:38.298: E/AndroidRuntime(25676): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{de.example.datum_zeit/de.example.datum_zeit.MainActivity}: java.lang.NullPointerException

03-25 10:30:38.298: E/AndroidRuntime(25676):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)

03-25 10:30:38.298: E/AndroidRuntime(25676):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)

03-25 10:30:38.298: E/AndroidRuntime(25676):    at android.app.ActivityThread.access$900(ActivityThread.java:161)

03-25 10:30:38.298: E/AndroidRuntime(25676):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)

03-25 10:30:38.298: E/AndroidRuntime(25676):    at android.os.Handler.dispatchMessage(Handler.java:102)

03-25 10:30:38.298: E/AndroidRuntime(25676):    at android.os.Looper.loop(Looper.java:157)

03-25 10:30:38.298: E/AndroidRuntime(25676):    at android.app.ActivityThread.main(ActivityThread.java:5356)

03-25 10:30:38.298: E/AndroidRuntime(25676):    at java.lang.reflect.Method.invokeNative(Native Method)

03-25 10:30:38.298: E/AndroidRuntime(25676):    at java.lang.reflect.Method.invoke(Method.java:515)

03-25 10:30:38.298: E/AndroidRuntime(25676):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)

03-25 10:30:38.298: E/AndroidRuntime(25676):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)

03-25 10:30:38.298: E/AndroidRuntime(25676):    at dalvik.system.NativeStart.main(Native Method)

03-25 10:30:38.298: E/AndroidRuntime(25676): Caused by: java.lang.NullPointerException

03-25 10:30:38.298: E/AndroidRuntime(25676):    at android.app.Activity.findViewById(Activity.java:1965)

03-25 10:30:38.298: E/AndroidRuntime(25676):    at de.example.datum_zeit.MainActivity.<init>(MainActivity.java:26)

03-25 10:30:38.298: E/AndroidRuntime(25676):    at java.lang.Class.newInstanceImpl(Native Method)

03-25 10:30:38.298: E/AndroidRuntime(25676):    at java.lang.Class.newInstance(Class.java:1208)

03-25 10:30:38.298: E/AndroidRuntime(25676):    at android.app.Instrumentation.newActivity(Instrumentation.java:1079)

03-25 10:30:38.298: E/AndroidRuntime(25676):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2222)

03-25 10:30:38.298: E/AndroidRuntime(25676):    ... 11 more

5 个答案:

答案 0 :(得分:1)

这个

EditText etdate = (EditText) findViewById(R.id.etdate);
EditText ettime = (EditText) findViewById(R.id.ettime);

不在任何方法之内。

您需要在将布局内容设置为活动

后进行初始化
EditText etdate;
EditText ettime;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etdate = (EditText) findViewById(R.id.etdate);
ettime = (EditText) findViewById(R.id.ettime);

答案 1 :(得分:0)

这应该适合你:

package de.example.datumzeit;

public class MainActivity extends Activity implements OnClickListener {

    EditText etdate, ettime;
    String mytime, mydate;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button datum_einfügen = (Button) findViewById(R.id.btngo);
        datum_einfügen.setOnClickListener(this);
        etdate = (EditText) findViewById(R.id.etdate);
        ettime = (EditText) findViewById(R.id.ettime);
        // time
        mytime = java.text.DateFormat.getTimeInstance().format(Calendar.getInstance().getTime());

        // date
        mydate =java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
    }

    @Override
    public void onClick(View view){
        etdate.setText(mydate); 
        ettime.setText(mytime);
    }
}

答案 2 :(得分:0)

首先,我建议您阅读Number Formatter的文档 愿以下人员可以帮到你。

public static final SimpleDateFormat MMM_DD_YYYY_HH_MM_12_HOURS  = new SimpleDateFormat("MMM dd, yyyy hh:mm a");
public static final SimpleDateFormat DD_MM_YYYY_HH_MM_12_HOURS  = new SimpleDateFormat("dd/MM/yyyy hh:mm a");

为此创建一个实例。

答案 3 :(得分:0)

你应该移动

EditText etdate = (EditText) findViewById(R.id.etdate);
EditText ettime = (EditText) findViewById(R.id.ettime);
在onCreate()方法中

。同时使EditText变量成为全局变量。

 EditText etdate ,ettime 

和onCreate()方法

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 Button datum_einfügen = (Button) findViewById(R.id.btngo);
 etdate = (EditText) findViewById(R.id.etdate);
 ettime = (EditText) findViewById(R.id.ettime);
 datum_einfügen.setOnClickListener(this);
}

更新:

@Override
public void onClick(View view){


 if(view.getId()==R.id.btngo)
 mytime = java.text.DateFormat.getTimeInstance().format(Calendar.getInstance().getTime());

// date
 mydate =java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
 etdate.setText(mydate); 
 ettime.setText(mytime);
}
}

并删除此

 mytime = java.text.DateFormat.getTimeInstance().format(Calendar.getInstance().getTime());

// date
mydate =java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());

来自onCreate()方法。

答案 4 :(得分:-1)

etdateettime的初始化应该在方法中,例如onCreate