我有一個微調的兩個按鈕在我的Android應用程序,的Java NullPointerException異常Android上的微調
微調從服務器端腳本dynamicaly填充。
它上次工作,但我不知道爲什麼現在我有一個java.nullPointerException問題,我不知道如何解決它,來自LogCat的日誌表示,我在我的空微調
第一,這是我在FormPilihLokasiKelompok.class
package com.nigmagrid.jm.demo;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class FormPilihLokasiKelompok extends Activity {
ArrayList<String> content;
ArrayAdapter<String> content_adapter;
Button btn_save, btn_back;
Context context;
String selected_value, spinner_prompt, toast_failed, url_retrieval;
Spinner spinner;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form_pilih_lokasi_kelompok);
btn_save = (Button) findViewById(R.id.btn_save);
btn_back = (Button) findViewById(R.id.btn_back);
spinner = (Spinner) findViewById(R.id.spinner);
context = FormPilihLokasiKelompok.this;
selected_value = "";
spinner_prompt = "Pilih Lokasi";
toast_failed = "Tidak ada lokasi yang terpilih";
url_retrieval = VarsUrl.getServerAddress()+"get_list.php?table=kelompok_lokasi";
content = new ArrayList<String>();
btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
finish();
}
});
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(selected_value.length() > 1){
Intent intent = new Intent(getApplicationContext(), FormPilihLokasiKm.class);
intent.putExtra("content", selected_value);
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(), toast_failed, Toast.LENGTH_SHORT).show();
}
}
});
spinner.setPrompt(spinner_prompt);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String sel_val = spinner.getSelectedItem().toString();
if(sel_val != null || sel_val.length() > 0){
selected_value = spinner.getSelectedItem().toString();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// do nothing
}
});
new PopulateSpinner().execute(url_retrieval);
}
protected class PopulateSpinner extends AsyncTask<String, Void, String>{
ProgressDialog pd = new ProgressDialog(context);
protected void onPreExecute(){
pd.setMessage("Mengambil data...");
}
@Override
protected String doInBackground(String... arg0) {
try{
String response = CustomHttpClient.excecuteHttpGet(arg0[0]);
String res = response.toString().trim();
return res;
}catch (Exception e){
return e.toString();
}
}
protected void onPostExecute(String res){
String[] datanya = res.split("#", 0);
int jumlah_data = datanya.length;
if(datanya[0].equals("1")){
try{
content.clear();
for(int i = 1; i < jumlah_data; i++){
content.add(datanya[i]);
}
content_adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, content);
content_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(content_adapter);
}catch(Exception e){
Toast.makeText(getApplicationContext(), "Terjadi kesalahan sistem\n"+e.toString(), Toast.LENGTH_SHORT).show();
}
}else{
content.add("Tidak ada data untuk ditampilkan");
}
content_adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, content);
content_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(content_adapter);
pd.dismiss();
}
}
}
完整的腳本這裏的日誌
11-23 12:49:57.179: W/dalvikvm(22610): threadid=1: thread exiting with uncaught exception (group=0x40015578)
11-23 12:49:57.187: E/AndroidRuntime(22610): FATAL EXCEPTION: main
11-23 12:49:57.187: E/AndroidRuntime(22610): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nigmagrid.jm.demo/com.nigmagrid.jm.demo.FormPilihLokasiKelompok}: java.lang.NullPointerException
11-23 12:49:57.187: E/AndroidRuntime(22610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
11-23 12:49:57.187: E/AndroidRuntime(22610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-23 12:49:57.187: E/AndroidRuntime(22610): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-23 12:49:57.187: E/AndroidRuntime(22610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-23 12:49:57.187: E/AndroidRuntime(22610): at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 12:49:57.187: E/AndroidRuntime(22610): at android.os.Looper.loop(Looper.java:123)
11-23 12:49:57.187: E/AndroidRuntime(22610): at android.app.ActivityThread.main(ActivityThread.java:3687)
11-23 12:49:57.187: E/AndroidRuntime(22610): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 12:49:57.187: E/AndroidRuntime(22610): at java.lang.reflect.Method.invoke(Method.java:507)
11-23 12:49:57.187: E/AndroidRuntime(22610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
11-23 12:49:57.187: E/AndroidRuntime(22610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-23 12:49:57.187: E/AndroidRuntime(22610): at dalvik.system.NativeStart.main(Native Method)
11-23 12:49:57.187: E/AndroidRuntime(22610): Caused by: java.lang.NullPointerException
11-23 12:49:57.187: E/AndroidRuntime(22610): at com.nigmagrid.jm.demo.FormPilihLokasiKelompok.onCreate(FormPilihLokasiKelompok.java:68)
11-23 12:49:57.187: E/AndroidRuntime(22610): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-23 12:49:57.187: E/AndroidRuntime(22610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
11-23 12:49:57.187: E/AndroidRuntime(22610): ... 11 more
THI s腳本在第68行:spinner.setPrompt(spinner_prompt);
我已經將spinner_prompt更改爲普通字符串,如「test prompt」,但它仍然相同 所以它必須是微調器(cmiiw),我不知道該在哪裏修復它。
有人知道如何解決它? 感謝
更新 這裏是我的佈局
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:text="Lokasi"
android:padding="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinner"
android:padding="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_save"
android:text="Lanjutkan"
android:padding="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_back"
android:text="Kembali"
android:padding="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
似乎微調器爲空,不是提示符。我可以請你分享一下你的「form_pilih_lokasi_kelompok.xml」代碼嗎? 所以剛剛評論第68行,你仍然會在69上得到例外。 – Arslan
更新,檢查出 –