将值动态放置到布局中的文本视图中,并将该布局放在另一个布局中。

时间:2015-07-14 15:20:25

标签: android android-layout

我尝试了一切,只是获得了一个JAVA:NULL指针EXCEPTION在我尝试将一个布局放入另一个布局的行。

这是MainActivity.java:

import java.util.ArrayList;
import java.util.Set;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
 private    SharedPreferences sp;
private  TableLayout enterstockTableLayout;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ArrayList<ItemData> list= new ArrayList<ItemData>();
    list.add(new ItemData("Indian Rupee",R.drawable.india));
    list.add(new ItemData("Us Dollar",R.drawable.usa_dollar));



    Spinner sp1=(Spinner) findViewById(R.id.spinner1);
    SpinnerAdapter adapter=newSpinnerAdapter(this,R.layout.spinnerlayout,R.id.txt,list);         
    sp1.setAdapter(adapter);

    Spinner sp2=(Spinner) findViewById(R.id.spinner2);
    sp2.setAdapter(adapter);
        sp= getSharedPreferences("CurrList",MODE_PRIVATE);

        insertIntoSp(sp);           

     updateCurrList(sp);
}

public void updateCurrList(SharedPreferences spd)
    {

String[] curr=spd.getAll().entrySet().toArray(new String[0]);
for(int i=0;i<curr.length;i++)
{
    String yqlquerryFirst="";
    String yqlquerrySecond="";
    String yqlFinalquery=yqlquerryFirst+curr[i]+yqlquerrySecond;
    insertIntoLayout(curr[i],i);   
}
}

private void insertIntoLayout(String string, int i)
{
    LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View newRow=inflater.inflate(R.layout.currencylistview,null);


    TextView newRowTextView=(TextView) newRow.findViewById(R.id.currtextView);
    TextView newRowRateView=(TextView) newRow.findViewById(R.id.changetextView);
    TextView newRowotherView=(TextView) newRow.findViewById(R.id.valuetextView);
    newRowTextView.setText(string);

    enterstockTableLayout.addView(newRow,i);
}

public void insertIntoSp(SharedPreferences spd)
{

    Editor edt=spd.edit();


edt.putString("Eur/Usd","EURUSD");
edt.putString("Usd/Jpy","USDJPY");
edt.putString("Gbp/Usd","GBPUSD");
edt.putString("Usd/Chf","USDCHF");
edt.putString("Usd/Cad","USDCAD");
edt.putString("Eur/Jpy","EURJPY");
edt.putString("Aud/Usd","AUDUSD");
edt.commit();

}

@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;
    }

}

这里是activity_main.xml

    <RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#126180"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

   <TableRow
    android:id="@+id/tableRowhead"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <TextView
        android:id="@+id/curenheadtextview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/CurrencyCon"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff" />

</TableRow>

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/tableRowhead"
    android:layout_below="@+id/tableRowhead"
    android:background="@drawable/splash" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="700dp"
        android:layout_marginTop="14dp"
        android:background="@android:color/transparent" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="29dp" >

            <EditText
                android:id="@+id/editText1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="7"
                android:inputType="numberDecimal"
                android:textColor="#ffffff" />

            <Spinner
                android:id="@+id/spinner1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#ffffff"
                android:entries="@array/countrylist1" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/tableRow1"
            android:layout_marginTop="38dp" >

            <EditText
                android:id="@+id/editText2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="7"

                android:inputType="numberDecimal"
                android:textColor="#ffffff" />

            <Spinner
                android:id="@+id/spinner2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#ffffff"
                android:entries="@array/countrylist1" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/tableRow2"
            android:layout_marginTop="11dp"
            android:background="#126180" >

            <TextView
                android:id="@+id/currtextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:text="@string/currennciesLabel"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#ffffff" />

        </TableRow>

        <TableLayout
            android:id="@+id/enterstockTableLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/tableRow3"
            android:padding="5dp"
            android:stretchColumns="yes" >

        </TableLayout>

    </RelativeLayout>
</ScrollView>

</RelativeLayout>

,这是currencylistview.xml

    <?xml version="1.0" encoding="utf-8"?>
   <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/currencylistviewLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

   <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/currtextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/valuetextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp" />

    <TextView
        android:id="@+id/changetextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#d3d3d3"
        android:padding="5dp" />

</TableRow>

<TextView
    android:id="@+id/desctextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

   </TableLayout>

我希望动态地将值插入currencylistview.xml,并将此表格布局在currencylistview.xml中插入activity_main.xml。

1 个答案:

答案 0 :(得分:1)

您的变量entertockTableLayout未初始化。在setContentView:

之后,您应该在onCreate中执行以下操作
enterstockTableLayout = (TableLayout)findViewById(R.id.enterstockTableLayout);