【问题标题】:setText issue at android activityandroid活动中的setText问题
【发布时间】:2014-04-17 20:16:52
【问题描述】:

我在某个活动中遇到问题。

我有一个由 JSON 填充的列表视图。如果用户点击一行,则会显示详细视图活动。在此详细视图活动中,有四行存在问题,我已注释 (//) 以避免异常。这里有详细视图活动的代码:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

public class Empresas_SingleItemView extends Activity {
    // Declare Variables
    String valoracionEmpresa;
    String nombreEmpresa;
    String direccionEmpresa;
    String imagenstrImagen;
    String position;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from singleitemview.xml
        ImageLoader imageLoader = new ImageLoader(this);
        setContentView(R.layout.empresas_singleitemview);


        Intent i = getIntent();
        // Get the result of rank
        valoracionEmpresa = i.getStringExtra("valoracionEmpresa");


        // Get the result of country
        nombreEmpresa = i.getStringExtra("nombreEmpresa");


        // Get the result of population
        direccionEmpresa = i.getStringExtra("direccionEmpresa");


        // Get the result of flag
        imagenstrImagen = i.getStringExtra("strImagen");


        // Locate the TextViews in singleitemview.xml
        TextView txtvaloracionempresa = (TextView) findViewById(R.id.valoracionEmpresa);
        TextView txtnombreempresa = (TextView) findViewById(R.id.nombreEmpresa);
        TextView txtdireccionempresa = (TextView) findViewById(R.id.direccionEmpresa);

        // Locate the ImageView in singleitemview.xml
        ImageView imagenEmpresa = (ImageView) findViewById(R.id.strImagen);

  //  *** this are the four lines that throw exception if not commented, each of them.

            // Set results to the TextViews
 (1)        //txtvaloracionempresa.setText(valoracionEmpresa);
 (2)        //txtnombreempresa.setText(nombreEmpresa);
 (3)        //txtdireccionempresa.setText(direccionEmpresa);

            // Capture position and set results to the ImageView
            // Passes flag images URL into ImageLoader.class
  (4)       //imageLoader.DisplayImage(imagenstrImagen, imagenEmpresa);
    }
}

我需要您的帮助来找出问题所在。谢谢

已编辑

这是布局 empresas_singleintemview:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!--  ListRow Left sied Thumbnail image -->
    <LinearLayout android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:layout_alignParentLeft="true"
        android:background="@drawable/image_bg"
        android:layout_marginRight="5dip">

        <ImageView
            android:id="@+id/strImagen"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:src="@drawable/rihanna"/>

    </LinearLayout>

    <!-- Title Of Song-->
    <TextView
        android:id="@+id/valoracionEmpresa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Rihanna Love the way lie"
        android:textColor="#040404"
        android:typeface="sans"
        android:textSize="15dip"
        android:textStyle="bold"/>

    <!-- Artist Name -->
    <TextView
        android:id="@+id/direccionEmpresa"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/nombreEmpresa"
        android:textColor="#343434"
        android:textSize="10dip"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Just gona stand there and ..." />

    <!-- Rightend Duration -->
    <TextView
        android:id="@+id/valoracionEmpresa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/nombreEmpresa"
        android:gravity="right"
        android:text="5:45"
        android:layout_marginRight="5dip"
        android:textSize="10dip"
        android:textColor="#10bcc9"
        android:textStyle="bold"/>

     <!-- Rightend Arrow -->
     <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>

</RelativeLayout>

已编辑

列表适配器的代码:

导入 java.util.ArrayList; 导入 java.util.HashMap;

导入 android.content.Context; 导入android.content.Intent; 导入android.util.Log; 导入 android.view.LayoutInflater; 导入android.view.View; 导入 android.view.View.OnClickListener; 导入android.view.ViewGroup; 导入 android.widget.BaseAdapter; 导入android.widget.ImageView; 导入android.widget.TextView;

public class Empresas_ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public Empresas_ListViewAdapter(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView valoracionEmpresa;
        TextView nombreEmpresa;
        TextView direccionEmpresa;
        ImageView strImagen;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.empresas_listview_item, parent, false);
        // Get the position
        resultp = data.get(position);

        // Locate the TextViews in listview_item.xml
        valoracionEmpresa = (TextView) itemView.findViewById(R.id.valoracionEmpresa);
        nombreEmpresa = (TextView) itemView.findViewById(R.id.nombreEmpresa);
        direccionEmpresa = (TextView) itemView.findViewById(R.id.direccionEmpresa);

        // Locate the ImageView in listview_item.xml
        strImagen = (ImageView) itemView.findViewById(R.id.strImagen);

        // Capture position and set results to the TextViews
        valoracionEmpresa.setText(resultp.get(Empresas_MainActivity.VALORACIONEMPRESA));
        nombreEmpresa.setText(resultp.get(Empresas_MainActivity.NOMBREEMPRESA));
        direccionEmpresa.setText(resultp.get(Empresas_MainActivity.DIRECCIONEMPRESA));
        // Capture position and set results to the ImageView
        // Passes flag images URL into ImageLoader.class
        imageLoader.DisplayImage(resultp.get(Empresas_MainActivity.STRIMAGEN), strImagen);
        // Capture ListView item click
        itemView.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View arg0) {
                // Get the position
                resultp = data.get(position);
                Intent intent = new Intent(context, Empresas_SingleItemView.class);
                Log.v("MVASCO valoracion Empresa =", "HOLA NOBMRE "+resultp.get(Empresas_MainActivity.NOMBREEMPRESA));
                Log.v("MVASCO valoracion Empresa =", "HOLA VALORACION"+resultp.get(Empresas_MainActivity.VALORACIONEMPRESA));

                // Pass all data rank
                intent.putExtra("valoracionEmpresa", resultp.get(Empresas_MainActivity.VALORACIONEMPRESA));

                // Pass all data country
                intent.putExtra("nombreEmpresa", resultp.get(Empresas_MainActivity.NOMBREEMPRESA));
                // Pass all data population
                intent.putExtra("direccionEmpresa",resultp.get(Empresas_MainActivity.DIRECCIONEMPRESA));
                // Pass all data flag
                intent.putExtra("strImagen", resultp.get(Empresas_MainActivity.STRIMAGEN));
                // Start SingleItemView Class
                context.startActivity(intent);


            }
        });
        return itemView;
    }
}

【问题讨论】:

  • 我会在你的字符串集上运行一个带有断点的调试,并通过 id 来查找你的资源,要么你的字符串为空,要么视图本身为空。我们可以看看你的布局文件吗?
  • 另外,您能否将存储变量的代码添加到您的Intent
  • 您能否发布 empresas_singleitemview.xml 以及您将字符串添加到 Intent 对象的代码?
  • 它必须从你的意图包中获取你的字符串,你的视图看起来不错
  • 从捆绑包中获取它们后,是否在 Empresas_SingleItemView 活动中运行日志以确保它们已设置?尝试,只是为了测试,做 ""your view name".setText("Random string"); 看看它是否仍然给你一个问题如果是这样,它与你的视图有关,如果不是它与你的字符串有关。

标签: android


【解决方案1】:

我认为您的错误在于您的 id 以供您查看。你有

<!-- Title Of Song-->
<TextView
    android:id="@+id/valoracionEmpresa"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="Rihanna Love the way lie"
    android:textColor="#040404"
    android:typeface="sans"
    android:textSize="15dip"
    android:textStyle="bold"/>

然后

 <TextView
    android:id="@+id/valoracionEmpresa"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/nombreEmpresa"
    android:gravity="right"
    android:text="5:45"
    android:layout_marginRight="5dip"
    android:textSize="10dip"
    android:textColor="#10bcc9"
    android:textStyle="bold"/>

具有相同的 ID。你也有

TextView txtnombreempresa = (TextView) findViewById(R.id.nombreEmpresa);

没有 ID 为 nombreEmpresa 的实际 TextView。要么删除重复的视图,要么给它一个新的 id(可能是 nombreEmpresa)。

抱歉没有早点发现错误

【讨论】:

  • 也就是说,用两天时间解决这个问题,非常简单。谢谢你
  • 别担心,这是一个容易犯的错误。值得注意的是,Android 会认为它的资源文件中的任何 id 都是有效的,所以仅仅因为它说 R.id."name" 有效并不意味着该视图当前实际上是可访问的。
【解决方案2】:

罪魁祸首似乎是这样的:

<TextView
    android:id="@+id/valoracionEmpresa"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="Rihanna Love the way lie"
    android:textColor="#040404"
    android:typeface="sans"
    android:textSize="15dip"
    android:textStyle="bold"/>

<!-- Artist Name -->
<TextView
    android:id="@+id/direccionEmpresa"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/nombreEmpresa"
    android:textColor="#343434"
    android:textSize="10dip"
    android:layout_marginTop="1dip"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="Just gona stand there and ..." />

<!-- Rightend Duration -->
<TextView
    android:id="@+id/valoracionEmpresa"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/nombreEmpresa"
    android:gravity="right"
    android:text="5:45"
    android:layout_marginRight="5dip"
    android:textSize="10dip"
    android:textColor="#10bcc9"
    android:textStyle="bold"/>

您已将same id 分配给两个TextView

【讨论】:

  • 答案和zgc7009一样,但他是第一个,非常感谢您的时间和精力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-28
  • 1970-01-01
  • 2016-06-18
  • 1970-01-01
相关资源
最近更新 更多