我在下面的布局文件中: 2个列表视图, 2 textview, 2个mapfragments, 当我尝试使用adatper将我的String []加载到第一个listView时我有奇怪的错误。有什么建议是错的吗?我确定这个问题是由于listview和适配器造成的。当我删除它并开始没有适配器的活动时,布局正在加载正常。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LLglowny"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1"
tools:context="com.example.norbert.routespreparation2.DistanceAdvisor">
<LinearLayout
android:id="@+id/LLtrasa1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical">
<TextView
android:id="@+id/TvInfoTrasa1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TEST"/>
<ListView
android:id="@+id/LvTrasa1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5">
</ListView>
<fragment android:id="@+id/map1"
class="com.google.android.gms.maps.SupportMapFragment"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5">
</fragment>
</LinearLayout>
<LinearLayout
android:id="@+id/LLtrasa2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="@+id/TvInfoTrasa2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TEST"/>
<ListView
android:id="@+id/LvTrasa2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5">
</ListView>
<fragment
android:id="@+id/map2"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5">
</fragment>
</LinearLayout>
</LinearLayout>
和类代码:
public class DistanceAdvisor extends AppCompatActivity implements OnMapReadyCallback {
@BindView(R.id.LvTrasa1)
ListView LvTrasa1;
@BindView(R.id.LvTrasa2)
ListView LvTrasa2;
@BindView(R.id.LLtrasa1)
LinearLayout LLtrasy;
@BindView(R.id.LLglowny)
LinearLayout LLglowny;
@BindView(R.id.TvInfoTrasa1)
TextView TvInfoTrasa1;
@BindView(R.id.TvInfoTrasa2)
TextView TvInfoTrasa2;
private GoogleMap map1, map2;
private int ItemClickedPosition;
private DatabaseHelper databaseHelper;
private Cursor data;
private String[] tablicaAdresow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_distance_advisor);
ButterKnife.bind(this);
SupportMapFragment supportMapFragment1 = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1);
SupportMapFragment supportMapFragment2 = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map2);
supportMapFragment1.getMapAsync(this);
supportMapFragment2.getMapAsync(this);
Bundle b = getIntent().getExtras();
ItemClickedPosition = b.getInt("ItemClicked", 0);
databaseHelper = new DatabaseHelper(this);
data = databaseHelper.getData();
tablicaAdresow = new String[100000];
StworzTabliceAdresow();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.activity_distance_advisor, R.id.LvTrasa1, tablicaAdresow);
LvTrasa1.setAdapter(adapter);
}
private void StworzTabliceAdresow() {
String stringAdresow;
data.moveToPosition(ItemClickedPosition);
stringAdresow = data.getString(4);
tablicaAdresow = stringAdresow.split(Pattern.quote("|"));
Log.d("pokaz", String.valueOf(tablicaAdresow.length));
}
@Override
public void onMapReady(GoogleMap googleMap) {
map1 = googleMap;
map2 = googleMap;
}
}
和错误日志:
FATAL EXCEPTION: main
Process: com.example.norbert.routespreparation2, PID: 7764
android.view.InflateException: Binary XML file line #34: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1673)
at android.widget.AbsListView.onLayout(AbsListView.java:2151)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1692)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #34: Duplicate id 0x
答案 0 :(得分:0)
这很愚蠢,但我忘了使用TextView创建单行布局。 例如,如果使用id为sLineLayout的make单行布局将包含带有id行的TextView,则ArrayAdapter初始化应如下所示: ArrayAdapter adapter = new ArrayAdapter(this,R.layout.sLineLayout,R.id.row,tab);
这很容易,也许它有助于某人:)