2013-11-28 224 views
0

我想通過編程來製作微調器,編碼後應用程序開始崩潰。我試圖做的是,當選擇微調選項時,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 
+0

也許你的語境爲空? – kai

+0

你是否已經在你的strings.xml文件中正確定義了你的數組? – GrIsHu

+0

使用String []而不是那個ArrayList,它可能有幫助 –

回答

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

setPromptId()需要一個R.string資源,不R.array。這導致

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

(在logcat中的NullPointerException異常是從以前版本的代碼通過logcat的時間戳就是證明。我編輯的問題,以去除不相關的老例外。)

+0

是這樣:String [] str = new String [6]; \t \t str [1] =「總資產」; \t \t str [2] =「淨收入」; \t \t str [3] =「銷售成本」; \t \t str [4] =「貢獻邊際」; \t \t str [5] =「價格差異」; \t \t str [6] =「Quantity Variation」; Spinner spin = new Spinner(this); \t \t spin.setPrompt(str +「」); \t \t layout.addView(spin); \t \t spin.setLayoutParams(params); –

+0

@ user3026643不,提示只是微調對話框標題。微調器選項來自適配器。 – laalto

+0

[http://s27.postimg.org/bp1txpnoj/Capture.png]這是組件顯示的方式。你能幫我修好嗎? –

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); 
+0

沒有解決問題。你能告訴我如何解決重疊的組件。就像我在運行程序之後在main.xml和.java文件中的其他組件相互重疊一樣。 –