活动与onStart()崩溃,但与onCreate()一起使用

时间:2015-11-09 20:56:34

标签: java android

我的问题是:当我尝试使用(defun compute-cost (fixed-cost transaction-type) (+ fixed-cost (variable-cost transaction-type))) (defun variable-cost (transaction-type) (case transaction-type ((employee) 2) ; oh, an extra case we'd forgotten about! ((discount) 3) (t 4))) 方法从数据库中获取数据时,它会崩溃。

我希望每次调用主页活动时都从数据库中获取数据,并且我无法从onStart()方法内部调用必要的方法。

onCreate()

}

这是整个错误堆栈:

public class Homepage extends FragmentActivity {

Button buttonAvailability;

ListView mDrawerList;
RelativeLayout mDrawerPane;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
String email;
View backgroundimage;
Drawable background;
ListView listView;

ArrayList<NavItem> mNavItems = new ArrayList<>();
final HashMap<String, User> allUser = new HashMap<>();
final HashMap<String, Disponibilita> allDisponibilita = new HashMap<>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen_homepage);

    backgroundimage = findViewById(R.id.drawerLayout);
    background = backgroundimage.getBackground();
    background.setAlpha(60);

    setupDrawerToggle();

    mNavItems.add(new NavItem("Home", "Go to homepage", R.drawable.homepage));
    mNavItems.add(new NavItem("Preferences", "Change your preferences", R.drawable.ingr_icon));
    mNavItems.add(new NavItem("Storico Attività", "Visuallizza tutte le attività passate e future", R.drawable.attivita));
    mNavItems.add(new NavItem("Logout", "Logout from application", R.drawable.logout_icon));

    // Populate the Navigation Drawer with options
    mDrawerPane = (RelativeLayout) findViewById(R.id.drawerPane);
    mDrawerList = (ListView) findViewById(R.id.navList);
    MenuAdapter adapter = new MenuAdapter(this, mNavItems);
    mDrawerList.setAdapter(adapter);
    buttonAvailability = (Button) findViewById(R.id.button);

    Intent intent = getIntent();
    email = intent.getStringExtra("email");



    final UserParser userParser = new UserParser();
    final LinkedList<JSONObject> listUser = new LinkedList<>();

    //Quando avvio la homepage prendo tutti gli utenti e li inserisco in un hashmap, poi prendo l'utente che
    //mi interessa e apro la sua homepage

    UserDao.getAllUser(listUser, new OnRestApiResultListener() {
        @Override

        public void onResult(Integer result) {

            if (result > 0) {
                User user = null;
                try {
                    for (JSONObject object : listUser) {
                        user = userParser.parse(object);
                        allUser.put(user.getEmail(), user);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                user = allUser.get(email);
                TextView text = (TextView) findViewById(R.id.textViewWelcome);
                String msg = "Welcome Dr. " + user.getSurname() + " " + user.getName();
                text.setText(msg);
                text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
            }
        }

        @Override
        public void onError(String message) {
            Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG).show();
        }

    });


    final DisponibilitaParser dispParser = new DisponibilitaParser();
    final LinkedList<JSONObject> listDisponibilita = new LinkedList<>();


    DisponibilitaDao.getAllDisponibilita(listDisponibilita, new OnRestApiResultListener() {
        @Override
        public void onResult(Integer result) {
            Disponibilita disp = null;
            //if (listDisponibilita.size()!=0) {

            Log.d("LOL", String.valueOf(listDisponibilita.size()));
            try {
                for (JSONObject object : listDisponibilita) {
                    disp = dispParser.parse(object);
                    allDisponibilita.put(disp.getId(), disp);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //}
        }

        @Override
        public void onError(String message) {
            Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG).show();
        }
    });

    final LinkedList<JSONObject> listRequest = new LinkedList<>();
    final ArrayList<Richiesta> jsonArrayConfermate = new ArrayList<>();
    final ArrayList<Richiesta> jsonArrayNonConfermate = new ArrayList<>();
    final RichiestaParser ricParser = new RichiestaParser();

    RichiestaDao.getAllRichiesteRicevute(email, listRequest, new OnRestApiResultListener() {
        JSONObject temp;

        @Override
        public void onResult(Integer result) {
            for (int i = 0; i < listRequest.size(); i++) {
                temp = listRequest.get(i);
                try {
                    if (temp.getInt("Confermata") == 1)
                        jsonArrayConfermate.add(ricParser.parse(temp));
                    else if (temp.getInt("Confermata") == 0)
                        jsonArrayNonConfermate.add(ricParser.parse(temp));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            setAppointmentAdapter(jsonArrayConfermate);
            setRequestAdapter(jsonArrayNonConfermate);
        }

        @Override
        public void onError(String message) {
            Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG).show();
        }
    });

    // Drawer Item click listeners
    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            if (position == 0) {
                Toast.makeText(getBaseContext(), "This is the homepage!", Toast.LENGTH_LONG).show();
            } else if (position == 1) {
                Intent intent = new Intent(Homepage.this, UserPreferences.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                intent.putExtra("email", email);
                startActivity(intent);
            } else if (position == 2) {
                Intent intent = new Intent(Homepage.this, HistoricalActivities.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                intent.putExtra("email", email);
                startActivity(intent);
            } else if (position == 3) {
                Intent intent = new Intent(Homepage.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }

            mDrawerLayout.closeDrawer(GravityCompat.START);
        }
    });

    buttonAvailability.setOnClickListener(new ClickHandlerInsert());
}

@Override
protected void onStart() {
    super.onStart();  // Always call the superclass method first
    Log.d("Homepage:","Chiamato onStart");

}

@Override
protected void onRestart() {
    super.onRestart();  // Always call the superclass method first

    // Activity being restarted from stopped state
    Log.d("Homepage:","Chiamato onRestart");
}

@Override
protected void onStop() {
    super.onStop();  // Always call the superclass method first
    Log.d("Homepage:", "Chiamato onStop");
}


private void setAppointmentAdapter(ArrayList<Richiesta> appuntamenti){

    AppointmentFragment frag = (AppointmentFragment)getFragmentManager().findFragmentById(R.id.appointments);
    ArrayList<RichiestaAdapterHomepage> appointments = new ArrayList<>();

    int size = appuntamenti.size();
    if(size == 0){
        frag.setText("There are no appointments!");
    }
    else{
        frag.setText("The next appointments are:");

        // Create the adapter to convert the array to views

        Disponibilita temp;
        for(Richiesta ric : appuntamenti){
            temp = allDisponibilita.get(ric.getIdDisponibilita());
            appointments.add( new RichiestaAdapterHomepage(ric.getId(), temp.getGiorno(), temp.getCitta(), allUser.get(ric.getMedicoRichiedente()).getSurname() + " " + allUser.get(ric.getMedicoRichiedente()).getName()) );
        }

        Collections.sort(appointments);
        AppointmentAdapter adapter;
        if(appointments.size() > 3)
            adapter = new AppointmentAdapter( getApplicationContext(), appointments.subList(0, 3) );
        else
            adapter = new AppointmentAdapter( getApplicationContext(), appointments );
        // Attach the adapter to a ListView
        ListView listView = (ListView) findViewById(R.id.listAppointments);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getBaseContext(), "Click element in position: " + position, Toast.LENGTH_LONG).show();
            }
        });
    }
    getFragmentManager().beginTransaction().replace(R.id.appointments, frag).addToBackStack( null ).commit();
}

private void setRequestAdapter(ArrayList<Richiesta> richieste){

    RequestFragment frag = (RequestFragment)getFragmentManager().findFragmentById(R.id.requests);
    ArrayList<RichiestaAdapterHomepage> requests = new ArrayList<>();

    int size = richieste.size();
    if(size == 0){
        frag.setText("There are no new requests!");
    }
    else{
        if (size == 1){
            frag.setText("There is a new request:");
        }
        else {
            frag.setText("There are " + size + " new requests:");
        }

        for(Richiesta ric : richieste){
            requests.add( new RichiestaAdapterHomepage( ric.getId(), allDisponibilita.get(ric.getIdDisponibilita()).getGiorno(), "", allUser.get(ric.getMedicoRichiedente()).getSurname() + " " + allUser.get(ric.getMedicoRichiedente()).getName()) );
        }
        // Create the adapter to convert the array to views
        Collections.sort(requests);
        RequestAdapter adapter = new RequestAdapter(getApplicationContext(), requests, frag.getText());
        // Attach the adapter to a ListView
        listView = (ListView) findViewById(R.id.listRequests);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getBaseContext(), "Click element in position: " + position, Toast.LENGTH_LONG).show();
            }
        });
    }
    getFragmentManager().beginTransaction().replace(R.id.requests, frag).addToBackStack( null ).commit();
}

private void setupDrawerToggle() {
    final ActionBar actionBar = getActionBar();
    if ( actionBar != null ) {
        actionBar.setLogo(R.drawable.ham_icon);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
    }

    mDrawerLayout = ( DrawerLayout ) findViewById( R.id.drawerLayout );

    mDrawerToggle = new ActionBarDrawerToggle(
            this,
            mDrawerLayout,
            new Toolbar( Homepage.this ),
            R.string.apri,
            R.string.chiudi
    ) {

        @Override
        public void onDrawerClosed( View drawerView ) {
            super.onDrawerClosed( drawerView );
            invalidateOptionsMenu();
            if ( actionBar != null ) actionBar.show();
        }

        @Override
        public void onDrawerOpened( View drawerView ) {
            if ( actionBar != null && actionBar.isShowing() ) {
                actionBar.hide();
            }
            super.onDrawerOpened( drawerView );
            invalidateOptionsMenu();
        }
    };
    mDrawerToggle.setDrawerIndicatorEnabled(true);

    if(mDrawerLayout != null)
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    //mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
}

@Override
protected void onPostCreate( Bundle savedInstanceState ) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public boolean onMenuItemSelected( int featureId, MenuItem item ) {
    switch ( item.getItemId() ) {
        case android.R.id.home:
            getActionBar().hide();
            break;
    }
    return mDrawerToggle.onOptionsItemSelected( item ) || super.onOptionsItemSelected( item );
}


private class ClickHandlerInsert implements View.OnClickListener{

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Homepage.this, Availability.class);
        intent.putExtra("email", email);
        //startActivityForResult(intent, 1);
        startActivity(intent);
    }
}

这是RequestFragment

11-09 16:17:28.584      925-925/com.example.simone.consulenzeapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.example.simone.consulenzeapp.FRAGMENT.RequestFragment.setText(RequestFragment.java:23)
        at com.example.simone.consulenzeapp.ACTIVITY.Homepage.setRequestAdapter(Homepage.java:292)
        at com.example.simone.consulenzeapp.ACTIVITY.Homepage.access$100(Homepage.java:53)
        at com.example.simone.consulenzeapp.ACTIVITY.Homepage$3.onResult(Homepage.java:186)
        at com.example.simone.consulenzeapp.DAO.RichiestaDao$4.onPostExecute(RichiestaDao.java:188)
        at com.example.simone.consulenzeapp.DAO.RichiestaDao$4.onPostExecute(RichiestaDao.java:151)
        at android.os.AsyncTask.finish(AsyncTask.java:631)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

}

0 个答案:

没有答案