微调器导致应用程序崩溃

时间:2013-11-28 12:53:27

标签: android eclipse android-layout

我正在尝试以编程方式创建一个微调器,编码后应用程序开始崩溃。我想要做的是当选择微调器的选项时,UI应该根据它改变。 (Android开发的新手)

Java代码:

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

2 个答案:

答案 0 :(得分:0)

spin.setPromptId(R.array.formulas);

setPromptId()需要R.string资源,而不是R.array。这导致了

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

(logcat中的NullPointerException来自您的代码的先前版本,由logcat时间戳记证明。我编辑了该问题以删除不相关的旧例外。)

答案 1 :(得分:0)

spin.setPromptId(R.array.formulas);行中,该方法获取整数值,您正在定义R.array

Sets the prompt to display when the dialog is shown.因此您应该像R.String.appname那样设置资源。

试试这样。

 spin.setPromptId(R.string.app_name);