当我尝试重置ImageView时 iv.setImageResource(R.drawable.swagimage2); 它使程序崩溃。
原始图片与我正在尝试替换它的尺寸相同。当我创建一个新项目时它起作用了。
我尝试了回答在这个问题中建议的内容,但它没有奏效。 setImageResource is not working
我尝试使用其他setImage方法,但它们不起作用。还有什么我可以尝试的吗?
package com.example.swgalizer;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
public class MainActivity extends Activity{
double randomNum; //Generates how many stars
mainMethods mm = new mainMethods(); //Dialog methods
TextView tv1; //TextView for each star.
TextView tv2;
TextView tv3;
TextView tv4;
TextView tv5;
ImageView iv;
TextView starText;
Button button;
int[] set;
int setCount = 0;
protected void onCreate(Bundle savedInstanceState) {
iv = (ImageView) findViewById(R.id.mainImage);
iv.setImageResource(R.drawable.swagimage2);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
set = mm.RandomSetDialog(this);
TextViews();
addListenerOnButton();
}
//Sets swagbutton LongClickable
public void addListenerOnButton(){
button = (Button) findViewById(R.id.swagbutton);
button.setLongClickable(true);
button.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
char[] inputArray = mm.getInputArray();
randomNum = Math.random();
TextViews();
//ImageView iv = (ImageView) findViewById(R.drawable.ic_launcher);
RatingBar rb = (RatingBar) findViewById(R.id.ratingBar1);
//Toast.makeText(this, a, Toast.LENGTH_LONG).show();
if(inputArray.length > setCount){
if(inputArray[setCount] == '1')
randomNum = .1;
if(inputArray[setCount] == '2')
randomNum = .3;
if(inputArray[setCount] == '3')
randomNum = .5;
if(inputArray[setCount] == '4')
randomNum = .7;
if(inputArray[setCount] == '5')
randomNum = .9;
}
if(randomNum < .2){
tv1.setVisibility(View.VISIBLE); //Set Visibility of text
tv1.setText(getStarString(1)); //Set text for text
starText.setText("1 Star Swag"); //Set num stars text
rb.setNumStars(1); //Sets num of stars
iv.setImageResource(R.drawable.foreveralone);
}
else if(randomNum <= .4){
getStarString(2);
tv2.setVisibility(View.VISIBLE);
starText.setText("2 Star Swag");
tv2.setText(getStarString(2));
rb.setNumStars(2);
}
else if(randomNum <= .6){
getStarString(3);
tv3.setVisibility(View.VISIBLE);
starText.setText("3 Star Swag");
tv3.setText(getStarString(3));
rb.setNumStars(3);
}
else if(randomNum <= .8){
getStarString(4);
tv4.setText(getStarString(4));
starText.setText("4 Star Swag");
tv4.setVisibility(View.VISIBLE);
rb.setNumStars(4);
}
else if(randomNum <= 1){
getStarString(5);
tv5.setText(getStarString(5));
tv5.setVisibility(View.VISIBLE);
starText.setText("5 Star Swag");
rb.setNumStars(5);
}
setCount++;
return true;
}
});
}
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;
}
private void TextViews(){
tv1 = (TextView) findViewById(R.id.text1);
tv2 = (TextView) findViewById(R.id.text2);
tv3 = (TextView) findViewById(R.id.text3);
tv4 = (TextView) findViewById(R.id.text4);
tv5 = (TextView) findViewById(R.id.text5);
starText = (TextView) findViewById(R.id.startext);
tv1.setVisibility(View.GONE);
tv2.setVisibility(View.GONE);
tv3.setVisibility(View.GONE);
tv4.setVisibility(View.GONE);
tv5.setVisibility(View.GONE);
}
//This method gets the text string for the star
private String getStarString(int star){
Random rnd = new Random();
if(star == 1){
String[] star1 = {"You suck", "Solja boy has more swag then you!", "You trash ass nigga"};
return star1[rnd.nextInt(star1.length)];
}
if(star == 2){
String[] star2 = {"You suck", "Solja boy has more swag then you!", "You trash ass nigga"};
return star2[rnd.nextInt(star2.length)];
}
if(star == 3){
String[] star3 = {"You suck", "Solja boy has more swag then you!", "You trash ass nigga"};
return star3[rnd.nextInt(star3.length)];
}
if(star == 4){
String[] star4 = {"You suck", "Solja boy has more swag then you!", "You trash ass nigga"};
return star4[rnd.nextInt(star4.length)];
}
if(star == 5){
String[] star5 = {"You are amazing", "Still Great", "I'd give you a trophy if I had one"};
return star5[rnd.nextInt(star5.length)];
}
return "idk";
}
}
答案 0 :(得分:1)
在设置内容视图之前和super.onCreate之前,您正在初始化IV。之后再打电话
答案 1 :(得分:1)
在访问任何UI元素之前,您需要先设置屏幕的内容视图。只需在onCreate
函数中交换东西:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//first show your layout
setContentView(R.layout.activity_main);
//then retrieve UI elements and configure them
iv = (ImageView) findViewById(R.id.mainImage);
iv.setImageResource(R.drawable.swagimage2);
set = mm.RandomSetDialog(this);
TextViews();
addListenerOnButton();
}