gpt4 book ai didi

java - 我目前正在为最后一年开发一个应用程序。没有出现错误,但是当我选择我的 searchScreen 应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 17:49:23 25 4
gpt4 key购买 nike

我开始为我最后一年的项目开发一个应用程序。我毫无问题地创建了所有抽屉导航,并且在我运行程序时它们运行良好。当我添加一个与我添加的 mapsAcivity 链接的搜索页面时。没有出现错误,但是应用程序会运行良好,但是当我单击抽屉导航以访问搜索页面和 mapsActivity 页面时,应用程序崩溃了。任何想法是什么问题? Screengrab of the app failing

04-26 23:48:25.311: E/AndroidRuntime(13703): Caused by: android.view.InflateException: Binary XML file line #285: ScrollView can host only one direct child 

04-26 23:48:25.311: E/AndroidRuntime(13703): Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child

以下是我的搜索屏幕的代码
package com.example.mcdai.derrytourism;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class SearchScreen extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

Spinner restaurantSpinner, accommidationSpinner;
ArrayAdapter<CharSequence> adapter;
Button btnSearch;
TextView buttonLogout;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_screen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

final EditText freeTextEntry = (EditText)findViewById(R.id.editFreeTextSearch);
//final EditText editMinDestination = (EditText)findViewById(R.id.editMinDestination);
final EditText editMaxDestination = (EditText)findViewById(R.id.editMaxDestination);
final EditText editMinPrice = (EditText)findViewById(R.id.editMinPrice);
final EditText editMaxPrice = (EditText)findViewById(R.id.editMaxPrice);
final Spinner spinnerRestautantType = (Spinner)findViewById(R.id.spinnerRestaurantType);
final Spinner spinnerAccommidationType = (Spinner)findViewById(R.id. spinnerAccommidationType);
buttonLogout = (TextView)findViewById(R.id.buttonLogout);

editMinPrice.setEnabled(false);
editMaxPrice.setEnabled(false);
spinnerRestautantType.setEnabled(false);
spinnerAccommidationType.setEnabled(false);

//restaurant drop down menu
restaurantSpinner = (Spinner)findViewById(R.id.spinnerRestaurantType);
adapter = ArrayAdapter.createFromResource(this,R.array.restaurant_type, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
restaurantSpinner.setAdapter(adapter);
restaurantSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
TextView defaultPleaseSelect = (TextView) view;
if(position == 0)
{
// Set hint size and colour
defaultPleaseSelect.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
defaultPleaseSelect.setTextColor(Color.GRAY);
}
else
{
// Set dropdown selection and display an onscreen message
Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+" selected", Toast.LENGTH_LONG).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{

}
});


//Accommidation drop down menu
accommidationSpinner = (Spinner)findViewById(R.id.spinnerAccommidationType );
adapter = ArrayAdapter.createFromResource(this,R.array.accommidation, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
accommidationSpinner.setAdapter(adapter);
accommidationSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
TextView defaultPleaseSelect = (TextView) view;
if(position == 0)
{
// Set hint size and colour
defaultPleaseSelect.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
defaultPleaseSelect.setTextColor(Color.GRAY);
}
else
{
// Set dropdown selection and display an onscreen message
Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+" selected", Toast.LENGTH_LONG).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{

}
});



btnSearch = (Button)findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{

Intent intentMapsActivity = new Intent(SearchScreen.this, MapsActivity.class);


//code to get min desitance for destination
//String minDestination = editMinDestination.getText().toString().trim();
//code to get max distance for destination
if(editMaxDestination.getText().toString().equals(""))
{
Toast.makeText(SearchScreen.this, "Please Populate Distance Field", Toast.LENGTH_LONG).show();
}
else
{
Double maxDestination = Double.parseDouble(editMaxDestination.getText().toString());
//code to get from editTextFreeSearch
String freeTextSearchParam = freeTextEntry.getText().toString();
//code to get restaurant type spinner
String restaurantTypeSpinner = spinnerRestautantType.getSelectedItem().toString();
//code to get accommidation type spinner
String accommidationSpinner = spinnerAccommidationType .getSelectedItem().toString();

if(freeTextSearchParam.equals("") && restaurantTypeSpinner.equals("Select") && accommidationSpinner.equals("Select"))
{
Toast.makeText(SearchScreen.this, "Please Populate Free Text, Price or Restaurant Type", Toast.LENGTH_LONG).show();
}
else
{
freeTextSearchParam = freeTextSearchParam.replaceAll("\\s", "\\+");
intentMapsActivity.putExtra("freeTextSearch", freeTextSearchParam);
intentMapsActivity.putExtra("maxDestination", maxDestination);
restaurantTypeSpinner = restaurantTypeSpinner.replaceAll("\\s", "\\+");
intentMapsActivity.putExtra("restaurantType", restaurantTypeSpinner);
accommidationSpinner = accommidationSpinner.replaceAll("\\s", "\\+");
intentMapsActivity.putExtra("accommidation", accommidationSpinner);
startActivity(intentMapsActivity);
}
}
}

});

buttonLogout = (TextView)findViewById(R.id.buttonLogout);
buttonLogout.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(getApplicationContext(), SearchScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}

});


}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if ( v instanceof EditText) {
Rect outRect = new Rect();
v.getGlobalVisibleRect(outRect);
if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
v.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
return super.dispatchTouchEvent( event );
}

public void onRadioButtonClicked(View view)
{
//EditText editMinDestination = (EditText)findViewById(R.id.editMinDestination);
EditText editMaxDestination = (EditText)findViewById(R.id.editMaxDestination);
EditText editMinPrice = (EditText)findViewById(R.id.editMinPrice);
EditText editMaxPrice = (EditText)findViewById(R.id.editMaxPrice);
Spinner spinnerRestautantType = (Spinner)findViewById(R.id.spinnerRestaurantType);
Spinner spinnerAccommidationType = (Spinner)findViewById(R.id.spinnerAccommidationType );
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
boolean unchecked = !((RadioButton) view).isChecked();

switch(view.getId())
{
case R.id.radioPrice:
if (checked)
{
editMinPrice.setEnabled(true);
editMaxPrice.setEnabled(true);
}break;
case R.id.radioRestuarantType:
if (checked)
{
spinnerRestautantType.setEnabled(true);
} break;
case R.id.radioAccommidationType:
if (checked)
{
spinnerAccommidationType.setEnabled(true);
}break;
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_screen, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.nav_MainActivity) {
Intent intentMainActivity = new Intent(SearchScreen.this, MainActivity.class);
startActivity(intentMainActivity);
finish();
} else if (id == R.id.nav_SearchScreen) {
Intent intentSearchScreen = new Intent(SearchScreen.this,SearchScreen.class);
startActivity(intentSearchScreen);
finish();
} else if (id == R.id.nav_Attractions) {
Intent intentAttractions = new Intent(SearchScreen.this, Attractions.class);
startActivity(intentAttractions);
finish();
} else if (id == R.id.nav_Food) {
Intent intentFood = new Intent(SearchScreen.this, Food.class);
startActivity(intentFood);
finish();
} else if (id == R.id.nav_Accommidation) {
Intent intentAccommidation = new Intent(SearchScreen.this,Accommidation.class);
startActivity(intentAccommidation);
finish();
} else if (id == R.id.nav_Entertainment) {
Intent intentEntertainment = new Intent(SearchScreen.this,Entertainment.class);
startActivity(intentEntertainment);
finish();
} else if (id == R.id.nav_MapsActivity) {
Intent intentMapsActivity = new Intent(SearchScreen.this,MapsActivity.class);
startActivity(intentMapsActivity);
finish();
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

}

下面的代码取自 activity_search_screen。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mcdai.derrytourism.SearchScreen">

<TextView
android:id="@+id/txtSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:paddingBottom="50sp"
android:textAlignment="center"
android:textSize="20sp"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:id="@+id/txtFreeTextSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Free-Text Search"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20sp"
android:paddingBottom="20sp"
android:textAppearance="@style/TextAppearance.AppCompat"
android:layout_below="@+id/txtSearch"
android:layout_alignParentLeft="true" />

<Button
android:id="@+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignTop="@+id/txtAccommidationType"
android:layout_toRightOf="@+id/txtDestination"
android:layout_centerHorizontal="true"
android:layout_marginTop="50sp" />

<TextView
android:id="@+id/txtAccommidationType"
android:text="AccommidationType"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat"
android:layout_marginLeft="40dp"
android:layout_alignBottom="@+id/radioGroup"
android:layout_toRightOf="@+id/radioGroup"
android:paddingBottom="10sp" />

<EditText
android:id="@+id/editFreeTextSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:ems="10"
android:inputType="text"
android:layout_below="@+id/txtFreeTextSearch"
android:layout_centerHorizontal="true" />

<Spinner
android:id="@+id/spinnerAccommidationType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/txtAccommidationType"
android:layout_alignParentRight="true" />

<Spinner
android:id="@+id/spinnerRestaurantType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/txtRestuarantType"
android:layout_alignParentRight="true"
android:layout_alignLeft="@+id/spinnerAccommidationType" />

<TextView
android:id = "@+id/buttonLogout"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat"
android:text = "Logout"
android:textSize="15sp"
android:textAlignment="center"
android:clickable="false"
android:textColor="@android:color/holo_blue_dark"
android:onClick="onClick"
android:paddingBottom="50sp"
android:layout_alignBaseline="@+id/txtSearch"
android:layout_alignBottom="@+id/txtSearch"
android:layout_alignParentRight="true" />

<TextView
android:id="@+id/txtMaxPrice"
android:text="Max(£)"
android:textSize="10sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textAlignment="center"
android:layout_above="@+id/editMinPrice"
android:layout_alignParentRight="true"
android:layout_alignLeft="@+id/editMaxPrice"
android:layout_marginBottom="-4sp" />

<EditText
android:id="@+id/editMaxPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:hint="10"
android:textSize="15sp"
android:textAlignment="center"
android:background="@android:color/transparent"
android:inputType="numberDecimal"
android:layout_marginBottom="21dp"
android:layout_above="@+id/spinnerRestaurantType"
android:layout_alignParentRight="true" />

<TextView
android:id="@+id/txtMinPrice"
android:text="Min(£)"
android:textSize="10sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textAlignment="center"
android:layout_above="@+id/editMinPrice"
android:layout_alignLeft="@+id/editMinPrice"
android:layout_toLeftOf="@+id/txtMaxPrice"
android:layout_marginBottom="-4sp"
android:paddingRight="55sp" />

<EditText
android:id="@+id/editMaxDestination"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:hint="10"
android:textSize="15sp"
android:background="@android:color/transparent"
android:textAlignment="center"


android:inputType="number"
android:layout_below="@+id/txtDestination"
android:layout_centerHorizontal="true" />

<TextView
android:id="@+id/txtCriteriaSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Criteria Search"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="20sp"
android:paddingTop="20sp"
android:textAppearance="@style/TextAppearance.AppCompat"
android:paddingBottom="10sp"
android:layout_marginTop="41dp"
android:layout_below="@+id/editFreeTextSearch"
android:layout_centerHorizontal="true" />

<TextView
android:id="@+id/txtDestination"
android:text="Max Distance (km)"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat"
android:layout_below="@+id/editFreeTextSearch"
android:layout_centerHorizontal="true"
android:paddingTop="10sp" />

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="14sp"
android:paddingBottom="6sp"
android:layout_below="@+id/txtCriteriaSearch"
android:layout_alignParentLeft="true">

<RadioButton
android:id="@+id/radioPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:layout_marginTop="10sp"
android:layout_marginBottom="3sp" />

<RadioButton
android:id="@+id/radioRestuarantType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:layout_marginTop="10sp"/>

<RadioButton
android:id="@+id/radioAccommidationType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioRestuarantType"
android:onClick="onRadioButtonClicked"
android:layout_marginTop="15sp" />
</RadioGroup>

<EditText
android:id="@+id/editMinPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:hint="0"
android:textSize="15sp"
android:textAlignment="center"
android:background="@android:color/transparent"
android:inputType="numberDecimal"
android:layout_alignBaseline="@+id/editMaxPrice"
android:layout_alignBottom="@+id/editMaxPrice"
android:layout_toLeftOf="@+id/buttonLogout"
android:paddingRight="55sp" />

<TextView
android:id="@+id/txtPrice"
android:text="Price"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat"
android:paddingTop="7sp"
android:layout_alignTop="@+id/txtMaxPrice"
android:layout_alignLeft="@+id/txtRestuarantType" />

<TextView
android:id="@+id/txtRestuarantType"
android:text="Restuarant Type"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat"
android:layout_marginBottom="26dp"
android:paddingBottom="2sp"
android:layout_above="@+id/txtAccommidationType"
android:layout_alignLeft="@+id/txtAccommidationType"
android:layout_alignStart="@+id/txtAccommidationType" />

</RelativeLayout>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
layout="@layout/app_bar_search_screen"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_search_screen"
app:menu="@menu/activity_search_screen_drawer" />


</android.support.v4.widget.DrawerLayout>
</ScrollView>

最佳答案

您崩溃的原因是您的 ScrollView 中有多个子元素。您需要将它们都包装在 ScrollView 内的父 View 中,或者将 DrawerLayout 移到 ScrollView 之外。

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mcdai.derrytourism.SearchScreen">

<!-- RelativeLayout Children go here -->

</RelativeLayout>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
layout="@layout/app_bar_search_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_search_screen"
app:menu="@menu/activity_search_screen_drawer"/>


</android.support.v4.widget.DrawerLayout>
</FrameLayout>
</ScrollView>

我不确定你希望你的布局是什么样的,但如果你按照上面的操作,你不应该崩溃。确保您使用 Code > Reformat Code 来保持缩进一致。祝你好运!

关于java - 我目前正在为最后一年开发一个应用程序。没有出现错误,但是当我选择我的 searchScreen 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43645538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com