英文:
Unable to launch another activity from fragment in android studio
问题
我有一个片段,在片段中我试图在按钮点击时调用一个新的活动。
btnLoadLimit.setOnClickListener(v -> {
Intent intent = new Intent(getActivity(), DataActivity.class);
startActivity(intent);
});
数据活动
public class DataActivity extends AppCompatActivity {
Context mContext;
@BindView(R.id.smart_msn_spinner)
Spinner msnSpinner;
ArrayList<String> msnArrayList = new ArrayList<>(Arrays.asList("Select MSN", "002998002010"));
ArrayAdapter<String> msnAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
msnAdapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_dropdown_item, msnArrayList);
msnSpinner.setAdapter(msnAdapter);
msnSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedMeterNo = msnArrayList.get(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
当我点击 btnLoadLimit 按钮时,我的应用程序会关闭,并显示以下错误:
无法启动组件{com.thumbsol.accuratemobileassetsmanagament/com.thumbsol.accuratemobileassetsmanagament.fragment.DataActivity}:java.lang.NullPointerException: 尝试调用空对象引用的 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' 的虚拟方法
at com.thumbsol.accuratemobileassetsmanagament.fragment.DataActivity.onCreate(DataActivity.java:122)
第 122 行是 msnAdapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_dropdown_item, msnArrayList);
我在片段中应用了相同的方法,它可以正常工作,但在我的新活动中却不行。
注意: 如果没有调用任何方法,则该活动会显示。
英文:
I have a fragment in which I am trying to call a new activity on a button click.
btnLoadLimit.setOnClickListener(v -> {
Intent intent = new Intent(getActivity(), DataActivity.class);
startActivity(intent);
});
Data Activity
public class DataActivity extends AppCompatActivity {
Context mContext;
@BindView(R.id.smart_msn_spinner)
Spinner msnSpinner;
ArrayList<String> msnArrayList = new ArrayList<>(Arrays.asList("Select MSN","002998002010" )); //"002999002020"
ArrayAdapter<String> msnAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
msnAdapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_dropdown_item, msnArrayList);
msnSpinner.setAdapter(msnAdapter);
msnSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedMeterNo = msnArrayList.get(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
When I click on btnLoadLimit button my app is closing with the following error
> Unable to start activity ComponentInfo{com.thumbsol.accuratemobileassetsmanagament/com.thumbsol.accuratemobileassetsmanagament.fragment.DataActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
>
> at com.thumbsol.accuratemobileassetsmanagament.fragment.DataActivity.onCreate(DataActivity.java:122)
The line 122 is msnAdapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_dropdown_item, msnArrayList);
The same method I have applied in my fragment and it's working but in my new activity it's not.
Note: The activity is displaying if none of the methods are invoked.
答案1
得分: 1
看起来你没有初始化你的 mContext。这意味着你正在将 null 传递给你的 ArrayAdapter()。
无论如何,你不应该存储你的 context,因为你的 context 就是你的 activity。
你可以直接使用 this:
msnAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, msnArrayList);
英文:
Looks like you don't initialize your mContext. meaning you're passing null to your ArrayAdapter().
You shouldn't be storing your context anyway, since your context is your activity..
You can just use this:
msnAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, msnArrayList);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论