gpt4 book ai didi

java - 按下后退按钮时 Android View.getContext() 出现空对象错误

转载 作者:行者123 更新时间:2023-12-02 04:25:52 25 4
gpt4 key购买 nike

在我的主要 Activity 中,我有一个微调器,当用户在微调器上进行选择时,它会发出 Intent 并启动 Activity2,但如果用户尝试返回主要 Activity ,应用程序就会崩溃。

主类:

public class MainActivity extends ActionBarActivity {

private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addItemsSpinner();
}

public void addItemsSpinner(){
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
for(int i = 1; i<=6; i++) {
if (position == i ) {
Intent intent = new Intent(view.getContext().getApplicationContext(), Activity2.class);
view.getContext().startActivity(intent);
intent.putExtra("sel", spinner.getSelectedItem().toString());
startActivity(intent);
}
}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

Activity 2:

public class Activity2 extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity2);
Intent getSel = getIntent();
String selection = getSel.getExtras().getString("sel");
final TextView textViewToChange = (TextView) findViewById(R.id.user);
textViewToChange.setText(
"user " + selection);

setDateAndTime();
}

public void setDateAndTime(){
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
final TextView textViewToChange = (TextView) findViewById(R.id.time);
textViewToChange.setText(
"time " + currentDateTimeString);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity2, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
finish();
super.onBackPressed();
}

日志猫:

08-24 14:43:17.713    3063-3063/dbxprts.moldtech E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: dbxprts.moldtech, PID: 3063
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.View.getContext()' on a null object reference
at dbxprts.moldtech.MainActivity$1.onItemSelected(MainActivity.java:30)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:897)
at android.widget.AdapterView.selectionChanged(AdapterView.java:884)
at android.widget.AdapterView.checkSelectionChanged(AdapterView.java:1047)
at android.widget.AdapterView.handleDataChanged(AdapterView.java:1027)
at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:184)
at android.widget.Spinner.onMeasure(Spinner.java:507)
at android.view.View.measure(View.java:17430)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:697)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
at android.view.View.measure(View.java:17430)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

最佳答案

更改此:

    Intent intent = new Intent(view.getContext().getApplicationContext(), Activity2.class);
view.getContext().startActivity(intent);
intent.putExtra("sel", spinner.getSelectedItem().toString());
startActivity(intent);

Intent intent = new Intent(MainActivity.this, Activity2.class);
intent.putExtra("sel", spinner.getSelectedItem().toString());
startActivity(intent);

关于java - 按下后退按钮时 Android View.getContext() 出现空对象错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32190500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com