Android Studio应用程序在我从Android Studio启动时有效,但在我从设备启动时则无效

时间:2017-07-16 16:17:32

标签: java android nullpointerexception

我是应用程序开发的新手,到目前为止我的应用程序正在按预期工作,但只有当我从Android Studio在我的设备上启动它时。例如,我有一个实例变量,我在onCreate()方法中给出值1。当我从Android studio启动应用程序到我的设备时,它工作正常,变量的值为1.但是,当我从我的设备启动它而不使用android studio时,变量的值为0.我有还发现我会在变量上获得一堆NullPointerExceptions,我知道它应该有一个值,并且从Android Studio启动时再次起作用,但是从设备启动时不行。

这是MainActivity

public class MainActivity extends AppCompatActivity
{
private ArrayList<String> arrayList;
private ArrayList<ListItem> itemList;
private ArrayAdapter<String> adapter;
private EditText txtInput;
private int payRoll;
private String value;
private Intent mainToPayroll;
private int hours;
private int earnings;
private ArrayList<Integer> rollList;
private ArrayList<Integer> hourList;
private ArrayList<Integer> wageList;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rollList = new ArrayList<>(0);
    hourList = new ArrayList<>(0);
    wageList = new ArrayList<>(0);
    payRoll = 1;

    Bundle bun = getIntent().getExtras();
    if(bun != null)
    {
        rollList = bun.getIntegerArrayList("rolls");
        hourList = bun.getIntegerArrayList("hours");
        wageList = bun.getIntegerArrayList("wages");
        payRoll = bun.getInt("roll");
    }

    ListView listView = (ListView) findViewById(R.id.listv);
    String[] items = {};
    arrayList = new ArrayList<>(Arrays.asList(items));
    itemList = new ArrayList<>(0);
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.txtitem, arrayList);
    listView.setAdapter(adapter);
    Button btAdd = (Button) findViewById(R.id.btadd);
    mainToPayroll = new Intent(this, PayrollActivity.class);

    if(rollList != null)
    {
        for (int i = 0; i < rollList.size(); i++) {
            ListItem newItem = new ListItem(rollList.get(i), hourList.get(i), wageList.get(i));
            arrayList.add(newItem.toString());
            itemList.add(newItem);
            adapter.notifyDataSetChanged();
        }


        rollList.clear();
        hourList.clear();
        wageList.clear();
    }

    btAdd.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            ListItem newItem = new ListItem(payRoll, 0, 0);
            arrayList.add(newItem.toString());
            itemList.add(newItem);
            adapter.notifyDataSetChanged();
            payRoll++;
        }
    });

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            value = (String)adapter.getItem(position);
            ListItem item = itemList.get(position);
            Bundle info = new Bundle();
            info.putString("val", value);
            info.putInt("hours", item.getHours());
            info.putInt("wage", item.getWages());
            info.putInt("pos", position);

            if(itemList.size() > 0)
            {
                for (ListItem items : itemList)
                {
                    rollList.add(items.getPayroll());
                    hourList.add(items.getHours());
                    wageList.add(items.getWages());
                }
            }

            info.putIntegerArrayList("rolls", rollList);
            info.putIntegerArrayList("hours", hourList);
            info.putIntegerArrayList("wages", wageList);
            info.putInt("roll", payRoll);
            info.putBoolean("rest", restore);


            mainToPayroll.putExtras(info);

            startActivity(mainToPayroll);
        }
    });
}

只要点击列表视图中的项目

,就会启动此活动
public class PayrollActivity extends AppCompatActivity
{
private static TextView text;
private String payrollNumber;
private int payrollHrs;
private int payrollWages;
private int position;
private Intent payrollToMain;
private Button returnButton;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_payroll);

    final Bundle info = getIntent().getExtras();
    System.out.print(getIntent().getType());
    payrollNumber = info.getString("val");
    payrollHrs = info.getInt("hours");
    payrollWages = info.getInt("wage");
    position = info.getInt("pos");
    payrollToMain = new Intent(this, MainActivity.class);
    returnButton = (Button) findViewById(R.id.btnRtrn);

    returnButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Bundle thing = new Bundle();
            thing.putIntegerArrayList("rolls", info.getIntegerArrayList("rolls"));
            thing.putIntegerArrayList("hours", info.getIntegerArrayList("hours"));
            thing.putIntegerArrayList("wages", info.getIntegerArrayList("wages"));
            thing.putInt("roll", info.getInt("roll"));
            thing.putBoolean("rest", info.getBoolean("rest"));
            payrollToMain.putExtras(thing);
            startActivity(payrollToMain);

        }
    });

    text = (TextView) findViewById(R.id.title);

    text.setText(payrollNumber);
}

public static void setLabelText(String val)
{
    text.setText(val);
}

这是我为列表视图中的项目创建的类<​​/ p>

public class ListItem
{
private int payroll;
private int hrs;
private int wages;

public ListItem(int roll, int hours, int wag)
{
    payroll = roll;
    hrs = hours;
    wages = wag;
}

public int getPayroll()
{
    return payroll;
}

public int getHours()
{
    return hrs;
}

public int getWages()
{
    return wages;
}

public void setPayroll(int roll)
{
    payroll = roll;
}

public void setHrs(int hours)
{
    hrs = hours;
}

public void setWages(int wage)
{
    wages = wage;
}

public String toString()
{
    return "Payroll " + payroll + "\n" + hrs + " hours\n$" + wages;
}

2 个答案:

答案 0 :(得分:0)

尝试从设备中完全卸载应用,然后重试。这有时解决了这个问题。

答案 1 :(得分:0)

我认为你的问题是你的MainActivity中的这段代码:

Bundle bun = getIntent().getExtras();
if(bun != null)
{
    rollList = bun.getIntegerArrayList("rolls");
    hourList = bun.getIntegerArrayList("hours");
    wageList = bun.getIntegerArrayList("wages");
    payRoll = bun.getInt("roll");
}

getIntent().getExtras()可能会返回一个非null Bundle对象,但该bundle可能没有您尝试访问的键,在这种情况下,所有实例变量都将设置为null或0 int

你可以通过简单地检查捆绑中是否存在特定密钥并且仅设置变量来解决这个问题。

bun.containsKey()

或者,如果变量在从捆绑中加载后为null,则可以初始化变量。