当我尝试实例化一个类时,我正在使用NPE,我已经尝试过使用NameOftheClass.this,getApplicationContext(),getApplication()。这些都不起作用,这是日志:
java.lang.NullPointerException
at br.com.FragmentClientes.<init>(FragmentClientes.java:87)
第87行是:mRepositorio = new Repositorio(FragmentClientes.this);
这是片段:
public class FragmentClientes extends ActionBarActivity {
private boolean searchCheck;
private List<ClienteModel> clientes = new ArrayList<ClienteModel>();
private ProgressBar progressBar;
private ListView lv;
private ClientViewAdapter ad;
private ClientViewAdapter ads;
private SearchView searchView;
private LinearLayout footerLinearLayout;
private boolean shouldExecuteOnResume;
private Repositorio mRepositorio;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
setContentView(R.layout.fragment_cliente);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
SecurePreferences mSessao = new SecurePreferences(FragmentClientes.this, "sessao");
mSessao.put("menuAtual", "Clientes");
lv = (ListView) findViewById(R.id.listaClientes);
}
public FragmentClientes(Integer idViagem) {
clientes = new ArrayList<ClienteModel>();
try {
mRepositorio = new Repositorio(FragmentClientes.this);
List lista = mRepositorio.getClientesViagem(idViagem);
clientes = lista;
ad = new ClientViewAdapter(FragmentClientes.this, this, clientes);
lv.setVerticalFadingEdgeEnabled(true);
lv.setVerticalScrollBarEnabled(true);
lv.setAdapter(ad);
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:2)
不要创建Parameteriged Activity
构造函数
只需在Activity
中初始化您的数据即可回复onCreate()
,onResume()
使用Activity
Intent
更多阅读活动文档Activity Documnetation
答案 1 :(得分:2)
不要在其构造函数中使用活动上下文,它现在可以工作。请将构造函数中的所有代码放入一个方法中,然后在`onCreate方法中调用它:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
setContentView(R.layout.fragment_cliente);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
SecurePreferences mSessao = new SecurePreferences(FragmentClientes.this, "sessao");
mSessao.put("menuAtual", "Clientes");
Integer idViagem = getIntent().getIntExtra(TAG, -1);
init(idViagem);
}
public void init(Integer mId) {
clientes = new ArrayList<ClienteModel>();
try {
mRepositorio = new Repositorio(FragmentClientes.this);
List lista = mRepositorio.getClientesViagem(mId);
clientes = lista;
ad = new ClientViewAdapter(FragmentClientes.this, this, clientes);
lv.setVerticalFadingEdgeEnabled(true);
lv.setVerticalScrollBarEnabled(true);
lv.setAdapter(ad);
} catch (Exception e) {
e.printStackTrace();
}
}
此外,您还需要找到传递整数参数的另一种方法。
为此,对启动活动的intent使用Intent.putExtra(TAG, idViagem)
,然后使用getIntent().getIntExtra(TAG, -1)
例如,如果您从另一个活动启动FragmentClientes活动:
Intent intent = new Intent(this, FragmentClientes.class);
intent.putExtra("idViagem", int_value_of_id_that_you_passed_through_constructor);
startActivity(intent);
答案 2 :(得分:1)
FragmentClientes是一个活动。您不应该定义自定义counstructor,因为方法onCreate
表现为构造函数,并且将由系统自动调用。
所以请将代码放入onCreate
方法。
重要提示:你永远不应该,我的意思是永远不要打电话 new FragmentClientes(1);
答案 3 :(得分:0)
可能是因为您没有正确实例化ListView
private ListView lv;
您永远不会(在您提供的代码中)对象的任何实例化。