Android 应用程序停止

Android application stops

当应用程序加载此 activity 时它停止 working.Below 是我的主要 activity、logcat 和所需数组的代码。请帮我解决这个问题。在此,当用户从微调器中选择任何项目时,然后根据所选项目另一个列表项加载结果。

主要活动:

Button cropPrice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addListenerOnButton();
    }

    public void addListenerOnButton(){

        final Context context = this;

        cropPrice = (Button)findViewById(R.id.button1);
        cropPrice.setOnClickListener(new OnClickListener(){

            public void onClick(View arg0){

                Intent intent = new Intent(context, CropPrice.class);
                startActivity(intent);
            }
        });
    }

CropPrice.java

public static String itemsa = "guj";

// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.crop_price);

    Spinner state = (Spinner) findViewById(R.id.spinner1);

    String[] courses={"guj","con","raj"};
    ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,courses);
    state.setAdapter(adapter1); 
    state.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> av, View v,
                int position, long itemId) {
            // TODO Auto-generated method stub
            itemsa = av.getItemAtPosition(position).toString();
         int getRes2 = getResources().getIdentifier(itemsa, "array", getPackageName());
         final String products[] = getResources().getStringArray(getRes2);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }});

    // Listview Data
  //  Intent intent = new Intent();
    // ((EditText)(findViewById(R.id.inputSearch))).setText(intent.getStringExtra("selected"));
    //int id = Context.getResources().getIdentifier(itemsa, "array", 
          //  context.getPackageName());

    //String products[] = context.getResources().getStringArray(id);
    //final String products[] = getResources().getStringArray(R.array.guj);
    //int resourceId = R.array.class.getField(Integer.parseInt(itemsa));
    //final String products[] = getResources().getStringArray(resourceId);
    //int holderint = getResources().getIdentifier(itemsa, "Array",
        //       this.getPackageName());

     //  String[] products = getResources().getStringArray(holderint);
    //String itemName = itemsa;
    //String getRes = "R.array."+ itemName;
    //final String products[] = getResources().getStringArray("getRes");
    lv = (ListView) findViewById(R.id.listView1);
    // Adding items to listview
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
    lv.setAdapter(adapter);
    lv.setVisibility(View.GONE);

    inputSearch = (EditText) findViewById(R.id.inputSearch);
    inputSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            if(count == 0)
            {
                tl.setVisibility(View.GONE);
            }
            submitButton.setVisibility(View.GONE);
            CropPrice.this.adapter.getFilter().filter(cs);
            if(cs.length()== 0){
                lv.setVisibility(View.GONE);
            }else{
            lv.setVisibility(View.VISIBLE);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    inputSearch.setText((String) lv.getAdapter().getItem(position));
                    cont = inputSearch.getText().toString();
                    lv.setVisibility(View.GONE);
                    submitButton.setVisibility(View.VISIBLE);
                    tl.setVisibility(View.VISIBLE);
                    } });
            }
        }
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub
        }
        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });
}

Strings.xml

<string-array name="raj">
    <item >raja</item>
    <item >rajb</item>        
</string-array>
<string-array name="Countreis">
    <item >manish</item>
    <item >India</item>          
</string-array>
<string-array name="guj">
    <item >guja</item>
    <item >gujb</item>         
</string-array>

日志猫:

01-03 06:11:20.013: E/AndroidRuntime(2883): FATAL EXCEPTION: main
01-03 06:11:20.013: E/AndroidRuntime(2883): Process: com.finaldbparse.finaldbparse, PID: 2883
01-03 06:11:20.013: E/AndroidRuntime(2883): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.finaldbparse.finaldbparse/com.finaldbparse.finaldbparse.CropPrice}: java.lang.NullPointerException: storage == null
01-03 06:11:20.013: E/AndroidRuntime(2883):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
01-03 06:11:20.013: E/AndroidRuntime(2883):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
01-03 06:11:20.013: E/AndroidRuntime(2883):     at android.app.ActivityThread.access0(ActivityThread.java:144)
01-03 06:11:20.013: E/AndroidRuntime(2883):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
01-03 06:11:20.013: E/AndroidRuntime(2883):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-03 06:11:20.013: E/AndroidRuntime(2883):     at android.os.Looper.loop(Looper.java:135)
01-03 06:11:20.013: E/AndroidRuntime(2883):     at android.app.ActivityThread.main(ActivityThread.java:5221)
01-03 06:11:20.013: E/AndroidRuntime(2883):     at java.lang.reflect.Method.invoke(Native Method)
01-03 06:11:20.013: E/AndroidRuntime(2883):     at java.lang.reflect.Method.invoke(Method.java:372)
01-03 06:11:20.013: E/AndroidRuntime(2883):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-03 06:11:20.013: E/AndroidRuntime(2883):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-03 06:11:20.013: E/AndroidRuntime(2883): Caused by: java.lang.NullPointerException: storage == null
01-03 06:11:20.013: E/AndroidRuntime(2883):     at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
01-03 06:11:20.013: E/AndroidRuntime(2883):     at java.util.Arrays.asList(Arrays.java:155)
01-03 06:11:20.013: E/AndroidRuntime(2883):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:141)
01-03 06:11:20.013: E/AndroidRuntime(2883):     at com.finaldbparse.finaldbparse.CropPrice.onCreate(CropPrice.java:128)
01-03 06:11:20.013: E/AndroidRuntime(2883):     at android.app.Activity.performCreate(Activity.java:5933)
01-03 06:11:20.013: E/AndroidRuntime(2883):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
01-03 06:11:20.013: E/AndroidRuntime(2883):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
01-03 06:11:20.013: E/AndroidRuntime(2883):     ... 10 more

java.lang.NullPointerException: name is null

因为 getResources().getIdentifier 在调用 onCreate 方法时执行,但您在 Spinner 的 onItemSelected 上的 itemsa 中设置值。

因此您需要在调用 getResources().getIdentifier 或使用一些默认值初始化 itemsa 之前进行空检查:

您最初没有初始化 itemsa 变量。 像这样更改行: 默认初始化一些值。

public static String itemsa="guj";

也更改这些行:

String[] courses={"guj","con","raj"};

String[] courses={"raj","Countreis","guj"};

编辑:

public static String itemsa = "guj";

    // List view
    private ListView lv;
    // Listview Adapter
    ArrayAdapter<String> adapter;
    // Search EditText
    EditText inputSearch;
    // ArrayList for Listview
    ArrayList<HashMap<String, String>> productList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.crop_price);

        Spinner state = (Spinner) findViewById(R.id.spinner1);

        String[] courses={"guj","Countreis","raj"};
        ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,courses);
        state.setAdapter(adapter1); 
        state.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> av, View v,
                    int position, long itemId) {
                // TODO Auto-generated method stub
                itemsa = av.getItemAtPosition(position).toString();
             int getRes2 = getResources().getIdentifier(itemsa, "array", getPackageName());
             final String products[] = getResources().getStringArray(getRes2);
             updateList(products);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }});

        int getRes2 = getResources().getIdentifier(itemsa, "array", getPackageName());
        final String products[] = getResources().getStringArray(getRes2);

        updateList(products);
    }

    private void updateList(String[] products) {
        lv = (ListView) findViewById(R.id.listView1);
        // Adding items to listview
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
        lv.setAdapter(adapter);
        lv.setVisibility(View.GONE);

        inputSearch = (EditText) findViewById(R.id.inputSearch);
        inputSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                if(count == 0)
                {
                    tl.setVisibility(View.GONE);
                }
                submitButton.setVisibility(View.GONE);
                CropPrice.this.adapter.getFilter().filter(cs);
                if(cs.length()== 0){
                    lv.setVisibility(View.GONE);
                }else{
                lv.setVisibility(View.VISIBLE);
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        inputSearch.setText((String) lv.getAdapter().getItem(position));
                        cont = inputSearch.getText().toString();
                        lv.setVisibility(View.GONE);
                        submitButton.setVisibility(View.VISIBLE);
                        tl.setVisibility(View.VISIBLE);
                        } });
                }
            }
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub
            }
            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
            }
        });     
    }

使用 try-catch 块来监视这样的错误,

try{
Spinner state = (Spinner) findViewById(R.id.spinner1);

String[] courses={"guj","con","raj"};
ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,courses);
state.setAdapter(adapter1); 
state.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> av, View v,
            int position, long itemId) {
        // TODO Auto-generated method stub
        itemsa = av.getItemAtPosition(position).toString();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // TODO Auto-generated method stub

    }});

  // Listview Data
  //  Intent intent = new Intent();
// ((EditText)(findViewById(R.id.inputSearch))).setText(intent.getStringExtra("selected"));
//int id = Context.getResources().getIdentifier(itemsa, "array", 
      //  context.getPackageName());

//String products[] = context.getResources().getStringArray(id);
//final String products[] = getResources().getStringArray(R.array.guj);
int getRes2 = getResources().getIdentifier(itemsa, "array", getPackageName());
final String products[] = getResources().getStringArray(getRes2);
//int resourceId = R.array.class.getField(Integer.parseInt(itemsa));
//final String products[] = getResources().getStringArray(resourceId);
//int holderint = getResources().getIdentifier(itemsa, "Array",
    //       this.getPackageName());

 //  String[] products = getResources().getStringArray(holderint);
//String itemName = itemsa;
//String getRes = "R.array."+ itemName;
//final String products[] = getResources().getStringArray("getRes");
lv = (ListView) findViewById(R.id.listView1);
// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
lv.setAdapter(adapter);
lv.setVisibility(View.GONE);

inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        // When user changed the Text
        if(count == 0)
        {
            tl.setVisibility(View.GONE);
        }
        submitButton.setVisibility(View.GONE);
        CropPrice.this.adapter.getFilter().filter(cs);
        if(cs.length()== 0){
            lv.setVisibility(View.GONE);
        }else{
        lv.setVisibility(View.VISIBLE);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                inputSearch.setText((String) lv.getAdapter().getItem(position));
                cont = inputSearch.getText().toString();
                lv.setVisibility(View.GONE);
                submitButton.setVisibility(View.VISIBLE);
                tl.setVisibility(View.VISIBLE);
                } });
        }
    }
    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub
    }
    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub
    }
});
}catch(Exception e){

//display e in toast
Toast.makeText(context, ""+e, duration).show();
}