我只在android studio上创建关于水平滚动视图库图像的应用程序,我在我的布局中制作了15个ImageView。当我添加大于五个图像时,应用程序停止
这是我的代码,请任何帮助
package ikh.ago.com.mall;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ConnectivityManager cm = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null != activeNetwork) {
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI || activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
//answer = "You are connected to network";
//Toast.makeText(getApplicationContext(), answer, Toast.LENGTH_LONG).show();
ImageView zarawomen = (ImageView) findViewById(R.id.zarawomen) ;
zarawomen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent n1 = new Intent(MainActivity.this, Massemo.class);
startActivity(n1);
}
});
ImageView massemo_women = (ImageView) findViewById(R.id.massimoduttiwomen) ;
massemo_women.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent n1 = new Intent(MainActivity.this, Massemo.class);
startActivity(n1);
}
});
ImageView mango = (ImageView) findViewById(R.id.mango) ;
mango.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent n1 = new Intent(MainActivity.this, Massemo.class);
startActivity(n1);
}
});
ImageView pbstore = (ImageView) findViewById(R.id.pbstore) ;
pbstore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent n1 = new Intent(MainActivity.this, Massemo.class);
startActivity(n1);
}
});
ImageView bershka2 = (ImageView) findViewById(R.id.bershka2) ;
bershka2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent n1 = new Intent(MainActivity.this, Massemo.class);
startActivity(n1);
}
});
ImageView gap = (ImageView) findViewById(R.id.gap) ;
gap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent n1 = new Intent(MainActivity.this, Massemo.class);
startActivity(n1);
}
});
ImageView massemo_men = (ImageView) findViewById(R.id.massimoduttimen) ;
massemo_men.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent n1 = new Intent(MainActivity.this, Massemo.class);
startActivity(n1);
}
});
ImageView pb = (ImageView) findViewById(R.id.pb) ;
pb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent n1 = new Intent(MainActivity.this, Massemo.class);
startActivity(n1);
}
});
ImageView zaraman = (ImageView) findViewById(R.id.zaraman) ;
zaraman.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent n1 = new Intent(MainActivity.this, Massemo.class);
startActivity(n1);
}
});
ImageView cafehello = (ImageView) findViewById(R.id.cafehello) ;
cafehello.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent n1 = new Intent(MainActivity.this, Massemo.class);
startActivity(n1);
}
});
ImageView costa = (ImageView) findViewById(R.id.costa) ;
costa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent n1 = new Intent(MainActivity.this, Massemo.class);
startActivity(n1);
}
});
ImageView compume = (ImageView) findViewById(R.id.compume) ;
compume.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent n1 = new Intent(MainActivity.this, Massemo.class);
startActivity(n1);
}
});
ImageView englander = (ImageView) findViewById(R.id.englander) ;
englander.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent n1 = new Intent(MainActivity.this, Massemo.class);
startActivity(n1);
}
});
ImageView casio = (ImageView) findViewById(R.id.casio) ;
casio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent n1 = new Intent(MainActivity.this, Massemo.class);
startActivity(n1);
}
});
} else {
//answer = "No Network Connectivity";
//Toast.makeText(getApplicationContext(), answer, Toast.LENGTH_LONG).show();
ImageView zarawomen = (ImageView) findViewById(R.id.zarawomen) ;
zarawomen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "No network connectivity", Toast.LENGTH_SHORT).show();
}
});
ImageView massemo_women = (ImageView) findViewById(R.id.massimoduttiwomen) ;
massemo_women.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "No network connectivity", Toast.LENGTH_SHORT).show();
}
});
ImageView mango = (ImageView) findViewById(R.id.mango) ;
mango.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "No network connectivity", Toast.LENGTH_SHORT).show();
}
});
ImageView pbstore = (ImageView) findViewById(R.id.pbstore) ;
pbstore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "No network connectivity", Toast.LENGTH_SHORT).show();
}
});
ImageView bershka2 = (ImageView) findViewById(R.id.bershka2) ;
bershka2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "No network connectivity", Toast.LENGTH_SHORT).show();
}
});
ImageView gap = (ImageView) findViewById(R.id.gap) ;
gap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "No network connectivity", Toast.LENGTH_SHORT).show();
}
});
ImageView massemo_men = (ImageView) findViewById(R.id.massimoduttimen) ;
massemo_men.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "No network connectivity", Toast.LENGTH_SHORT).show();
}
});
ImageView pb = (ImageView) findViewById(R.id.pb) ;
pb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "No network connectivity", Toast.LENGTH_SHORT).show();
}
});
ImageView zaraman = (ImageView) findViewById(R.id.zaraman) ;
zaraman.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "No network connectivity", Toast.LENGTH_SHORT).show();
}
});
ImageView cafehello = (ImageView) findViewById(R.id.cafehello) ;
cafehello.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "No network connectivity", Toast.LENGTH_SHORT).show();
}
});
ImageView costa = (ImageView) findViewById(R.id.costa) ;
costa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "No network connectivity", Toast.LENGTH_SHORT).show();
}
});
ImageView compume = (ImageView) findViewById(R.id.compume) ;
compume.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "No network connectivity", Toast.LENGTH_SHORT).show();
}
});
ImageView englander = (ImageView) findViewById(R.id.englander) ;
englander.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "No network connectivity", Toast.LENGTH_SHORT).show();
}
});
ImageView casio = (ImageView) findViewById(R.id.casio) ;
casio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "No network connectivity", Toast.LENGTH_SHORT).show();
}
});
}
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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);
}
@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.main, 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_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}

<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="ikh.ago.com.mall.MainActivity"
tools:showIn="@layout/app_bar_main"
android:background="#000000">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/horizontalScrollView"
android:fillViewport="true"
android:overScrollMode="always">
<LinearLayout
android:orientation="horizontal"
android:id="@+id/mygallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/zarawomen"
android:layout_width="500dp"
android:layout_height="393dp"
android:src="@drawable/zarawomen" />
<ImageView
android:layout_width="500dp"
android:layout_height="393dp"
android:id="@+id/massimoduttiwomen"
android:src="@drawable/massimoduttiwomen" />
<ImageView
android:layout_width="500dp"
android:layout_height="393dp"
android:id="@+id/mango"
android:src="@drawable/mango" />
<ImageView
android:layout_width="500dp"
android:layout_height="393dp"
android:id="@+id/pbstore"
android:src="@drawable/pbstore" />
<ImageView
android:layout_width="500dp"
android:layout_height="393dp"
android:id="@+id/bershka2"
android:src="@drawable/bershka2" />
<ImageView
android:layout_width="500dp"
android:layout_height="393dp"
android:id="@+id/gap"
android:src="@drawable/gap" />
<ImageView
android:layout_width="500dp"
android:layout_height="393dp"
android:id="@+id/massimoduttimen"
android:src="@drawable/massimoduttimen" />
<ImageView
android:layout_width="500dp"
android:layout_height="393dp"
android:id="@+id/pb"
android:src="@drawable/pb" />
<ImageView
android:layout_width="500dp"
android:layout_height="393dp"
android:id="@+id/zaraman"
android:src="@drawable/zaraman" />
<ImageView
android:layout_width="500dp"
android:layout_height="393dp"
android:id="@+id/cafehello"
android:src="@drawable/cafehello" />
<ImageView
android:layout_width="500dp"
android:layout_height="393dp"
android:id="@+id/costa"
android:src="@drawable/costa" />
<ImageView
android:layout_width="500dp"
android:layout_height="393dp"
android:id="@+id/compume"
android:src="@drawable/compume" />
<ImageView
android:layout_width="500dp"
android:layout_height="393dp"
android:id="@+id/englander"
android:src="@drawable/englander" />
<ImageView
android:layout_width="500dp"
android:layout_height="393dp"
android:id="@+id/casio"
android:src="@drawable/casio" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
&#13;
答案 0 :(得分:0)
什么是你的应用停止时的异常消息?,我认为这是为了消耗内存,尝试把android:largeHeap =&#34; true&#34;在您的manifest.xml的应用程序标记中。
此外,我认为如果您使用Horizontal LayoutManager执行RecyclerView,这是更好的实现,使用此表单可以防止创建15 ImageView,因为RecyclerView会重用您的xml资源。 RecyclerView Adapter使用ViewHolder Pattern,在这种情况下,也许可以帮到你。