简体   繁体   中英

Android: Creating using LayoutInflater

I have a problem with creating things using LayoutInflater:

package com.tip.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements View.OnClickListener {
    EditText billamount, percent, people;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        billamount = (EditText)findViewById(R.id.billtext);
        percent = (EditText)findViewById(R.id.percenttext);
        people = (EditText)findViewById(R.id.numberpeople);
        Button unevensplitbutton = (Button)findViewById(R.id.unevensplit);
        Button calculates = (Button)findViewById(R.id.calculate);
        unevensplitbutton.setOnClickListener(this);
        calculates.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch(v.getId()){
            case R.id.calculate:
                TableLayout calculatenumberinflated = (TableLayout)findViewById(R.id.numbertable2);
                View view = getLayoutInflater().inflate(R.layout.calculatenumbertable,calculatenumberinflated,false);
                calculatenumberinflated.addView(view);
                break;
            case R.id.unevensplit:
                break;
        }
    }


    @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, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }




}

And the XML of calculatenumbertable:

<?xml version="1.0" encoding="utf-8"?>

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="25dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/numbertable2">
<TableRow
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Each Person Pays"
        android:id="@+id/numberpeople"
        android:layout_column="3" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/eachpersonedit"
        android:layout_column="6" />
</TableRow></TableLayout>

And the XML of fragment_main:

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="25dp">

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Bill amount"
                android:id="@+id/bill"
                android:layout_column="3" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"
                android:ems="10"
                android:id="@+id/billtext"
                android:layout_column="6" />
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Percentage %"
                android:id="@+id/percentage"
                android:layout_column="3" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"
                android:ems="10"
                android:id="@+id/percenttext"
                android:layout_column="6" />
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Number of people"
                android:id="@+id/textView"
                android:layout_column="3" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:ems="10"
                android:id="@+id/numberpeople"
                android:layout_column="6" />
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Split Unevenly"
                android:id="@+id/unevensplit"
                android:layout_column="3" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Calculate"
                android:id="@+id/calculate"
                android:layout_column="6" />
        </TableRow>
    </TableLayout>
</ScrollView>

It returns an error where:

12-16 21:22:56.825  28459-28459/com.tip.calculator E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.tip.calculator.MainActivity.onClick(MainActivity.java:35)

Where line 35 is the calculatenumberinflated.addView(view); line of code.

I feel lost right now, can someone please clarify what I am doing wrong? Thanks.

Try this

LayoutInflater inflater=getLayoutInflater();
View view= inflater.inflate(R.layout.calculatenumbertable, null);
calculatenumberinflated.addView(view);

只需将android:id =“ @ + id / numbertable2”从calculatenumbertable.xml移动到tableLayouts中的fragment_main.xml。

You are getting Nullpointer error at this line TableLayout calculatenumberinflated = (TableLayout)findViewById(R.id.numbertable2); which is not at all available in your fragment_main layout. It in the layout calculatenumbertable file and as you are inflating fragment_main layout so its not getting the correct id of your TableLayout .

Try to add your layout in TableRow besides TableLayout . Create separate row in your calculatenumbertable layout and then try add the layout in that row. You can not add layout directly in TableLayout .

Try as below: In your calculatenumbertable add row in last as below:

 <TableRow
        android:id="@+id/tablerow"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

  </TableRow>

In your onClick add the layout as below:

public void onClick(View v) {
        switch(v.getId()){
            case R.id.calculate:
                TableRowcalculatenumberinflated = (TableRow)findViewById(R.id.tablerow);
                View view = getLayoutInflater().inflate(R.layout.calculatenumbertable,calculatenumberinflated,false);
                calculatenumberinflated.addView(view);
                break;
            case R.id.unevensplit:
                break;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM