【问题标题】:java.lang.ClassCastException: android.widget.RelativeLayoutjava.lang.ClassCastException: android.widget.RelativeLayout
【发布时间】:2013-11-26 07:12:38
【问题描述】:

我开发了一个简单的listview 应用程序,它显示listview,但是当我选择一个listview 时,我收到以下错误。

这是我的单个列表项 XML 文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<TextView android:id="@+id/product_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25dip"
        android:textStyle="bold"
        android:padding="10dip"
        android:textColor="#ffffff"/>   
</LinearLayout>

这是我的 MainActivity.java 文件

import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.Toast;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity implements FetchDataListener{
private ProgressDialog dialog;

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

    ListView lv = getListView();

    // listening to single list item on click
    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

          // selected item
          String product = ((TextView) view).getText().toString();

          // Launching new Activity on selecting single List Item
          Intent i = new Intent(getApplicationContext(), SingleListItem.class);
          // sending data to new activity
          i.putExtra("product", product);
          startActivity(i);

      }
    });

}

private void initView() {
    // show progress dialog
    dialog = ProgressDialog.show(this, "", "Loading...");

    String url = "http://pubbapp.comze.com/pubapp.php";
    FetchDataTask task = new FetchDataTask(this);
    task.execute(url);
}

@Override
public void onFetchComplete(List<Application> data) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // create new adapter
    ApplicationAdapter adapter = new ApplicationAdapter(this, data);
    // set the adapter to list
    setListAdapter(adapter);        
}

@Override
public void onFetchFailure(String msg) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // show failure message
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();        
}
}

这是我的第二个屏幕 java 文件,

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

public class SingleListItem extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.single_list_item_view);

    TextView txtProduct = (TextView) findViewById(R.id.product_label);

    Intent i = getIntent();
    // getting attached intent data
    String product = i.getStringExtra("product");
    // displaying selected product name
    txtProduct.setText(product);

}
}

我不知道如何解决这个错误。有人可以帮我解决这个问题吗?

【问题讨论】:

  • 你有什么ApplicationAdapter
  • 检查我的答案-------- 不需要在你的意图中传递 getApplicationContext() 。而不是这个你可以传递 MainActivity.this 在意图中。 Intent i = new Intent(MainActivity.this, SingleListItem.class);

标签: java android listview classcastexception


【解决方案1】:

如你所见:

java.lang.ClassCastException: android.widget.RelativeLayout

因为这里:

      String product = ((TextView) view).getText().toString();

您正在尝试将 ListView 选定的行视图(RelativeLayout)转换为 TextView。如果您想从选定的行布局中访问 TextView,请执行以下操作:

    TextView txtview = ((TextView) view.findViewById(R.id.your_textview_id));
    String product = txtview.getText().toString();

【讨论】:

  • 我更改了上面的代码,但后来又出现了另一个错误。
  • 这是我的错误。 11-26 15:48:14.860:E/AndroidRuntime(5570):致命异常:主要 11-26 15:48:14.860:E/AndroidRuntime(5570):java.lang.NullPointerException 11-26 15:48:14.860: E/AndroidRuntime(5570): 在 com.sj.jsondemo.MainActivity$1.onItemClick(MainActivity.java:35) 11-26 15:48:14.860: E/AndroidRuntime(5570): 在 android.widget.AdapterView.performItemClick( AdapterView.java:284) 11-26 15:48:14.860: E/AndroidRuntime(5570): 在 android.widget.ListView.performItemClick(ListView.java:3745)
  • @user2881604:请更新您有问题的代码并附上问题日志以获得更多帮助
【解决方案2】:

改变

// selected item
          String product = ((TextView) view).getText().toString();

TextView txtview= (TextView) view.findViewById(R.id.product_label);
String product = txtview.getText().toString();

【讨论】:

    【解决方案3】:

    右键单击项目并选择列表中的属性。转到 java 构建路径,将 Gen 文件夹移到上,将 src 文件夹移到下。清理项目并运行。如果它没有告诉我,这对我有用吗?

    【讨论】:

      【解决方案4】:

      如果您为 ListItem 使用自定义 xml 文件,那么您可以使用您在 xml 文件中编写的 textview id。用注释行替换这些行

       //   String product = ((TextView) view).getText().toString();
       TextView txtview= (TextView) view.findViewById(R.id.your_textview_id);
       String product = txtview.getText().toString();
      

      如果您在适配器中使用 android.R.layout.simple_list_item_1,那么您必须在代码中设置 android.R.id.text1 资源 ID。这种情况也一样

       //   String product = ((TextView) view).getText().toString();
       TextView txtview= (TextView) view.findViewById(android.R.id.text1);
       String product = txtview.getText().toString();
      

      例如。

       // listening to single list item on click
      lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
      
            // selected item------------->replace below line with this
             // String product = ((TextView) view).getText().toString();
              TextView txtview= (TextView) view.findViewById(R.id.your_textview_id);
              String product = txtview.getText().toString();
      
      
      
            // Launching new Activity on selecting single List Item
            Intent i = new Intent(MainActivity.this, SingleListItem.class);
            // sending data to new activity
            i.putExtra("product", product);
            startActivity(i);
      
        }
      });
      

      【讨论】:

      • 不需要在你的意图中传递 getApplicationContext() 。而不是这个你可以传递 MainActivity.this 在意图中。 Intent i = new Intent(MainActivity.this, SingleListItem.class);
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多