简体   繁体   English

获取空对象引用如果我在第三项活动之前开始第二项活动(如果我直接从第一项移到第三项,则没有错误)

[英]Getting null object reference If i start 2nd activity before 3rd activity (No error if I move from 1st to 3rd directly)

I know its a common error , and i know lots of topics here were asking about the same error, but i tried alot of solutions and non works. 我知道这是一个常见错误,并且我知道这里有很多主题都在问同样的错误,但是我尝试了很多解决方案和无效方法。 My application is like this: 我的应用程序是这样的:

1st activity is a sign in activity, 2nd is a menu to navigate where to go, 3rd is the customer's details. 第一个活动是登录活动,第二个是导航位置的菜单,第三个是客户的详细信息。

I think i know where the problem is but i don't whats causing it 我想我知道问题出在哪里,但我不知道是什么引起的

In the 2nd activity i am calling a function to get the customer id (the same function i am calling in the 3rd activity but without taking all the details i am only taking it's ID because i need it in other activities ) 在第二个活动中,我正在调用获取客户ID的函数(在第三个活动中,我正在调用相同的函数,但没有获取所有详细信息,我只获取了它的ID,因为在其他活动中我需要它)

So result i am getting second time is always null , which is causing this error 因此,我第二次得到的结果始终为null,这导致此错误

so if i jump directly from 1st to 3rd app doesn't crash. 因此,如果我直接从第一应用跳到第三应用,则不会崩溃。

but (1st 2nd 3rd ) then the function will be called twice (even though i am storing data in a different object) and works only at the first time it's called 但是(1st 2nd 3rd)然后该函数将被调用两次(即使我将数据存储在另一个对象中)并且仅在第一次调用时起作用

Hope i explained it well 希望我解释得好

now my code for 2nd activity: 现在我第二个活动的代码:

public class AfterLogin extends AppCompatActivity {
    @Override
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new AsyncpkAbone().execute(SharedValues.AboneKod);
        setContentView(R.layout.activity_after_login);


    }
    public void AboneBilgiPressed(View v){
        Intent i = new Intent(AfterLogin.this, UserDetailsActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(i);


    }


    protected class AsyncpkAbone extends AsyncTask<String,Void,UserDetailsTable>
    {

        @Override
        protected UserDetailsTable doInBackground(String... params) {
            // TODO Auto-generated method stub
            UserDetailsTable userDetail2=null;
            RestAPI api = new RestAPI();
            try {

                JSONObject jsonObj = api.GetUserDetails(params[0]);
                JSONParser parser = new JSONParser();
                userDetail2 = parser.parseUserDetails(jsonObj);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("AsyncUserDetails", e.getMessage());

            }

            return userDetail2;
        }

        @Override
        protected void onPostExecute(UserDetailsTable result2) {
            // TODO Auto-generated method stub

            SharedValues.AboneKod =result2.getAboneKod();
            SharedValues.pkAbone = result2.getPkAbone();

        }

    }

the Code for the 3rd activity (user details) 第三项活动的代码(用户详细信息)

public class UserDetailsActivity extends AppCompatActivity {

    TextView tvAdres, tvTelefon,tvpkAbone;
    String Adres;
    String WEBParola;
    String Tel1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_details);
        new AsyncUserDetails().execute(SharedValues.AboneKod);


        tvAdres = (TextView) findViewById(R.id.tv_firstname);
        tvAdres.setTextIsSelectable(true);

        tvTelefon = (TextView) findViewById(R.id.tv_lastname);
        tvTelefon.setTextIsSelectable(true);

        tvpkAbone = (TextView) findViewById(R.id.tv_pkAbone);
        tvpkAbone.setTextIsSelectable(true);

        tvAdres.setText(Adres);
        tvTelefon.setText(Tel1);
        tvpkAbone.setText(String.valueOf( SharedValues.pkAbone));

    }

    protected class AsyncUserDetails extends AsyncTask<String,Void,UserDetailsTable>
    {

        @Override
        protected UserDetailsTable doInBackground(String... params) {
            // TODO Auto-generated method stub
            UserDetailsTable userDetail=null;
            RestAPI api = new RestAPI();
            try {

                JSONObject jsonObj = api.GetUserDetails(params[0]);
                JSONParser parser = new JSONParser();
                userDetail = parser.parseUserDetails(jsonObj);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("AsyncUserDetails", e.getMessage());

            }

            return userDetail;
        }

        @Override
        protected void onPostExecute(UserDetailsTable result) {
            // TODO Auto-generated method stub

            tvAdres.setText(result.getAdres());
            tvTelefon.setText(result.getTelefon());

        }

    }

the data i get from the function is stored in a object of type (userdetails tables) 我从函数中获取的数据存储在类型的对象(userdetails表)中

the code for the Userdetailstable is (might be needed) Userdetailstable的代码是(可能需要)

package com.artyazilim.art;

public class UserDetailsTable {

    String Adres,Tel1,AboneKod,WEBParola;
    int pkAbone;
    public UserDetailsTable(String Adres, String Tel1, String AboneKod,
                            String WEBParola,int pkAbone) {
        super();
        this.Adres = Adres;
        this.Tel1 = Tel1;
        this.AboneKod = AboneKod;
        this.WEBParola = WEBParola;
        this.pkAbone = pkAbone;
    }

    public UserDetailsTable() {
        super();
        this.Adres = null;
        this.Tel1 = null;
        this.AboneKod = null;
        this.WEBParola = null;
        this.pkAbone = 0;
    }

    public String getAdres() {
        return Adres;
    }

    public void setAdres(String adres) {
        Adres = adres;
    }

    public String getTelefon() {
        return Tel1;
    }

    public void setTelefon(String telefon) {
        Tel1 = telefon;
    }

    public String getAboneKod() {
        return AboneKod;
    }

    public void setAboneKod(String aboneKod) {
        AboneKod = aboneKod;
    }

    public String getWEBParola() {
        return WEBParola;
    }

    public void setWEBParola(String WEBParola) {
        this.WEBParola = WEBParola;
    }

    public int getPkAbone() {
        return pkAbone;
    }

    public void setPkAbone(int pkAbone) {
        this.pkAbone = pkAbone;
    }
}

the function which i am calling in the both Async is this: 我在两个异步中调用的函数是这样的:

 public JSONObject GetUserDetails(String AboneKod) throws Exception {
        JSONObject result = null;
        JSONObject o = new JSONObject();
        JSONObject p = new JSONObject();
        o.put("interface","Service1");
        o.put("method", "GetUserDetails");
        p.put("AboneKod",mapObject(AboneKod));
        o.put("parameters", p);
        String s = o.toString();
        String r = load(s);
        result = new JSONObject(r);
        return result;
    }

and in the web service this is the GetUserDetails function: 在Web服务中,这是GetUserDetails函数:

public DataTable GetUserDetails(string AboneKod)
{       
    DataTable userDetailsTable = new DataTable();
    userDetailsTable.Columns.Add(new DataColumn("Adres", typeof(String)));
    userDetailsTable.Columns.Add(new DataColumn("Tel1", typeof(String)));
    userDetailsTable.Columns.Add(new DataColumn("pkAbone", typeof(String)));

    if (dbConnection.State.ToString() == "Closed")
    {
        dbConnection.Open();
    }

    string query = "SELECT Adres,Tel1,pkAbone FROM r_Abone WHERE AboneKod='" + AboneKod + "';";

    SqlCommand command = new SqlCommand(query, dbConnection);
    SqlDataReader reader = command.ExecuteReader();

    if (reader.HasRows)
    {
        while (reader.Read())
        {
            userDetailsTable.Rows.Add(reader["Adres"], reader["Tel1"], reader["pkAbone"]);
        }
    }

    reader.Close();
    dbConnection.Close();
    return userDetailsTable;

}

the error i am getting when going from 2nd to 3rd is 从第二到第三我得到的错误是

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.artyazilim.art.UserDetailsTable.getAdres()' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String com.artyazilim.art.UserDetailsTable.getAdres()'

10-30 05:33:13.410 24881-24881/com.artyazilim.art E/AndroidRuntime: 10-30 05:33:13.410 24881-24881 / com.artyazilim.art E / Android运行时:
at com.artyazilim.art.UserDetailsActivity$AsyncUserDetails.onPostExecute(UserDetailsActivity.java:74) 在com.artyazilim.art.UserDetailsActivity $ AsyncUserDetails.onPostExecute(UserDetailsActivity.java:74)

10-30 05:33:13.410 24881-24881/com.artyazilim.art E/AndroidRuntime: 10-30 05:33:13.410 24881-24881 / com.artyazilim.art E / Android运行时:
at com.artyazilim.art.UserDetailsActivity$AsyncUserDetails.onPostExecute(UserDetailsActivity.java:47) 10 在com.artyazilim.art.UserDetailsActivity $ AsyncUserDetails.onPostExecute(UserDetailsActivity.java:47)10

i know it seems like a duplicate and I know the rules search before ask,I have spent lots of time trying other's solutions but the reason i might didn't find the answer else where is because i don't know whats is actually causing this error so not knowing what to search for. 我知道这似乎是重复的,我知道在询问之前进行规则搜索,我花了很多时间尝试其他解决方案,但是我可能找不到其他答案的原因是在哪里,因为我不知道实际上是什么原因引起的错误,因此不知道要搜索什么。

thanks in advance :) 提前致谢 :)

In you second activity check if result2.getAboneKod(); 在您的第二个活动中,检查result2.getAboneKod(); is not returning a null object. 没有返回null对象。

I think this is why when you open the 3rd activity from the 2nd, you have the NullPointerException . 我认为这就是为什么当您从第二个打开第三个活动时,会出现NullPointerException

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 android 中将数据从第一个选项卡传递到第三个选项卡活动时获取 null object 引用 - Getting null object reference when passing data from 1st tab to 3rd tab activity in android JComboBox仅在从第1到第2时才更改组件(而不是从第3到第2时) - JComboBox changes components only when going from 1st to 2nd (Not from 3rd to 2nd) 如何通过从第1类调用第2类的方法来调用第3类的方法 - How to call a method of a 3rd class by calling the method of the 2nd class from the 1st class 以这样的方式更新 HashMap,使第二个值变成第一个,第三个变成第二个,依此类推 - Updating a HashMap in such a way that the 2nd value becomes 1st and 3rd becomes 2nd and so on 如何将文本视图数据从第一活动发送到第三活动 - How to send Text View data from 1st activity to 3rd activity Java 8 LocalDate:确定每月的星期几(第1,第2,第3等) - Java 8 LocalDate: Determine What Monday of the Month its (1st, 2nd, 3rd, etc) 当第一个 API 和第二个 API 在测试环境中工作时,如何模拟第三个 API 响应 - How to mock 3rd API response when 1st API and 2nd API is working on test environment 由于某种原因,数据没有从第三个活动发送到主活动,并且应用程序崩溃,而第二个活动工作得很好 - For some reason data is not sent from 3rd Activity to main Activity and the app crashes while the 2nd Activity works perfectly fine 如何解决获取#2nd记录而不是#1st记录的光标(#3rd而不是#2nd等)? - How to fix cursor that is taking #2nd record instead of #1st (#3rd instead of #2nd etc.)? 如何获得有关按钮单击第一活动的信息并在第三活动中使用它的信息? -安卓 - How to get information about a button click on 1st activity and use it in the 3rd activity? - android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM