开发者

TextView is not initializing

开发者 https://www.devze.com 2023-01-17 05:04 出处:网络
My layout xml has a Tablelayout with TextViews for a cell: <?xml version=\"1.0\" encoding=\"utf-8\"?>

My layout xml has a Tablelayout with TextViews for a cell:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">
    <TableRow>
        <TextView
            android:text="Style Details..."
            android:padding="3dip" />

    </TableRow>
    <TableRow>
        <TextView
            android:text="Style"
            android:padding="3dip" />
        <TextView
            android:id="@+id/StyleName"
            android:gravity="right"
            android:padding="3dip" />
    </TableRow>

    <TableRow>
        <TextView
            android:text="Price (Euro)"
            android:padding="3dip" />
        <TextView
            android:id="@+id/StylePrice"
            android:gravity="right"
            android:padding="3dip" />
    </TableRow>
    <TableRow>
        <TextView
            android:text="Price for Volume &lt;60&lt;90 pcs"
            android:padding="3dip" />
        <TextView
            android:id="@+id/PriceVolume6090"
            android:gravity="right"
            android:padding="3dip" />
    </TableRow>
    <TableRow>
        <TextView
            android:text="Price for Volume &开发者_JAVA百科;lt;100&lt;250pcs"
            android:padding="3dip" />
        <TextView
            android:id="@+id/PriceVoume100250"
            android:gravity="right"
            android:padding="3dip" />
    </TableRow>
    <TableRow>
        <TextView
            android:text="Price for Volume &lt;251&lt;500pcs"
            android:padding="3dip" />
        <TextView
            android:id="@+id/PriceVoume251500"
            android:gravity="right"
            android:padding="3dip" />
    </TableRow>
    <TableRow>
        <TextView
            android:text="Price for Volume &lt;501pcs"
            android:padding="3dip" />
        <TextView
            android:id="@+id/PriceVolume501"
            android:gravity="right"
            android:padding="3dip" />
    </TableRow>
</TableLayout>

I want to set the value for StyleName through my Activity. Following is the code that I am using:

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class StyleDetailsActivity extends Activity {
 private TextView tvStyleName;


 @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
                setContentView(R.layout.styledetailsview);
  String styleName = "Testing Style Name";
                tvStyleName = (TextView)findViewById(R.id.StyleName);
  tvStyleName.setText(styleName);


        }
}

The code is compiling fine and the app starts. On Running the app, I am getting a nullpointer exception. When I debug, I see that tvStyleName is null. Any suggestions as to why it would not be initializing


It's due to the different views with the same id(android:id in xml) . There might be present the text view with the same id in a different layout file.


Solved the problem.

Changed:

<TextView
            android:id="@+id/StyleName"
            android:gravity="right"
            android:padding="3dip" />

To:

<TextView
            android:id="@id/StyleName"
            android:gravity="right"
            android:padding="3dip" />

Added a Item in Strings.xml for StyleName

0

精彩评论

暂无评论...
验证码 换一张
取 消