日期不会显示在我指定的textView中

时间:2014-06-10 12:12:43

标签: android

所以我在这里看了很多问题,搜索并寻找无济于事的答案。

我的问题是textView:myDateTimeTextView中没有显示任何内容。 我希望显示当前日期。

这里是代码:

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

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

public class currentDateTime extends AsyncTask<Object, Void, String>{
    protected String doInBackground(Object... arg0) {
        TextView myDateTextView = (TextView)findViewById(R.id.myDateTextView);
        String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
        myDateTextView.setText(currentDateTimeString);
        String Format = "EEEE, MMMM dd, YYYY";
        Date today = new Date(); SimpleDateFormat sdf = new SimpleDateFormat(Format, Locale.ENGLISH); System.err.format("%30s %s\n", Format, sdf.format(today));

    return currentDateTimeString;
    }
    }

当我打开它时,我的应用程序崩溃之前。我发现(通过这个网站)我应该把它作为AsycTask运行。这解决了崩溃问题,但出现了一个新问题:它没有显示日期。

非常感谢任何帮助! 谢谢:))

编辑1

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

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    new currentDateTime().execute();

}

public class currentDateTime extends AsyncTask<Void, Void, String>{
    TextView myDateTextView = (TextView)findViewById(R.id.myDateTextView);
    @Override
    protected String doInBackground(Void... arg0) {
        String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
        String format = "EEEE, MMMM dd, YYYY";
        Date today = new Date(); SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH); System.err.format("%30s %s\n", format, sdf.format(today));

    return currentDateTimeString;
        }
        @Override
       protected void onPostExecute(String currentDateTime) {
            myDateTextView.setText(currentDateTime);
        }   
}

所以,你们基本上都告诉我要做同样的事情,这是我看到的唯一方法,在我声明的变量下没有给我任何错误(我的应用程序在发布时仍然崩溃)。 例如:如果我搬家 TextView myDateTextView = (TextView)findViewById(R.id.myDateTextView);到目前为止的任何地方,我都会收到错误信息,说它未被使用,然后onPostExecute()下的错误说无法解决。

我被卡住了:(

编辑2

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

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new      PlaceholderFragment()).commit();
    }
//  Calendar calendar = Calendar.getInstance();
 //   DateFormat format = new SimpleDateFormat("EEEE, MMMM dd, YYYY");

  //  TextView myDateTextView = (TextView)findViewById(R.id.myDateTextView);
 //   String currentDateTimeString = format.format(calendar.getTime());
   // myDateTextView.setText(currentDateTimeString);


    new currentDateTime().execute();

}

   public class currentDateTime extends AsyncTask<Void, Void, String>{
    TextView myDateTextView = (TextView)findViewById(R.id.myDateTextView);
    @Override
    protected String doInBackground(Void... arg0) {
        String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
        String format = "EEEE, MMMM dd, YYYY";
        Date today = new Date(); SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH); System.err.format("%30s %s\n", format, sdf.format(today));

        return currentDateTimeString;
    }
    @Override
    protected void onPostExecute(String currentDateTime) {
        myDateTextView.setText(currentDateTime);
    }   
    }

这里是LogCat

06-14 07:26:15.483: D/gralloc_goldfish(856): Emulator without GPU emulation detected. 06-14 07:26:16.173: W/dalvikvm(856): threadid=11: thread exiting with uncaught exception (group=0x41465700) 06-14 07:26:16.203: E/AndroidRuntime(856): FATAL EXCEPTION: AsyncTask #1 06-14 07:26:16.203: E/AndroidRuntime(856): java.lang.RuntimeException: An error occured while executing doInBackground() 06-14 07:26:16.203: E/AndroidRuntime(856): at android.os.AsyncTask$3.done(AsyncTask.java:299) 06-14 07:26:16.203: E/AndroidRuntime(856): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 06-14 07:26:16.203: E/AndroidRuntime(856): at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 06-14 07:26:16.203: E/AndroidRuntime(856): at java.util.concurrent.FutureTask.run(FutureTask.java:239) 06-14 07:26:16.203: E/AndroidRuntime(856): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 06-14 07:26:16.203: E/AndroidRuntime(856): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 06-14 07:26:16.203: E/AndroidRuntime(856): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 06-14 07:26:16.203: E/AndroidRuntime(856): at java.lang.Thread.run(Thread.java:841) 06-14 07:26:16.203: E/AndroidRuntime(856): Caused by: java.lang.IllegalArgumentException: Unknown pattern character 'Y' 06-14 07:26:16.203: E/AndroidRuntime(856): at java.text.SimpleDateFormat.validateFormat(SimpleDateFormat.java:264) 06-14 07:26:16.203: E/AndroidRuntime(856): at java.text.SimpleDateFormat.validatePattern(SimpleDateFormat.java:319) 06-14 07:26:16.203: E/AndroidRuntime(856): at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:365) 06-14 07:26:16.203: E/AndroidRuntime(856): at com.mikitz.baalpha1blank.MainActivity$currentDateTime.doInBackground(MainActivity.java:49) 06-14 07:26:16.203: E/AndroidRuntime(856): at com.mikitz.baalpha1blank.MainActivity$currentDateTime.doInBackground(MainActivity.java:1) 06-14 07:26:16.203: E/AndroidRuntime(856): at android.os.AsyncTask$2.call(AsyncTask.java:287) 06-14 07:26:16.203: E/AndroidRuntime(856): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 06-14 07:26:16.203: E/AndroidRuntime(856): ... 4 more 06-14 07:26:18.983: I/Choreographer(856): Skipped 36 frames! The application may be doing too much work on its main thread. 06-14 07:26:20.454: I/Process(856): Sending signal. PID: 856 SIG: 9

4 个答案:

答案 0 :(得分:0)

在最后的onCreate()方法中添加此行:

new currentDateTime().execute();

并在onPostExucute()类中使用以下代码添加AsynTask方法。

protected void onPostExecute(String result) {

     myDateTextView.setText(result);

} 

请务必从myDateTextView.setText()移除doInbackGround(),否则会导致异常

答案 1 :(得分:0)

您需要在onPostExecute()方法

中更新UI
protected void onPostExecute(String result) {
 myDateTextView.setText(result);
} 

您可以在AsyncTask上致电onCreate()

new currentDateTime().execute();

答案 2 :(得分:0)

在onCreate()

中调用你的AsyncTask
new currentDateTime().execute();

在onCreate()

中调用inualise你的textview之前

myDateTextView = (TextView)findViewById(R.id.myDateTextView);

更改public class currentDateTime extends AsyncTask<Object, Void, String>

public class currentDateTime extends AsyncTask<Void, Void, String>  // as you're not passing anything while calling AsyncTask.

更改

protected String doInBackground(Object... arg0)

protected String doInBackground(Void... arg0)

doInBackground之后添加onPostExecute()

protected void onPostExecute(String result) {
 myDateTextView.setText(result);
} 

答案 3 :(得分:0)

您不必使用asyncTask。您可以尝试这样做:

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

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit();
    }

    Calendar calendar = Calendar.getInstance();
    DateFormat format = new SimpleDateFormat("EEEE, MMMM dd, YYYY");

    TextView myDateTextView = (TextView)findViewById(R.id.myDateTextView);
    String currentDateTimeString = format.format(calendar.getTime());
    myDateTextView.setText(currentDateTimeString);

    }

你也做错了什么。你的变量名是&#34;格式&#34;这是错误的,因为这是一个类名。你应该把它改成&#34;格式&#34;。

String Format = "EEEE, MMMM dd, YYYY";

String format = "EEEE, MMMM dd, YYYY";