show dialogprogress时,Activity已泄露窗口

时间:2016-08-11 15:07:39

标签: android android-fragments

我正在尝试在片段中生成列表视图。代码显示没有错误但是当我尝试启动应用程序时,在progressdialog显示之后,应用程序crashed.below是我的show_fish.java文件。请帮忙。我是android开发的新手。

show_fish.java

package com.rapier.tehranbargh;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.telecom.Connection;
import android.util.Log;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import layout.Ghabz;
import layout.SoratHesabFragment;
import layout.info;
import layout.masaref;
import layout.news;

public class Show_fish extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private ProgressDialog progressDialog;
    JSONParser jsonParser = new JSONParser();
    ArrayList<HashMap<String, String>> hashMaps;
    JSONArray jsonArray = null;
    private final String url = "http://192.168.1.106:8080/ghabz.php";
    ListView listview;

    TextView EmailView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_fish);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
         listview = (ListView) findViewById(R.id.listView);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);


        if (savedInstanceState == null) {
            // on first time to display view for first navigation item based on the number
            displayView(1); // 2 is your fragment's number for "CollectionFragment"
            FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
            tx.replace(R.id.content_frame, new news());
            tx.commit();
        }


        new newse().execute();
    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.show_fish, 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);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        displayView(item.getItemId());
        return true;
    }

    public void displayView(int viewId) {

        Fragment fragment = null;
        String title = getString(R.string.rapier);

        switch (viewId) {
            case R.id.nav_news:
                fragment = new news();
                title = " اخبار ";
                break;

            case R.id.nav_info:
                fragment = new info();
                title = " اطلاعات عمومی ";
                break;

            case R.id.nav_billam:
                fragment = new SoratHesabFragment();
                title = " صورت حساب ها ";

                break;
            case R.id.nav_use:
                fragment = new masaref();
                title = " مصارف ";
                break;
            case R.id.nav_ghabz:
                fragment = new Ghabz();
                title = " قبض ";
                break;

        }

        if (fragment != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_frame, fragment);
            ft.commit();
        }

        // set the toolbar title
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(title);
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);

    }


















class  newse extends AsyncTask<String,String,String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog =new ProgressDialog(Show_fish.this);
            progressDialog.setMessage("Loading");
            progressDialog.show();
        }

        @Override
        protected String doInBackground(String... strings) {
            List<NameValuePair> params= new ArrayList<>();
            JSONObject json=jsonParser.makeHttpRequest(url,"GET",params);

            try {
                int t=json.getInt("t");
                if(t==1){
                    jsonArray=json.getJSONArray("news");
                    for (int i = 0 ;i<jsonArray.length();i++){
                        JSONObject c = jsonArray.getJSONObject(i);
                        String ID=c.getString("ID");
                        String TitrNews=c.getString("TitrNews");
                        String MiniMatnNews=c.getString("MiniMatnNews");
                        String MatnNews=c.getString("MatnNews");
                        String TArikh=c.getString("TArikh");
                        HashMap<String,String> map = new HashMap<String, String>();
                        map.put("ID",ID);
                        map.put("TitrNews",TitrNews);
                        map.put("MiniMatnNews",MiniMatnNews);
                        map.put("MatnNews",MatnNews);
                        map.put("TArikh",TArikh);

                        hashMaps.add(map);
                    }


                }
                else {
                    Toast.makeText(Show_fish.this," اطلاعاتی دریافت نشد ",Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            progressDialog.cancel();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ListAdapter adapter = new SimpleAdapter(Show_fish.this, hashMaps, R.id.listView, new String[]{"TitrNews", "MiniMatnNews", "TArikh"}, new int[]{R.id.titr, R.id.mtn, R.id.data});
                    listview.setAdapter(adapter);
                }
            });
        }



    }


}
带有listview的

片段

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="layout.news">

    <!-- TODO: Update blank fragment layout -->

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/listView"
            android:layout_gravity="center_horizontal" />

</LinearLayout>

news.java 带有列表视图的片段的java文件

public class news extends Fragment {


    public news() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_news, container, false);
    }


}

并且误解了她

android.view.WindowLeaked: Activity com.rapier.tehranbargh.Show_fish has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{3cdf88f6 V.E..... R......D 0,0-729,232} that was originally added here
   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
   at android.app.Dialog.show(Dialog.java:298)
   at com.rapier.tehranbargh.Show_fish$newse.onPreExecute(Show_fish.java:205)
   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
   at android.os.AsyncTask.execute(AsyncTask.java:535)
   at com.rapier.tehranbargh.Show_fish.onCreate(Show_fish.java:90)
   at android.app.Activity.performCreate(Activity.java:5933)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
   at android.app.ActivityThread.access$800(ActivityThread.java:144)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:135)
   at android.app.ActivityThread.main(ActivityThread.java:5221)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

1 个答案:

答案 0 :(得分:0)

将检查添加为:

if(progressDialog != null && progressDialog.isShowing()) {
    progressDialog.dismiss();
}