日期和时间应用无法启动



我做了一个应用程序,你可以点击一个按钮,它应该在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

This

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);

这应该可以为您工作:

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);
    }
}

首先我给你一个建议去浏览数字格式化器的文档希望下面这个能帮到你。

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

你应该移动

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());

etdateettime的初始化应该像onCreate一样在一个方法中

相关内容

  • 没有找到相关文章

最新更新