我的程序不会从另一个类更新textview,它每次都会退出,我也不知道为什么? - java

顶级活动

此活动是我其他班次“发布活动”中收到的显示更新的活动

这两个程序的目标是来回发送显示,从一个屏幕转到另一个屏幕,并更新发布活动类中主要活动的日历显示。如果有人有任何建议,那将非常有帮助!

    package com.example.oife;

import android.content.Context;
import android.media.Image;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ShareActionProvider;
import android.widget.TextView;
import android.content.Intent;
import android.view.View;

import java.text.SimpleDateFormat;

public class TopLevelActivity extends Activity {


    public TextView mText;
    //Context context;
   // private ShareActionProvider shareActionProvider;

    /*public TopLevelActivity(Context context){
        this.context = context;

    }*/

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

        mText = (TextView) findViewById(R.id.calendarView);

        long date = System.currentTimeMillis();

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

        SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
        String dateString = sdf.format(date);
        displayDate.setText(dateString);
    }



   /* @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_oife, menu);
        MenuItem menuItem = menu.findItem(R.id.action_settings);
        shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
        //setIntent("This is example text");
        return super.onCreateOptionsMenu(menu);
    }*/

    /*private void setIntent(String text) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, text);
        shareActionProvider.setShareIntent(intent);
    }*/

  /*  @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        switch (item.getItemId()) {
            case R.id.action_settings
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }
*/
    //call onPostClick() method when the button is clicked
    public void onPostClick (View view) {

        Intent intent = new Intent(this, PostActivity.class);

        startActivity(intent);
    }

    //call onHomeClick() method when the button is clicked
    public void onHomeClick (View view){

        Intent intent = new Intent(this,TopLevelActivity.class);
        startActivity(intent);
    }

    //call onMoreClick() method when the button is clicked
    public void onMoreClick(View view){

        Intent intent = new Intent(this, MoreActivity.class);
        startActivity(intent);
    }



    public TextView getMainText(){
        return mText;
    }

    public void setMText(TextView t){


        mText.setText(t.getText());
    }

}

现在这里是发布活动

此类具有更新显示功能,该功能应更新另一个类别中的textview日历

package com.example.oife;

import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.content.Intent;
import android.view.View;
import android.content.Context;

public class PostActivity extends Activity {

    private TextView pDisplayDate;
    private Button pPickDate;
    private int pYear;
    private int pMonth;
    private int pDay;
    private TextView MAINText;
    static final int DATE_DIALOG_ID = 0;
    private TextView day;
    private TextView xyz;
    TopLevelActivity top;
    TextView d ;
    RadioGroup t;
    RadioGroup g;
    protected TopLevelActivity context;


    public PostActivity(Context context){
        this.context = (TopLevelActivity) context;
    }


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



        /** Capture our View elements */
        pDisplayDate = (TextView) findViewById(R.id.displayDate);
        pPickDate = (Button) findViewById(R.id.pickDate);
        xyz = (TextView) findViewById(R.id.name);
       top = new TopLevelActivity();
        /** Listener for click event of the button */
        pPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });
        t = (RadioGroup) findViewById(R.id.TOD);
       d = (TextView) findViewById(R.id.description);
        g = (RadioGroup) findViewById(R.id.WoW);

        MAINText = new TextView(this);
        day = new TextView(this);

        /** Get the current date */
        final Calendar cal = Calendar.getInstance();
        pYear = cal.get(Calendar.YEAR);
        pMonth = cal.get(Calendar.MONTH);
        pDay = cal.get(Calendar.DAY_OF_MONTH);

        /** Display the current date in the TextView */
       // updateDisplay();

    }



   public void setMainText(){

       day.setText(makeDay().getText());

        this.MAINText.setText(top.getMainText() + "/n/n" + xyz + "/n" + day + "/n" + d.getText());

    }

    private TextView makeDay(){
        TextView ToD;
        int id = t.getCheckedRadioButtonId();
        if(id == -1) {
            ToD = null;
        }
        else{
         ToD = (TextView) findViewById(id);
        }

        /*TextView wow;

        id = g.getCheckedRadioButtonId();
        if (id == -1){
            wow = null;
        }
        else{
            wow = (TextView) findViewById(id);
        }
        */

        TextView d = (TextView) findViewById(R.id.displayDate);

        String s = String.valueOf(d);
        String j;
        if (ToD.getText().equals("Morning")){
            j = "9:00";
        }
        else if(ToD.getText().equals("Midday")){
            j = "12:00";
        }
        else if (ToD.getText().equals("Evening")){
            j = "5:00";
        }
        else {
            j = "10:00";
        }

        /*String l;

        if (wow.getText().equals("Weekend")){
           l="Sunday";
        }
        else {
            l="Wednesday";
        }*/

        TextView time = new TextView(this);

        time.setText( j + " " + s );

        return  time;

    }

    private DatePickerDialog.OnDateSetListener pDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {
                    pYear = year;
                    pMonth = monthOfYear;
                    pDay = dayOfMonth;
                    updateDisplay();
                    displayToast();
                }
            };

    public void updateDisplay() {
        pDisplayDate.setText(
                new StringBuilder()
                        // Month is 0 based so add 1
                        .append(pMonth + 1).append("/")
                        .append(pDay).append("/")
                        .append(pYear).append(" "));
    }

    public void updateMainDisplay(){

        setMainText();

        top.setMText(MAINText);

    }

    /*public void updateTV(final String str1){
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                context.getMainText().setText(str1);
            }
        });
    }*/

    public void displayToast() {
        Toast.makeText(this, new StringBuilder().append("Date choosen is ").append(pDisplayDate.getText()), Toast.LENGTH_SHORT).show();}



    /** Create a new dialog for date picker */
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this,
                        pDateSetListener,
                        pYear, pMonth, pDay);
        }
        return null;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_post, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public void onCreateClick(View view){

        updateMainDisplay();

        Intent intent = new Intent(this, TopLevelActivity.class);
        startActivity(intent);

    }


}

错误记录在这里:

 --------- beginning of crash
01-04 11:56:16.368    2057-2057/com.example.oife E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.oife, PID: 2057
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.oife/com.example.oife.PostActivity}: java.lang.InstantiationException: java.lang.Class<com.example.oife.PostActivity> has no zero argument constructor
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.InstantiationException: java.lang.Class<com.example.oife.PostActivity> has no zero argument constructor
            at java.lang.Class.newInstance(Native Method)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

参考方案

这是非常错误的:

top = new TopLevelActivity();

您无法创建活动实例以使用某些值对其进行更新,您必须将此值传递给现有实例。为此,请使用startActivityForResult创建新活动,并将其接收到TopLevelActivy.onActivityResult中。有关更多信息,请参见:http://developer.android.com/training/basics/intents/result.html

Android Firebase:将数据保存到数据库 - java

我正在尝试从Firebase保存和检索数据,但是在获取正确的语法时遇到了一些麻烦。用户通过电子邮件和密码的身份验证方法进行注册,并使用相同的详细信息登录。我有两个编辑文本字段,要求输入名称和语句。在两个编辑文本框下面有一个按钮,用于将数据保存到数据库。private void saveQuote(){ String name = author.getText…

单击后退按钮时,滑行加载的图像会重置 - java

我正在滑行加载图像。但是问题是,当我们按下后退按钮时,从视图中清除加载的图像。无论我在活动图像视图中还是在recyclerview中加载图像,我都面临着这个问题。我正在加载图像GlideApp.with(this) .load(url) .into(mToolbarAvatar); 参考方案 在代码中添加两行.skipMemoryCache(true) .d…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

AppCompat不支持当前主题 - java

我的应用在Android N上运行正常,但在Android M上的setContentView(R.layout.activity_main)崩溃: Caused by: java.lang.IllegalArgumentException: AppCompat does not support the current theme features: { w…

java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…