-6
有人可以看看我的代碼嗎?我正在嘗試爲個人學校項目構建此應用程序。我是android studio的新手。我已經組建了一個簡單的應用程序,您可以在停車場顯示可用空間。如何在Android中調試「預期的android.widget.TextView,但發現java.lang.string」?
但現在我試圖打開第二個活動屏幕,當我點擊列表視圖屏幕中的其中一個地段。我得到了這個錯誤,並沒有辦法解決它。
**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中
大聲笑...不錯的拍攝哥們:) – DGN
這似乎是在做伎倆,它實際上是我的一個粗心的錯誤。但是我又遇到了另外一個錯誤,我想知道如果你不介意的話,你是否可以看看它。謝謝一堆。這是我得到的錯誤:AndroidRuntime:java.lang.RuntimeException:無法啓動activityComponentInfo {com.example.sahan.smartpark/com.example.sahan.smartpark.SingleLotView}:java.lang.NullPointerException:試圖讀取null array – SahanWijesinghe
我建議學習使用調試器並逐步瀏覽代碼,以查看導致NPE(NullPointerException)的原因。同時檢查您正在查找的資源的名稱。 – Buddy