我遇到了这个错误:当我按下一个按钮时,它会进入一个有阵列的活动。在添加阵列之前,它很好。添加数组后,应用程序崩溃。
public class navigation_front extends AppCompatActivity {
Button scan_btn;
Spinner dest;
EditText Edit_current;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(navigation_front);
scan_btn = (Button) findViewById(R.id.scan_btn);
final Activity activity = this;
scan_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
dest = (Spinner) findViewById(R.id.edit_destination);
String edit_destination = dest.getSelectedItem().toString().trim();
final ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.destination_array, R.layout.support_simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dest.setAdapter(adapter);
dest.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String type = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result.getContents() != null){
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
EditText editText =(EditText)findViewById(R.id.Edit_current);
editText.setText("" + result.getContents(), TextView.BufferType.EDITABLE);
}
}
以下是错误日志:
FATAL EXCEPTION: main
Process: com.sp.ez_mart, PID: 15263
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sp.ez_mart/com.sp.ez_mart.navigation_front}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2787)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.sp.ez_mart.navigation_front.onCreate(navigation_front.java:54)
at android.app.Activity.performCreate(Activity.java:6754)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2787)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
答案 0 :(得分:0)
可能
String type = parent.getItemAtPosition(position).toString();
由于parent.getItemAtPosition(position)
返回null
而导致问题。
使用Spinner
从position
获取所选项目,并将数据源传递给ArrayAdapter
构造函数:
@Override
public void onItemSelected(AdapterView<?> parent,View view,int position,long id) {
// get Array from xml
String [] dataArray = getResources().getStringArray(R.array.destination_array);
String type = dataArray[position];
}
答案 1 :(得分:0)
用@ρяσѕρєя解决了它添加了K行并删除了一些行
public class navigation_front extends AppCompatActivity {
Button scan_btn;
Spinner dest;
EditText Edit_current;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(navigation_front);
scan_btn = (Button) findViewById(R.id.scan_btn);
final Activity activity = this;
scan_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
dest = (Spinner) findViewById(R.id.edit_destination);
final ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.destination_array, R.layout.support_simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dest.setAdapter(adapter);
dest.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String [] dataArray = getResources().getStringArray(R.array.destination_array);
String type = dataArray[position];}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result.getContents() != null){
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
EditText editText =(EditText)findViewById(R.id.Edit_current);
editText.setText("" + result.getContents(), TextView.BufferType.EDITABLE);
}
}
}
}