0

这里我创建了四个xml文件main.xml, test1.xml, test2.xml, test3.xml.

可以访问它们,spinner但我收到了异常。我也给logcat

请帮我。

String[] lan = { "test1", "test2", "test3" };

Spinner spn = (Spinner) findViewById(R.id.spinner1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, lan);

spn.setAdapter(adapter);

spn.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
        if (pos == 0) {
            findViewById(R.layout.test1).setVisibility(View.VISIBLE);
        } else if (pos == 1) {
            findViewById(R.layout.test2).setVisibility(View.VISIBLE);
        } else if (pos == 2) {
            findViewById(R.layout.test3).setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    }
});

日志猫:

07-11 18:09:39.480: E/AndroidRuntime(21200): FATAL EXCEPTION: main
07-11 18:09:39.480: E/AndroidRuntime(21200): java.lang.NullPointerException
07-11 18:09:39.480: E/AndroidRuntime(21200):    at com.venky.innverlayoutchangevisibility.MainActivity$1.onItemSelected(MainActivity.java:30)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at android.widget.AdapterView.access$200(AdapterView.java:42)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at android.os.Handler.handleCallback(Handler.java:587)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at android.os.Looper.loop(Looper.java:130)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at android.app.ActivityThread.main(ActivityThread.java:3691)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at java.lang.reflect.Method.invokeNative(Native Method)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at java.lang.reflect.Method.invoke(Method.java:507)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
07-11 18:09:39.480: E/AndroidRuntime(21200):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

在此处输入代码您不能使用来自不同布局的组件,例如。你需要做的是定义一个LayoutInflator并从中工作。R.id.yourComponent只能访问(大部分)在 onCreate 方法中定义的组件,例如:

setContentView(R.layout.main);

做你需要的,为你的每一个布局创建一个充气器(注意:为了让这个工作,给你的 test1.xml、test2.xml、test3.xml 中的每个主要 LinearLayouts 一个 ID 并访问它,而不是文件本身):

String[] lan = { "test1", "test2", "test3" };

Spinner spn = (Spinner) findViewById(R.id.spinner1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_dropdown_item, lan);

//Duplicate this code for every .xml layout inside your onItemSelected
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View firstView = inflater.inflate(R.layout.test1, null); //inflates test1.xml
LinearLayout test1Layout = (LinearLayout) firstView.findViewById(R.id.idOfTheLayout); //This finds a component inside your test1.xml file. As mentioned, give the highest Layout which holds all your components an ID.
test1Layout.setVisibility(View.VISIBLE);


spn.setAdapter(adapter);

spn.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int pos,
            long id) {
        if (pos == 0) {
            findViewById(R.layout.test1).setVisibility(View.VISIBLE);
        } else if (pos == 1) {
            findViewById(R.layout.test2).setVisibility(View.VISIBLE);
        } else if (pos == 2) {
            findViewById(R.layout.test3).setVisibility(View.VISIBLE);
        }
    }
于 2013-07-11T15:16:41.403 回答
0

我几乎可以肯定你不能这样做。根据文档

从在 onCreate(Bundle) 中处理的 XML 中查找由 id 属性标识的视图。

这用于查找View当前膨胀的内部layout,我理解这些是单独的文件。您将需要使用inflater. 或者在这种情况下,如果您在选择项目后setContentView()不需要使用任何电流,请致电Views

 @Override
    public void onItemSelected(AdapterView<?> parent, View v, int pos,
            long id) {
        int layout;   // create int for layout to inflate
        if (pos == 0) {
            layout = R.layout.test1;   // assign int according to the layout needed
        } else if (pos == 1) {
            layout = R.layout.test2;
        } else if (pos == 2) {
           layout = R.layout.test3;
        }
        setContentView(layout);      // inflate the layout
    }

请注意,这样做会将您中View的 s设置为默认值,并且原始膨胀中的 s将不存在。我不确定这是否是您正在寻找的东西。如果没有,请告诉我。layoutinflateViewlayoutonCreate()

编辑

现在我知道这些在你layoutsincludemainlayout中,你不想使用

    findViewById(R.layout.test3)

当您使用<include>标签将它们插入主目录时,请layout给他们一个id并使用id

     findViewById(R.id.test3)
于 2013-07-11T13:24:46.727 回答