无法为我的Android应用程序使用putExtra()和getExtras()

时间:2015-03-22 11:43:26

标签: java android android-intent

我想将电影的名称从MainActivity(ListView)发送到SongActivity。这就是我尝试过的。

MainActivity.java

public class MainActivity extends Activity{
  Activity context;
   HttpPost httppost;
   StringBuffer buffer;
   HttpResponse response;
   HttpClient httpclient;
   ProgressDialog pd;
   CustomAdapter adapter;
   ListView listProduct;
   ArrayList<String> records;
protected void onCreate(Bundle savedInstanceState) {
   //TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   context=this;
   records=new ArrayList<String>();
   listProduct=(ListView)findViewById(R.id.product_list);
   adapter=new CustomAdapter(context, R.layout.list_item,R.id.pro_name, records);
   listProduct.setAdapter(adapter);
   listProduct.setOnItemClickListener(new OnItemClickListener(){
       @Override
       public void onItemClick(AdapterView<?> arg0, View v, int position, long id){

           String sText = ((TextView) v).getText().toString();
           Intent songIntent = new Intent(getApplicationContext(), SongActivity.class);
           songIntent.putExtra("movie_name", sText );
           startActivity(songIntent);

       }

   });
   }

这是我的SongActivity.java

package com.example.telugump3;

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

public class SongActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.song_activity);
        TextView tt = (TextView)findViewById(R.id.testing);
        Intent iin= getIntent();
        Bundle b = iin.getExtras();

        if(b!=null)
        {
            String j =(String) b.get("movie_name");
            tt.setText(j);
        }

    }

logcat的

03-22 17:17:20.949: E/IMGSRV(22747): :0: PVRDRMOpen: TP3, ret = 46
03-22 17:17:20.959: E/IMGSRV(22747): :0: PVRDRMOpen: TP3, ret = 50
03-22 17:17:20.959: E/IMGSRV(22747): :0: PVRDRMOpen: TP3, ret = 51
03-22 17:17:20.959: E/IMGSRV(22747): :0: PVRDRMOpen: TP3, ret = 51
03-22 17:17:20.959: E/IMGSRV(22747): :0: PVRDRMOpen: TP3, ret = 51
03-22 17:17:20.969: E/IMGSRV(22747): :0: PVRDRMOpen: TP3, ret = 53
03-22 17:17:20.999: D/OpenGLRenderer(22747): Enabling debug mode 0
03-22 17:17:23.149: I/dalvikvm(22747): Total arena pages for JIT: 11
03-22 17:17:23.149: I/dalvikvm(22747): Total arena pages for JIT: 12
03-22 17:17:23.149: I/dalvikvm(22747): Total arena pages for JIT: 13
03-22 17:17:23.149: I/dalvikvm(22747): Total arena pages for JIT: 14
03-22 17:17:23.149: I/dalvikvm(22747): Total arena pages for JIT: 15
03-22 17:17:23.149: I/dalvikvm(22747): Total arena pages for JIT: 16
03-22 17:17:23.159: I/dalvikvm(22747): Total arena pages for JIT: 17
03-22 17:17:23.159: I/dalvikvm(22747): Total arena pages for JIT: 18
03-22 17:17:23.159: I/dalvikvm(22747): Total arena pages for JIT: 19
03-22 17:17:23.159: I/dalvikvm(22747): Total arena pages for JIT: 20
03-22 17:17:25.289: D/AndroidRuntime(22747): Shutting down VM
03-22 17:17:25.289: W/dalvikvm(22747): threadid=1: thread exiting with uncaught exception (group=0x430ef140)
03-22 17:17:25.299: E/AndroidRuntime(22747): FATAL EXCEPTION: main
03-22 17:17:25.299: E/AndroidRuntime(22747): Process: com.example.telugump3, PID: 22747
03-22 17:17:25.299: E/AndroidRuntime(22747): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
03-22 17:17:25.299: E/AndroidRuntime(22747):    at com.example.telugump3.MainActivity$1.onItemClick(MainActivity.java:51)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at android.widget.AbsListView.performItemClick(AbsListView.java:1158)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2949)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at android.widget.AbsListView$3.run(AbsListView.java:3683)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at android.os.Handler.handleCallback(Handler.java:733)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at android.os.Handler.dispatchMessage(Handler.java:95)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at android.os.Looper.loop(Looper.java:149)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at android.app.ActivityThread.main(ActivityThread.java:5257)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at java.lang.reflect.Method.invokeNative(Native Method)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at java.lang.reflect.Method.invoke(Method.java:515)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at dalvik.system.NativeStart.main(Native Method)
03-22 17:17:27.259: I/Process(22747): Sending signal. PID: 22747 SIG: 9

我尝试删除putExtra()并完美启动SongActivity。但是如果我使用putExtra(),当我点击ListView中的任何项目时应用程序崩溃。我只想要点击项目的文本,以便我可以在其他活动中使用它。

6 个答案:

答案 0 :(得分:0)

尝试:String j =(String) b.getString("movie_name");

Source

答案 1 :(得分:0)

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView

您正在向RelativeLayout投射TextView。如果有这样的演员或您使用错误的findViewById()

,请检查id来电

答案 2 :(得分:0)

发送时执行此操作

 intent.putExtra("message", message);

并在收到

  Intent intent = getIntent();
  String message = intent.getStringExtra("message");

答案 3 :(得分:0)

onItemClick的参数v是listview项的根布局。这不是你的TextView。所以你必须修改如下。

//String sText = ((TextView) v).getText().toString();    
String sText = ((TextView) v.findViewById(R.id.[your TextView id])).getText().toString();

答案 4 :(得分:0)

Ops,你没有put或get Extra的问题,请注意错误 您的问题是单击的目标是布局,布局无法转换为TextView

答案 5 :(得分:0)

  

java.lang.ClassCastException:android.widget。 RelativeLayout无法强制转换为 android.widget。 TextView

     

at com.example.telugump3。 MainActivity $ 1.onItemClick (MainActivity.java:51)

正如您所看到的,您的问题与其他内容无关,但您知道在哪里查看,那么让我们来看看如何在MainActivity

中声明您的项目适配器
adapter = new CustomAdapter(context, R.layout.list_item,R.id.pro_name, records);

我将假设参数如下:context,layout id,text view id,data set。因此,项目布局中TextView的ID为R.id.pro_name

现在让我们看看在OnItemClickListener

中会发生什么
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id){
    // arg0 is the ListView
    // v is the whole item layout

    String sText = ((TextView) v).getText().toString();
    // you assumed that the whole layout was a TextView

    // ...
}

现在你可以很好地了解问题所在。您有两种选择:

选项1)快速而肮脏。

String sText = ((TextView) v.findViewById(R.id.pro_name).getText().toString();

为什么这么糟糕?你必须在两个地方写R.id.pro_name。您正在将应用的演示层和模型层混合在一起 - 您不是在询问数据集,而是在询问已经依赖于数据集的人。

选项2)要走的路。

String sText = adapter.getItem(position).toString();

您要求数据与列表视图使用的同一个适配器。