0

so I'm trying to build and android app which takes in code from user input and puts it into a custom layout for a listview.

Here is the xml for the layout i have for each element of the list (the following is called game_row.xml)

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/dateout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/team1out"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/dateout"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/team2out"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/team1out"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/score1out"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/team2out"
        android:layout_alignParentRight="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/score2out"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/team2out"
        android:layout_alignParentRight="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <View
    android:layout_width="fill_parent"
    android:layout_below="@+id/team2out"
    android:layout_height="1dp"
    android:background="@android:color/white"/>

   </RelativeLayout>

And here's the java code for my class:

public class KeepScoreHomeScreen extends Activity {

    ArrayList<Game> Game_data = new ArrayList<Game>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_screen);

        if (savedInstanceState != null) {
            System.out.println("this shouldnt happen");
            ArrayList<Game> inp = (ArrayList<Game>) savedInstanceState.get("games");
            System.out.println("game made");
            Game_data.addAll(inp);

        }
        // System.out.println("helo");
        // Game_data.add(new Game(1,2,3,4,3, "t","T"));

        GameAdapter adapter = new GameAdapter(this, R.layout.game_row, Game_data);

        // This is used to update the maximum number of days in a month

        Bundle extras = getIntent().getExtras();

        ListView gameList = (ListView) findViewById(R.id.main_list_view);
        gameList.setAdapter(adapter);
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putParcelableArrayList("games", Game_data);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_home_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle item selection
        switch (item.getItemId()) {
        case R.id.action_newEntry:
            toNewEntry(this.findViewById(R.layout.home_screen));
            break;
        case R.id.action_help:
            buildHelp(this.findViewById(R.layout.home_screen));
            break;
        case R.id.action_settings:
            buildSettings(this.findViewById(R.layout.home_screen));
        default:
            return super.onOptionsItemSelected(item);
        }
        return false;
    }

    public void toNewEntry(View view) {
        Intent intent = new Intent(this, KeepScoreActivity.class);
        startActivityForResult(intent, 1);
    }

    protected void onActivityResult(int r, int j, Intent intent) {
        // System.out.println("hedslo");
        Bundle bundle = intent.getBundleExtra("myfirstintent");
        int sc1 = bundle.getInt("score1");
        int sc2 = bundle.getInt("score2");
        int ye = bundle.getInt("year");
        int mo = bundle.getInt("month");
        int da = bundle.getInt("day");
        String te1 = bundle.getString("team1");
        String te2 = bundle.getString("team2");
        Game g = new Game(sc1, sc2, ye, mo, da, te1, te2);
        Game_data.add(g);
        GameAdapter adapter = new GameAdapter(this, R.layout.game_row, Game_data);
        ListView gameList = (ListView) findViewById(R.id.main_list_view);
        gameList.setAdapter(adapter);
    }

    public void buildHelp(View view) {
        /***
         * this code to build an alert dialog was taken from
         * http://stackoverflow
         * .com/questions/2795300/how-to-implement-a-custom-alertdialog-view
         * 
         * I used this code because it allowed me to insert a simple xml file as
         * my alertdialog
         * 
         */

        LayoutInflater inflater = getLayoutInflater();
        View dialoglayout = inflater.inflate(R.layout.help_layout, (ViewGroup) getCurrentFocus());
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialoglayout);
        builder.show();
    }

    public void buildSettings(View view) {
        LayoutInflater inflater = getLayoutInflater();
        View dialoglayout = inflater.inflate(R.layout.settings_layout, (ViewGroup) getCurrentFocus());
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialoglayout);
        builder.show();

    }
}

The problem I have is that every time I manage to create the game_row outputs and then attempt to inflate the setting bar I get hte following error

05-10 14:27:56.153: E/AndroidRuntime(1569): FATAL EXCEPTION: main
05-10 14:27:56.153: E/AndroidRuntime(1569): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.widget.AdapterView.addView(AdapterView.java:477)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.view.LayoutInflater.inflate(LayoutInflater.java:497)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at edu.ucsb.cs.cs185.hw4skeleton.KeepScoreHomeScreen.buildSettings(KeepScoreHomeScreen.java:145)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at edu.ucsb.cs.cs185.hw4skeleton.KeepScoreHomeScreen.onOptionsItemSelected(KeepScoreHomeScreen.java:91)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.app.Activity.onMenuItemSelected(Activity.java:2534)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:958)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:514)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:99)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.view.View.performClick(View.java:4084)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.view.View$PerformClick.run(View.java:16966)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.os.Handler.handleCallback(Handler.java:615)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.os.Looper.loop(Looper.java:137)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.app.ActivityThread.main(ActivityThread.java:4745)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at java.lang.reflect.Method.invokeNative(Native Method)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at java.lang.reflect.Method.invoke(Method.java:511)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

can anyone please help me figure out how to get my views to play nicely with eachother?

4

1 回答 1

1

你使用LayoutInflater不当。在下面的代码块中......

View dialoglayout = inflater.inflate(R.layout.settings_layout, (ViewGroup) getCurrentFocus());

...充气机试图做两件事:

  1. 使用提供的 ViewGroup 扩展 XML 布局以确定根 LayoutParams
  2. 将膨胀的布局附加到提供的 ViewGroup

您不希望发生第二件事,因为您将视图传递给 AlertDialog,而不是将其附加到“当前焦点”(这显然是崩溃日志中的 ListView)。你也不希望第一件事发生,因为“当前焦点”不会是这个膨胀视图的父级,所以用它来计算 LayoutParams 也是没有意义的。

通常,您应该始终将要附加到的直接父视图作为 的第二个参数传递,inflate()如果当时不想附加两个参数,则使用第三个参数。但是,在此示例中,您无法访问实际的父视图(位于正在构建的 AlertDialog 内),并且LayoutParams无论如何它都会覆盖任何内容,因此您应该修改代码以在充气时不传递父视图:

View dialoglayout = inflater.inflate(R.layout.settings_layout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);

边注:

同样,这仅适用于这种特殊情况。在几乎所有其他情况下,您都应该inflate()像这样使用:

inflater.inflate(R.layout.settings_layout, parent, false);

您传入膨胀布局的真实父视图的位置。

于 2013-05-10T22:01:15.263 回答