0

I am trying to make a spinner programmatically and after coding it the app started crashing. What i am trying to make is when an option from the spinner is selected the UI should change according to it. (newbie to android development)

Java code:

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class Main_Page extends Activity {
int pos;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main__page);

    ArrayList<String> array = new ArrayList<String>();
    array.add("Total Assets");
    array.add("Net Income");
    array.add("Cost of Goods Sold");
    array.add("Contribution Margin");
    array.add("Price Variance");
    array.add("Quantity Variance");

    LayoutParams params= new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);

    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);

    Spinner spin = new Spinner(this);
    spin.setPromptId(R.array.formulas);
    layout.addView(spin);
    spin.setLayoutParams(params);

    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, array);
    spin.setAdapter(spinnerArrayAdapter);
    spin.setOnItemSelectedListener(new OnItemSelectedListener() 
    {
        public void onItemSelected(AdapterView<?> nothing, View v, int position, long something)
        {
            pos = position;
        }

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

        }
    });



    if(pos==0)
    {

        TextView tv = new TextView(this);
        tv.setText("Liabilities:");
        tv.setLayoutParams(params);

        EditText et = new EditText(this);
        et.setHint("Enter liabilities value here");
        et.setLayoutParams(params);

        TextView tv1 = new TextView(this);
        tv1.setText("Owner's Equity:");
        tv1.setLayoutParams(params);

        EditText et1 = new EditText(this);
        et1.setHint("Enter Owner's Equity value here");
        et1.setLayoutParams(params);

        Button btn = new Button(this);
        btn.setText("Find");
        btn.setLayoutParams(params);



        layout.addView(tv);
        layout.addView(et);
        layout.addView(tv1);
        layout.addView(et1);
        layout.addView(btn);



    }
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    this.addContentView(layout, layoutParams);
}

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

}

Logcat:

11-28 15:46:16.659: E/AndroidRuntime(652): FATAL EXCEPTION: main
11-28 15:46:16.659: E/AndroidRuntime(652): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.accountingsoftware/com.example.accountingsoftware.Main_Page}: android.content.res.Resources$NotFoundException: String resource ID #0x7f060000
11-28 15:46:16.659: E/AndroidRuntime(652):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-28 15:46:16.659: E/AndroidRuntime(652):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-28 15:46:16.659: E/AndroidRuntime(652):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-28 15:46:16.659: E/AndroidRuntime(652):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-28 15:46:16.659: E/AndroidRuntime(652):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 15:46:16.659: E/AndroidRuntime(652):  at android.os.Looper.loop(Looper.java:123)
11-28 15:46:16.659: E/AndroidRuntime(652):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-28 15:46:16.659: E/AndroidRuntime(652):  at java.lang.reflect.Method.invokeNative(Native Method)
11-28 15:46:16.659: E/AndroidRuntime(652):  at java.lang.reflect.Method.invoke(Method.java:507)
11-28 15:46:16.659: E/AndroidRuntime(652):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-28 15:46:16.659: E/AndroidRuntime(652):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-28 15:46:16.659: E/AndroidRuntime(652):  at dalvik.system.NativeStart.main(Native Method)
11-28 15:46:16.659: E/AndroidRuntime(652): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f060000
11-28 15:46:16.659: E/AndroidRuntime(652):  at android.content.res.Resources.getText(Resources.java:201)
11-28 15:46:16.659: E/AndroidRuntime(652):  at android.content.Context.getText(Context.java:173)
11-28 15:46:16.659: E/AndroidRuntime(652):  at android.widget.Spinner.setPromptId(Spinner.java:285)
11-28 15:46:16.659: E/AndroidRuntime(652):  at com.example.accountingsoftware.Main_Page.onCreate(Main_Page.java:40)
11-28 15:46:16.659: E/AndroidRuntime(652):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-28 15:46:16.659: E/AndroidRuntime(652):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-28 15:46:16.659: E/AndroidRuntime(652):  ... 11 more
laalto
  • 150,114
  • 66
  • 286
  • 303

2 Answers2

0
spin.setPromptId(R.array.formulas);

setPromptId() expects a R.string resource, not R.array. This leads to the

android.content.res.Resources$NotFoundException: String resource ID #0x7f060000

(The NullPointerException in the logcat was from a previous version of your code as evidenced by logcat timestamps. I edited the question to remove irrelevant old exceptions.)

laalto
  • 150,114
  • 66
  • 286
  • 303
  • Is it this way: String [] str = new String [6]; str[1] ="Total Assets"; str[2]="Net Income"; str[3]="Cost of Goods Sold"; str[4]="Contribution Margin"; str[5]="Price Variance"; str[6]="Quantity Variance";Spinner spin = new Spinner(this); spin.setPrompt(str+""); layout.addView(spin); spin.setLayoutParams(params); – Noha Philip Nov 28 '13 at 13:28
  • @user3026643 No, the prompt is just the spinner dialog title. Spinner options come from the adapter. – laalto Nov 28 '13 at 13:30
  • [http://s27.postimg.org/bp1txpnoj/Capture.png] this is the way the componets get displayed. Can u help me fix it? – Noha Philip Nov 28 '13 at 13:56
  • @NohaPhilip That seems to be an issue with the layout. It's a separate issue from this spinner crash question. You can post another question for it. – laalto Nov 28 '13 at 14:01
0

In your spin.setPromptId(R.array.formulas); line the method gets the integer value and you are defining the R.array

Whereas it Sets the prompt to display when the dialog is shown. so you should set the resource like R.String.appname.

Try out like this.

 spin.setPromptId(R.string.app_name);
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • didnt solve the problem. Can u tell me how to solve overlapping of components. Like if i make a spinner in main.xml and the rest of the components in .java file after running the program the components overlap each other. – Noha Philip Nov 28 '13 at 13:41