我想通過編程來製作微調器,編碼後應用程序開始崩潰。我試圖做的是,當選擇微調選項時,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
也許你的語境爲空? – kai
你是否已經在你的strings.xml文件中正確定義了你的數組? – GrIsHu
使用String []而不是那個ArrayList,它可能有幫助 –