【问题标题】:spinner causes app to crash微调器导致应用程序崩溃
【发布时间】:2013-11-28 21:32:13
【问题描述】:

我正在尝试以编程方式制作微调器,在对其进行编码后,应用程序开始崩溃。 What i am trying to make is when an option from the spinner is selected the UI should change according to it. (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

【问题讨论】:

  • 也许你的上下文是空的?
  • 您是否在您的 strings.xml 文件中正确定义了您的数组?
  • 使用 String[] 而不是那个 arraylist ,它可能会有所帮助
  • 你能告诉我们使用 logcat 出现错误的确切位置吗?
  • @user3026643 看看我的回答。

标签: android eclipse android-layout


【解决方案1】:
spin.setPromptId(R.array.formulas);

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

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

(logcat 中的 NullPointerException 来自先前版本的代码,logcat 时间戳证明了这一点。我编辑了问题以删除不相关的旧异常。)

【讨论】:

  • 是这样的吗:String [] str = new String [6]; str[1] ="总资产"; str[2]="净收入"; str[3]="商品销售成本"; str[4]="贡献保证金"; str[5]="价格差异"; str[6]="Quantity Variance";Spinner spin = new Spinner(this); spin.setPrompt(str+""); layout.addView(spin); spin.setLayoutParams(params);
  • @user3026643 不,提示只是微调器对话框的标题。微调器选项来自适配器。
  • [s27.postimg.org/bp1txpnoj/Capture.png] 这是组件的显示方式。你能帮我修一下吗?
  • @NohaPhilip 这似乎是布局的问题。这是与此微调器崩溃问题不同的问题。您可以为此发布另一个问题。
【解决方案2】:

在您的 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);

【讨论】:

  • 没有解决问题。你能告诉我如何解决组件重叠的问题吗?就像我在 main.xml 和 .java 文件中的其余组件在运行程序后创建一个微调器一样,这些组件相互重叠。
猜你喜欢
  • 2016-07-26
  • 1970-01-01
  • 2015-10-21
  • 2016-06-04
  • 2011-08-24
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
相关资源
最近更新 更多