2017-07-16 101 views
0

我是新应用程序开发人员,迄今为止我的应用程序按预期工作,但仅在Android Studio的设备上启动时才适用。例如,我有一个实例变量,我在onCreate()方法中给出1的值。当我从android studio启动应用程序到我的设备上时,它工作正常,变量的值为1.但是,当我从我的设备启动它而不使用android studio时,变量的值为0.我有还发现我会在我知道应该有一个值的变量上得到一堆NullPointerExceptions,并且再次从Android Studio启动时会起作用,但在从设备启动时不起作用。Android Studio应用程序在我从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); 
} 

这是我该走的列表视图中的项目创建的类此活动开始

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; 
} 
+0

您是否使用Instant Run? – ifiok

+0

向我们展示一些代码,没有它我们不能帮你 – Konrad

+0

是的,我正在使用即时运行。 –

回答

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()可能会返回一个非空Bundle对象,但包可能没有你要访问的钥匙,其中如果您的实例变量的值为int,则会将其设置为空或零。

您可以通过简单地检查包中是否存在某个特定的密钥并仅在设置变量的情况下解决此问题。

bun.containsKey()

或者,如果他们是从包加载它们后空可以初始化变量。

+0

感谢您的回应,这似乎已经解决了问题,并且现在应用程序既可以在通过android studio运行时也可以在我的设备上运行。这真的很有帮助。 –

0

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

+0

我曾试过,但我没有工作。我会继续尝试,直到它可以工作,或者我找到另一个解决方案。谢谢回复。 –

相关问题