Java 当我从意念归来的时候

Java 当我从意念归来的时候,java,android,android-intent,Java,Android,Android Intent,我在安卓系统中的意图有问题,当我启动一个新的意图,向我显示不属于我的应用程序的东西,例如,一个网站或一个文本文件时,这一切都可以正常工作,所以问题是当我从网站或文本文件返回到我的应用程序时,我的表布局没有视图,我将代码放在resume()上和onRestart()重建表格布局,但未调用它 有人能帮忙吗 对不起,我的英语不好,解释也不好 public class GridActivity extends AppCompatActivity { private Context context; To

我在安卓系统中的意图有问题,当我启动一个新的意图,向我显示不属于我的应用程序的东西,例如,一个网站或一个文本文件时,这一切都可以正常工作,所以问题是当我从网站或文本文件返回到我的应用程序时,我的表布局没有视图,我将代码
放在resume()上
onRestart()
重建表格布局,但未调用它

有人能帮忙吗

对不起,我的英语不好,解释也不好

public class GridActivity extends AppCompatActivity {
private Context context;
Toolbar mActionBarToolbar;
static Activity activity;
public static String strJSON, poca_conta, poca_enti;
public int t_diario = 300, t_doc = 300, t_desc = 1100, t_data = 400, t_datavenc = 400, t_valor = 400, t_valorliq = 400;
double valor_liq = 0, valor = 0;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Utils.onActivityCreateSetTheme(this);
    setContentView(R.layout.grid_extratos);

    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round);
    ActivityManager.TaskDescription taskDesc = new ActivityManager.TaskDescription(getString(R.string.app_name), bm, fetchPrimaryColor());
    setTaskDescription(taskDesc);

    Bundle bundle=getIntent().getExtras();
    poca_conta = bundle.getString("poca_conta");
    poca_enti = bundle.getString("poca_enti");
    String empresa = bundle.getString("empresa");

    SharedPreferences settings = getSharedPreferences("JSONHELPER", 0);
    strJSON = settings.getString("json", "");
    //strJSON = bundle.getString("json");
    context = this;
    activity = this;
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    int densityDpi = (int)(metrics.density * 160f);
    t_diario = (densityDpi * t_diario) / 400;
    t_doc = (densityDpi * t_doc) / 400;
    t_desc = (densityDpi * t_desc) / 400;
    t_data = (densityDpi * t_data) / 400;
    t_valor = (densityDpi * t_valor) / 400;
    t_valorliq = (densityDpi * t_valorliq) / 400;
    t_datavenc = (densityDpi * t_datavenc) / 400;

    mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar2);
    if(Utils.sTheme == 0) {
        mActionBarToolbar.setPopupTheme(R.style.AppTheme);
    }
    if(Utils.sTheme == 1) {
        mActionBarToolbar.setPopupTheme(R.style.AppThemeLight);
    }
    if(Utils.sTheme == 2) {
        mActionBarToolbar.setPopupTheme(R.style.AppCompat);
    }
    if(Utils.sTheme == 3) {
        mActionBarToolbar.setPopupTheme(R.style.AppCompatLight);
    }
    mActionBarToolbar.setOverflowIcon(getDrawable(R.drawable.ic_action_navigation_more_vert));
    setSupportActionBar(mActionBarToolbar);
    getSupportActionBar().setSubtitle(empresa);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_material);
    upArrow.setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);
    getSupportActionBar().setHomeAsUpIndicator(upArrow);

    FazerHeader();
    FazerTabela();
    FazerFooter();
    FazerAux();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_grid, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.item1:
            final ExportarModalActivity actionbarDialog = new ExportarModalActivity();
            actionbarDialog.classe = "Grid";
            actionbarDialog.show(getSupportFragmentManager(), "action_bar_frag");
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

public static void AbrirCSV() {
    try {
        JSONArray docs = myJSONARRSTATIC;

        File file= new File(activity.getExternalCacheDir(), "TabelaPendentes.csv");
        String csv = CDL.toString(docs);
        FileUtils.writeStringToFile(file, csv);

        Uri path = FileProvider.getUriForFile(activity, activity.getApplicationContext().getPackageName() + ".provider", file);
        Intent opencsv = new Intent(Intent.ACTION_VIEW);
        opencsv.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        opencsv.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        opencsv.setData(path);

        activity.startActivity(opencsv);
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private int fetchPrimaryColor() {
    TypedValue typedValue = new TypedValue();

    TypedArray a = this.obtainStyledAttributes(typedValue.data, new int[] { R.attr.colorPrimary });
    int color = a.getColor(0, 0);

    a.recycle();

    return color;
}

public void FazerHeader() {
    TableRow rowHeader = (TableRow) findViewById(R.id.header);
    rowHeader.removeAllViews();
    rowHeader.setBackgroundColor(fetchPrimaryColor());
    rowHeader.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
            TableLayout.LayoutParams.WRAP_CONTENT));
    String[] headerText = {"Informações", "Valor", "Valor Liquido"};
    for(int i = 0; i < headerText.length; i++) {
        TextView tv = new TextView(this);
        switch (i) {
            case 0 : tv.setLayoutParams(new TableRow.LayoutParams(t_desc,
                    TableRow.LayoutParams.WRAP_CONTENT)); break;
            case 1 : tv.setLayoutParams(new TableRow.LayoutParams(t_valor,
                    TableRow.LayoutParams.WRAP_CONTENT)); break;
            case 2 : tv.setLayoutParams(new TableRow.LayoutParams(t_valorliq,
                    TableRow.LayoutParams.WRAP_CONTENT)); break;
        }
        tv.setGravity(Gravity.CENTER);
        tv.setTextSize(18);
        tv.setTextColor(Color.parseColor("#FFFFFF"));
        tv.setText(headerText[i]);
        rowHeader.addView(tv);
    }
}

JSONArray myJSONARR;
static JSONArray myJSONARRSTATIC;
static JSONObject fnlJSON = null;
public void FazerTabela() {
    TableLayout tableLayout = (TableLayout) findViewById(R.id.tablelayout);
    tableLayout.removeAllViews();

    try {
        myJSONARR = new JSONArray(strJSON);
        myJSONARRSTATIC = myJSONARR;
        num_docarr = new String[myJSONARR.length()];
        tipo_docarr = new String[myJSONARR.length()];
        anoarr = new String[myJSONARR.length()];
        for(int k = 0; k < myJSONARR.length(); k++) {
            JSONObject myJSON = new JSONObject(myJSONARR.get(k).toString());
            fnlJSON = new JSONObject(myJSONARR.get(k).toString());
            Date dataVar = null;
            TableRow row = new TableRow(context);
            row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
            for(int i = 0; i < 9; i++) {
                if(i != 8) {
                    TextView tv = new TextView(this);
                    tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
                    switch (i) {
                        case 0 : tv.setLayoutParams(new TableRow.LayoutParams(t_desc, TableRow.LayoutParams.WRAP_CONTENT));
                            String geral = "";
                            String strDate = myJSON.getString("DATA");
                            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
                            Date date = dateFormat.parse(strDate);
                            dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                            geral = geral + ("   " + "Data: " + dateFormat.format(date) + "\n");
                            geral = geral + ("   " + "Documento: " + myJSON.getString("DIARIO") + "/" + myJSON.getString("DOCUMENTO") + " " + myJSON.getString("DESCRICAO") + "\n");
                            String strDate1 = myJSON.getString("DATA_VENC");
                            dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
                            Date date1 = dateFormat.parse(strDate1);
                            dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                            geral = geral + "   " + "Data Vencimento: " + dateFormat.format(date1) + "\n";
                            tv.setText(geral);
                            tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
                            break;
                        case 1 : tv.setLayoutParams(new TableRow.LayoutParams(t_valor, TableRow.LayoutParams.WRAP_CONTENT));
                            tv.setText(format(myJSON.getDouble("VALOR")) + "€" + "   ");
                            tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
                            valor = valor + myJSON.getDouble("VALOR");
                            break;
                        case 2 : tv.setLayoutParams(new TableRow.LayoutParams(t_valorliq, TableRow.LayoutParams.WRAP_CONTENT));
                            tv.setText(format(myJSON.getDouble("VALOR_LIQ")) + "€" + "   ");
                            tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
                            valor_liq = valor_liq + myJSON.getDouble("VALOR_LIQ");
                            break;
                    }
                    tv.setGravity(Gravity.CENTER);
                    tv.setTextSize(18);
                    if(k != myJSONARR.length() - 1) {
                        tv.setPadding(0, 0, 0, 5);
                    }
                    row.addView(tv);
                }
            }
            num_docarr[k] = myJSON.getString("DOCUMENTO");
            String strDate = myJSON.getString("DATA");
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            Date date = dateFormat.parse(strDate);
            dateFormat = new SimpleDateFormat("yyyy");
            anoarr[k] = dateFormat.format(date);
            tipo_docarr[k] = myJSON.getString("DIARIO");
            final int z = k;
            row.setOnLongClickListener(new View.OnLongClickListener()
            {
                @Override
                public boolean onLongClick(View v) {
                    final MyModalActivity actionbarDialog = new MyModalActivity();
                    actionbarDialog.classe = "Grid";
                    n = z;
                    actionbarDialog.show(getSupportFragmentManager(), "action_bar_frag");
                    return true;
                }
            });
            tableLayout.addView(row);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static String[] num_docarr = null, anoarr = null, tipo_docarr = null;

public static int n;

public static void AbrirPDF() {
    ano = anoarr[n];
    tipo_doc = tipo_docarr[n];
    num_doc = num_docarr[n];
    new VerPDF().execute();
    ProgressBarr();
}

public void FazerAux() {
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            TableRow rowHeader = (TableRow) findViewById(R.id.header);
            TableRow rowFooter = (TableRow) findViewById(R.id.footer);
            ScrollView cenas = (ScrollView) findViewById(R.id.layout);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT
            );
            params.setMargins(0, rowHeader.getHeight(), 0, rowFooter.getHeight());
            cenas.setLayoutParams(params);
        }
    }, 100);
}

public void FazerFooter() {
    TableRow rowFooter = (TableRow) findViewById(R.id.footer);
    rowFooter.removeAllViews();
    rowFooter.setBackgroundColor(fetchPrimaryColor());
    rowFooter.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
            TableLayout.LayoutParams.WRAP_CONTENT));
    String[] footerText = {"Informações", "Valor", "Valor Liquido"};
    for(int i = 0; i < footerText.length; i++) {
        TextView tv = new TextView(this);
        switch (i) {
            case 0:
                tv.setLayoutParams(new TableRow.LayoutParams(t_desc,
                        TableRow.LayoutParams.WRAP_CONTENT));
                break;
            case 1:
                tv.setLayoutParams(new TableRow.LayoutParams(t_valor, TableRow.LayoutParams.WRAP_CONTENT));
                tv.setText(format(valor) + "€" + "   ");
                tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
                break;
            case 2:
                tv.setLayoutParams(new TableRow.LayoutParams(t_valorliq, TableRow.LayoutParams.WRAP_CONTENT));
                tv.setText(format(valor_liq) + "€" + "   ");
                tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
                break;
        }
        tv.setGravity(Gravity.CENTER);
        tv.setTextSize(18);
        tv.setTextColor(Color.parseColor("#FFFFFF"));
        rowFooter.addView(tv);
    }
}

@Override
protected void onRestart() {
    super.onRestart();
    SharedPreferences settings = getSharedPreferences("JSONHELPER", 0);
    strJSON = settings.getString("json", "");
    valor = 0; valor_liq = 0;
    FazerHeader();
    FazerTabela();
    FazerFooter();
    FazerAux();
}

@Override
protected void onResume() {
    super.onResume();
    SharedPreferences settings = getSharedPreferences("JSONHELPER", 0);
    strJSON = settings.getString("json", "");
    valor = 0; valor_liq = 0;
    FazerHeader();
    FazerTabela();
    FazerFooter();
    FazerAux();
}

@Override
protected void onStart() {
    super.onStart();
    SharedPreferences settings = getSharedPreferences("JSONHELPER", 0);
    strJSON = settings.getString("json", "");
    valor = 0; valor_liq = 0;
    FazerHeader();
    FazerTabela();
    FazerFooter();
    FazerAux();
}

public static String format(Double n) {
    DecimalFormat df = new DecimalFormat("#,###,##0.00");
    DecimalFormatSymbols otherSymbols = new   DecimalFormatSymbols(Locale.ENGLISH);
    otherSymbols.setDecimalSeparator(',');
    otherSymbols.setGroupingSeparator('.');
    df.setDecimalFormatSymbols(otherSymbols);
    return df.format(n);
}

@Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}

public static String JSONres, tipo_doc, ano, num_doc;
static int resu;
private static class VerPDF extends AsyncTask<Void, Integer, Integer> {
    protected Integer doInBackground(Void... params) {
        try {
            String url = Utils.backOffice(activity) + "GetPdfPendentes";
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            con.setRequestMethod("POST");
            con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
            con.setRequestProperty("Accept-Charset", "UTF-8");
            con.setConnectTimeout(7500);

            String username = Utils.getUsername();
            String urlParameters = "ano="+ano+"&tipo_doc="+tipo_doc+"&num_doc="+num_doc+"&username="+username;

            con.setDoOutput(true);

            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
            writer.write(urlParameters);
            writer.close();
            wr.close();

            int responseCode = con.getResponseCode();

            Log.d("CENAS", Integer.toString(responseCode));
            if(responseCode != 200) {
                resu = 2;
            } else {
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }

                in.close();

                Log.d("CENAS", response.toString());
                JSONObject rawJSON = XML.toJSONObject(response.toString());
                JSONObject semirawJSON = new JSONObject(rawJSON.get("string").toString());
                JSONres = semirawJSON.getString("content");
                resu = 1;
            }
        } catch (Exception e) {
            return 2;
        }
        return resu;
    }

    protected void onPostExecute(Integer result) {
        if(result == 1) {
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Uri uri = Uri.parse(JSONres);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    activity.startActivity(intent);
                }
            }, 3250);
        } else if(result == 0) {
            Toast.makeText(activity, "ERRO!",
                    Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(activity, "NÃO FOI POSSIVEL ESTABLECER LIGAÇÃO AO SERVIDOR!",
                    Toast.LENGTH_LONG).show();
        }
    }
}
公共类GridActivity扩展了AppCompatActivity{
私人语境;
工具栏图标工具栏;
静态活动;
公共静态字符串strJSON,poca_conta,poca_enti;
公共整数t_diario=300,t_doc=300,t_desc=1100,t_data=400,t_datavenc=400,t_valor=400,t_valorliq=400;
双重英勇=0,英勇=0;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Utils.onActivityCreateSetTheme(this);
setContentView(R.layout.grid_extratos);
位图bm=BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher_round);
ActivityManager.TaskDescription taskDesc=新建ActivityManager.TaskDescription(getString(R.string.app_name),bm,fetchPrimaryColor());
setTaskDescription(任务描述);
Bundle Bundle=getIntent().getExtras();
poca_conta=bundle.getString(“poca_conta”);
poca_enti=bundle.getString(“poca_enti”);
字符串empresa=bundle.getString(“empresa”);
SharedReferences设置=GetSharedReferences(“JSONHELPER”,0);
strJSON=settings.getString(“json”,即“”);
//strJSON=bundle.getString(“json”);
上下文=这个;
活动=此;
DisplayMetrics=getResources().getDisplayMetrics();
int densityDpi=(int)(metrics.density*160f);
t_diario=(密度dpi*t_diario)/400;
t_doc=(密度dpi*t_doc)/400;
t_desc=(密度dpi*t_desc)/400;
t_数据=(密度dpi*t_数据)/400;
t值=(密度dpi*t值)/400;
t_valorliq=(密度dpi*t_valorliq)/400;
t_datavenc=(密度dpi*t_datavenc)/400;
mActionBarToolbar=(工具栏)findViewById(R.id.toolbar2);
如果(Utils.sTime==0){
设置弹出主题(R.style.AppTheme);
}
if(Utils.sTheme==1){
mActionBarToolbar.setPopupTheme(R.style.AppThemeLight);
}
if(Utils.sTheme==2){
mActionBarToolbar.setPopupTheme(R.style.AppCompat);
}
if(Utils.sTheme==3){
mActionBarToolbar.setPopupTheme(R.style.AppCompatLight);
}
mActionBarToolbar.setOverflowIcon(getDrawable(R.drawable.ic_action_navigation_more_vert));
设置支持操作栏(mActionBarToolbar);
getSupportActionBar().setSubtitle(empresa);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
最终可绘制向上箭头=getResources().getDrawable(R.Drawable.abc_ic_ab_back_material);
setColorFilter(getResources().getColor(R.color.white),PorterDuff.Mode.SRC_);
getSupportActionBar().setHomeAsUpIndicator(向上箭头);
FazerHeader();
FazerTabela();
FazerFooter();
FazerAux();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.menu\u网格,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id.1:
最终ExportarModalActivity actionbarDialog=新的ExportarModalActivity();
actionbarDialog.classe=“网格”;
show(getSupportFragmentManager(),“action\u bar\u frag”);
返回true;
违约:
返回super.onOptionsItemSelected(项目);
}
}
公共静态无效AbrirCSV(){
试一试{
JSONArray docs=没药;
File File=新文件(activity.getExternalCacheDir(),“TabelaPendentes.csv”);
字符串csv=CDL.toString(文档);
FileUtils.writeStringToFile(文件,csv);
Uri路径=FileProvider.getUriForFile(活动,活动.getApplicationContext().getPackageName()+“.provider”,文件);
Intent opencsv=新的Intent(Intent.ACTION\u视图);
opencsv.setFlags(Intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
setFlags(Intent.FLAG\u GRANT\u READ\u URI\u权限);
opencsv.setData(路径);
activity.startActivity(opencsv);
}捕获(JSONException e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
private int fetchPrimaryColor(){
TypedValue TypedValue=新的TypedValue();
TypedArray a=this.obtainStyleDatAttributes(typedValue.data,newint[]{R.attr.colorPrimary});
int color=a.getColor(0,0);
a、 回收();
返回颜色;
}
public void FazerHeader(){
TableRow rowHeader=(TableRow)findViewById(R.id.header);
rowHeader.removeAllViews();
setBackgroundColor(fetchPrimaryColor());
rowHeader.setLayoutParams(新的TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_父级,
TableLayout.LayoutParams.WRAP_CONTENT);
String[]headerText={“Informações”,“Valor”,“Valor Liquido”};
对于(int i=0;i