如何在Android中调试“期望的android.widget.TextView但找到了java.lang.string”?

时间:2015-10-15 04:07:05

标签: android android-studio

有人可以看看我的代码吗?我正在尝试为个人学校项目构建此应用程序。我是android studio的新手。我已经整理了一个简单的应用程序,您可以在其中显示停车场中的空间。

但是,当我点击listView屏幕中的一个地块时,我正在尝试打开第二个活动屏幕。我收到了这个错误,无法解决它。

**MainActivity.java**

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout         
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

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

<TextView
    android:id="@+id/lotSpaces"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lotName"/>

</RelativeLayout>

**SingleLotView.java**

package com.example.sahan.smartpark;

/**
* Created by Sahan on 2015-10-14.
*/
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SingleLotView extends Activity{

//declare variables
TextView lotName;
TextView lotSpaces;

String[] lot_name;
String[] lot_spaces;
int position;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.singlelotview);
    // Retrieve data from MainActivity on listview item click
    Intent i = getIntent();
    // Get the listview item click position
    position = i.getExtras().getInt("position");
    // Get the list of rank
   lotName = i.getStringArrayExtra("lotName");
    // Get the list of country
   lotSpaces = i.getStringArrayExtra("lotSpaces");
    // Get the list of population

    // Locate the TextViews in singleitemview.xml
    lotName = (TextView) findViewById(R.id.move_title);
    lotSpaces = (TextView) findViewById(R.id.move_title);

    // Load the text into the TextViews followed by the position
    lotName.setText(lot_name[position]);
    lotSpaces.setText(lot_spaces[position]);

}
}

**MoveAdapter.java**

package com.example.sahan.smartpark;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Sahan on 2015-10-12.
*/
public class MoveAdapter extends ArrayAdapter{
List list=new ArrayList();

public MoveAdapter(Context context, int resource) {
    super(context, resource);
}


static class DataHandler
{
    ImageView poster;
    TextView name;
    TextView spaces;
}
public void add(Object object){
    super.add(object);
    list.add(object);
}
public int getCount(){
    return this.list.size();
}

@Override
public Object getItem(int position) {
    return this.list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row;
    row=convertView;
    DataHandler handler;

    if(convertView==null)
    {
        row=inflater.inflate(R.layout.second_layout,parent,false);
        handler=new DataHandler();
        handler.poster=(ImageView)row.findViewById(R.id.move_poster);
        handler.name=(TextView)row.findViewById(R.id.move_title);
        handler.spaces=(TextView)row.findViewById(R.id.move_rating);
        row.setTag(handler);
    }
    else {

        handler=(DataHandler)row.getTag();
    }
    MoveDataProvider dataProvider;

    dataProvider=(MoveDataProvider)this.getItem(position);
    handler.poster.setImageResource
         (dataProvider.getMove_poster_resource());
    handler.name.setText(dataProvider.getLot_name());
    handler.spaces.setText(dataProvider.getLot_spaces());
    return row;




}
}

**SingleToView.xml**
package com.example.sahan.smartpark;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; 
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Sahan on 2015-10-12.
*/
public class MoveAdapter extends ArrayAdapter{
List list=new ArrayList();

public MoveAdapter(Context context, int resource) {
    super(context, resource);
}


static class DataHandler
{
    ImageView poster;
    TextView name;
    TextView spaces;
}
public void add(Object object){
    super.add(object);
    list.add(object);
}
public int getCount(){
    return this.list.size();
}

@Override
public Object getItem(int position) {
    return this.list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row;
    row=convertView;
    DataHandler handler;

    if(convertView==null)
    {

        row=inflater.inflate(R.layout.second_layout,parent,false);
        handler=new DataHandler();
        handler.poster=(ImageView)row.findViewById(R.id.move_poster);
        handler.name=(TextView)row.findViewById(R.id.move_title);
        handler.spaces=(TextView)row.findViewById(R.id.move_rating);
        row.setTag(handler);
    }
    else {

        handler=(DataHandler)row.getTag();
    }
    MoveDataProvider dataProvider;

    dataProvider=(MoveDataProvider)this.getItem(position);

    handler.poster.setImageResource
    (dataProvider.getMove_poster_resource());

    handler.name.setText(dataProvider.getLot_name());
    handler.spaces.setText(dataProvider.getLot_spaces());
    return row;

}
}

**strings.xml**

<resources>
<string name="app_name">SmartPark</string>

<string name="action_settings">Settings</string>
<string-array name="lot_name">
    <item>  Parking Lot A</item>
    <item>  Parking Lot B</item>

</string-array>

<string-array name="lot_spaces">
    <item>Available:  225</item>
    <item>Available:  107</item>

</string-array>

</resources>

我得到的错误是我的SingleLotView.java代码:

  

不兼容的类型 - 预期的android.widget.TextView但找到了java.lang.string

1 个答案:

答案 0 :(得分:2)

BorderLayout

应该是:

lotName = i.getStringArrayExtra("lotName");