Android应用程序不受空指针异常的影响

时间:2015-04-25 17:17:05

标签: java android-fragments jdbc android-studio nullpointerexception

由于某种原因,我的应用程序因空指针异常而一直崩溃。我正在为每个页面使用带有片段的导航抽屉。我也使用JDBC连接到我的oracle数据库。我遇到的问题是我的两个片段。当我运行程序时,无论我选择第一个片段还是第二个片段,它都可以正常工作。但是,如果我再次选择相同的片段或尝试转到另一个片段,则应用程序崩溃。

我能够找出问题所在。它在尝试呼叫时崩溃:

installDir = "your/install/dir"
...
install.packages("yourPackage", dir=installDir)

第二次。

以下是两个片段的代码:

排名片段:

st = conn.createStatement();

每周比赛片段:

package com.capstone.hammond.wallstreetfantasyleaguefinal;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;


public class StandingsFragment extends Fragment {
    List<Integer> idList = new ArrayList<>();
    List<String> winList = new ArrayList<>();
    List<String> lossList = new ArrayList<>();
    List<String> firstName = new ArrayList<>();
    List<String> lastName = new ArrayList<>();


    int resultID;
    String resultWins;
    String resultLosses;
    String firstNameS;
    String lastNameS;

    Connection conn;
    ResultSet rs, rs1, rs2, rs3, rs4, rs5, rs6;
    Statement st, st1, st2, st3, st4, st5, st6;

    View rootview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.fragment_standings, container, false);
        return rootview;

    }

    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        try {
            List<List> resultList = new Standings().execute().get();
            firstName = resultList.get(0);
            lastName = resultList.get(1);
            winList = resultList.get(2);
            lossList = resultList.get(3);

            TextView player1;
            TextView player2;
            TextView player3;
            TextView player4;
            TextView player5;
            TextView player6;
            TextView player1Win;
            TextView player2Win;
            TextView player3Win;
            TextView player4Win;
            TextView player5Win;
            TextView player6Win;
            TextView player1Loss;
            TextView player2Loss;
            TextView player3Loss;
            TextView player4Loss;
            TextView player5Loss;
            TextView player6Loss;

            player1 = (TextView) view.findViewById(R.id.player1);
            player1.setText(firstName.get(0) + " " + lastName.get(0));
            player2 = (TextView) view.findViewById(R.id.player2);
            player2.setText(firstName.get(1) + " " + lastName.get(1));
            player3 = (TextView) view.findViewById(R.id.player3);
            player3.setText(firstName.get(2) + " " + lastName.get(2));
            player4 = (TextView) view.findViewById(R.id.player4);
            player4.setText(firstName.get(3) + " " + lastName.get(3));
            player5 = (TextView) view.findViewById(R.id.player5);
            player5.setText(firstName.get(4) + " " + lastName.get(4));
            player6 = (TextView) view.findViewById(R.id.player6);
            player6.setText(firstName.get(5) + " " + lastName.get(5));

            player1Win = (TextView) view.findViewById(R.id.player1_win);
            player1Win.setText(winList.get(0));
            player2Win = (TextView) view.findViewById(R.id.player2_win);
            player2Win.setText(winList.get(1));
            player3Win = (TextView) view.findViewById(R.id.player3_win);
            player3Win.setText(winList.get(2));
            player4Win = (TextView) view.findViewById(R.id.player4_win);
            player4Win.setText(winList.get(3));
            player5Win = (TextView) view.findViewById(R.id.player5_win);
            player5Win.setText(winList.get(4));
            player6Win = (TextView) view.findViewById(R.id.player6_win);
            player6Win.setText(winList.get(5));

            player1Loss = (TextView) view.findViewById(R.id.player1_loss);
            player1Loss.setText(lossList.get(0));
            player2Loss = (TextView) view.findViewById(R.id.player2_loss);
            player2Loss.setText(lossList.get(1));
            player3Loss = (TextView) view.findViewById(R.id.player3_loss);
            player3Loss.setText(lossList.get(2));
            player4Loss = (TextView) view.findViewById(R.id.player4_loss);
            player4Loss.setText(lossList.get(3));
            player5Loss = (TextView) view.findViewById(R.id.player5_loss);
            player5Loss.setText(lossList.get(4));
            player6Loss = (TextView) view.findViewById(R.id.player6_loss);
            player6Loss.setText(lossList.get(5));


        } catch(InterruptedException e) {
            e.printStackTrace();
        } catch(ExecutionException e) {
            e.printStackTrace();
        }




    }

    public class Standings extends AsyncTask<Void, Void, List<List>> {

        @Override
        protected List<List> doInBackground(Void... params) {
            try {
                conn = ConnectionManager.getConnection();
                rs = null;
                st = null;
                if (conn != null)
                    st = conn.createStatement();
                if (st != null)
                    rs = st.executeQuery("SELECT * FROM L1_STANDINGS");
                if (rs != null)
                    while(rs.next()) {
                        resultID = rs.getInt("USERID");
                        idList.add(resultID);
                        resultWins = rs.getString("WINS");
                        winList.add(resultWins);
                        resultLosses = rs.getString("LOSSES");
                        lossList.add(resultLosses);
                    }

                st1 = null;
                if (conn != null)
                    st1 = conn.createStatement();
                rs1 = null;
                if (st1 != null)
                    rs1 = st1.executeQuery("SELECT * FROM USERINFO WHERE USERID = " + (idList.get(0)));
                if (rs1 != null)
                    while(rs1.next()) {
                        firstNameS = rs1.getString("FIRSTNAME");
                        firstName.add(firstNameS);
                        lastNameS = rs1.getString("LASTNAME");
                        lastName.add(lastNameS);
                    }
                st2 = null;
                if (conn != null)
                    st2 = conn.createStatement();
                rs2 = null;
                if (st2 != null)
                    rs2 = st2.executeQuery("SELECT * FROM USERINFO WHERE USERID = " + (idList.get(1)));
                if (rs2 != null)
                    while(rs2.next()) {
                        firstNameS = rs2.getString("FIRSTNAME");
                        firstName.add(firstNameS);
                        lastNameS = rs2.getString("LASTNAME");
                        lastName.add(lastNameS);
                    }
                st3 = null;
                if (conn != null)
                    st3 = conn.createStatement();
                rs3 = null;
                if (st3 != null)
                    rs3 = st3.executeQuery("SELECT * FROM USERINFO WHERE USERID = " + (idList.get(2)));
                if (rs3 != null)
                    while(rs3.next()) {
                        firstNameS = rs3.getString("FIRSTNAME");
                        firstName.add(firstNameS);
                        lastNameS = rs3.getString("LASTNAME");
                        lastName.add(lastNameS);
                    }
                st4 = null;
                if (conn != null)
                    st4 = conn.createStatement();
                rs4 = null;
                if (st4 != null)
                    rs4 = st4.executeQuery("SELECT * FROM USERINFO WHERE USERID = " + (idList.get(3)));
                if (rs4 != null)
                    while(rs4.next()) {
                        firstNameS = rs4.getString("FIRSTNAME");
                        firstName.add(firstNameS);
                        lastNameS = rs4.getString("LASTNAME");
                        lastName.add(lastNameS);
                    }
                st5 = null;
                if (conn != null)
                    st5 = conn.createStatement();
                rs5 = null;
                if (st5 != null)
                    rs5 = st5.executeQuery("SELECT * FROM USERINFO WHERE USERID = " + (idList.get(4)));
                if (rs5 != null)
                    while(rs5.next()) {
                        firstNameS = rs5.getString("FIRSTNAME");
                        firstName.add(firstNameS);
                        lastNameS = rs5.getString("LASTNAME");
                        lastName.add(lastNameS);
                    }
                st6 = null;
                if (conn != null)
                    st6 = conn.createStatement();
                rs6 = null;
                if (st6 != null)
                    rs6 = st6.executeQuery("SELECT * FROM USERINFO WHERE USERID = " + (idList.get(5)));
                if (rs6 != null)
                    while(rs6.next()) {
                        firstNameS = rs6.getString("FIRSTNAME");
                        firstName.add(firstNameS);
                        lastNameS = rs6.getString("LASTNAME");
                        lastName.add(lastNameS);
                    }

                List<List> resultList = new ArrayList<>();
                resultList.add(firstName);
                resultList.add(lastName);
                resultList.add(winList);
                resultList.add(lossList);

                return resultList;


            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try{
                    if(rs!=null)
                        rs.close();
                    if(rs1!=null)
                        rs1.close();
                    if(rs2!=null)
                        rs2.close();
                    if(rs3!=null)
                        rs3.close();
                    if(rs4!=null)
                        rs4.close();
                    if(rs5!=null)
                        rs5.close();
                    if(rs6!=null)
                        rs6.close();
                    if(st!=null)
                        st.close();
                    if(st1!=null)
                        st1.close();
                    if(st2!=null)
                        st2.close();
                    if(st3!=null)
                        st3.close();
                    if(st4!=null)
                        st4.close();
                    if(st5!=null)
                        st5.close();
                    if(st6!=null)
                        st6.close();
                    if(conn!=null)
                        conn.close();
                } catch(SQLException e) {
                    e.printStackTrace();
                }

            }
            return null;
        }


    }
}

logcat的:

package com.capstone.hammond.wallstreetfantasyleaguefinal;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;


public class WeeklyMatchFragment extends Fragment {
    Connection conn;
    ResultSet rs, rs1, rs2;
    Statement st, st1, st2;

    View rootview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.fragment_weekly_match, container, false);
        return rootview;

    }

    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        try {

            TextView user1;
            TextView user2;
            TextView bank1;
            TextView bank2;

            List<String> resultList = new MatchDetails().execute().get();

            user1 = (TextView) view.findViewById(R.id.player1);
            user1.setText(UserLoginInfo.fName + " " + UserLoginInfo.lName);
            bank1 = (TextView) view.findViewById(R.id.bank1);
            bank1.setText("Bank: $" + resultList.get(0));
            user2 = (TextView) view.findViewById(R.id.user2);
            user2.setText(resultList.get(1) + " " + resultList.get(2));
            bank2 = (TextView) view.findViewById(R.id.bank2);
            bank2.setText("Bank: $" + resultList.get(3));
        } catch(InterruptedException e) {
            e.printStackTrace();
        } catch(ExecutionException e) {
            e.printStackTrace();
        }


    }

    public class MatchDetails extends AsyncTask<Void, Void, List<String>> {

        @Override
        protected List<String> doInBackground(Void... params) {
            try {
                List<String> resultList = new ArrayList<>();
                conn = ConnectionManager.getConnection();
                rs = null;
                st = null;
                if (conn != null)
                    st = conn.createStatement();
                if (st != null)
                    rs = st.executeQuery("SELECT * FROM L1_STANDINGS WHERE EMAIL = '" + UserLoginInfo.userEmail + "'");
                if (rs != null)
                    while(rs.next()) {
                        resultList.add(rs.getString("BANK"));
                        UserLoginInfo.currOpp = rs.getInt("CURR_OPP");
                    }
                st1 = null;
                if (conn != null)
                    st1 = conn.createStatement();
                rs1 = null;
                if (st1 != null)
                    rs1 = st1.executeQuery("SELECT * FROM USERINFO WHERE USERID = '" + UserLoginInfo.currOpp + "'");
                if (rs1 != null)
                    while(rs1.next()) {
                        resultList.add(rs1.getString("FIRSTNAME"));
                        resultList.add(rs1.getString("LASTNAME"));
                    }
                rs2 = null;
                st2 = null;
                if (conn != null)
                    st2 = conn.createStatement();
                if (st2 != null)
                    rs2 = st.executeQuery("SELECT * FROM L1_Standings WHERE USERID = '" + UserLoginInfo.currOpp + "'");
                if (rs2 != null)
                    while(rs2.next()) {
                        resultList.add(rs2.getString("BANK"));
                    }
                return resultList;
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try{
                    if(rs!=null)
                        rs.close();
                    if(rs1!=null)
                        rs1.close();
                    if(st!=null)
                        st.close();
                    if(st1!=null)
                        st1.close();
                    if(conn!=null)
                        conn.close();
                } catch(SQLException e) {
                    e.printStackTrace();
                }



            }
            return null;
        }


    }
}

任何帮助都将非常感谢!!!

3 个答案:

答案 0 :(得分:0)

onViewCreated中重温您的方法StandingsFragment

检查resultList或您的某个观点(例如player1)是否为空

答案 1 :(得分:0)

我认为在AsyncTask中使用onViewCreated不是正确的方法来进行数据库操作。此外,您正在使用get()方法,onViewCreated也在Thread上运行。

因此,您应该使用简单的函数而不是AsyncTask

<强> Editted
好吧,
第一:我不知道我错了:D。
第二:我仍然不确定在Thread中连接数据库是否必要 第三:我在Runnable做过:(这里是:

public class Items extends Fragment {

    private DBViewOperation mDBViewOp;
    private ArrayList<DashboardItem> items;
    private Runnable runnable;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   mDBViewOp = new DBViewOperation(getActivity());    

   /* It is not funny :/ */ 
   runnable= new Runnable() {
            @Override
            public void run() {
                loadItems();
            }
   }
   getActivity().runOnUiThread(runnable);

}

private void loadItems(){
        items = mDBViewOp.GetItems(groupID);
        int size = items.size();

        int flashCount = 0;//....
}

不要使用conn.createStatement();
检查一下:
Android Sqlite getReadableDatabase

答案 2 :(得分:0)

问题是连接以某种方式关闭,而不是正确地重新打开。通过保持连接打开(不调用conn.close())一切正常。