无法从 Android Studio 中的片段启动另一个活动

huangapple go评论76阅读模式
英文:

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 -&gt; {
        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&lt;String&gt; msnArrayList = new ArrayList&lt;&gt;(Arrays.asList(&quot;Select MSN&quot;,&quot;002998002010&quot; )); //&quot;002999002020&quot;

ArrayAdapter&lt;String&gt; msnAdapter;

 @Override
 protected void onCreate(Bundle savedInstanceState) {


  msnAdapter = new ArrayAdapter&lt;&gt;(mContext, android.R.layout.simple_spinner_dropdown_item, msnArrayList);
    msnSpinner.setAdapter(msnAdapter);
    msnSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
            selectedMeterNo = msnArrayList.get(position);
        }

        @Override
        public void onNothingSelected(AdapterView&lt;?&gt; 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&lt;&gt;(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&lt;&gt;(this, android.R.layout.simple_spinner_dropdown_item, msnArrayList);

huangapple
  • 本文由 发表于 2020年8月15日 21:35:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63426616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定