首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

关于android imageview显示的有关问题,求大神解答

2013-06-25 
关于android imageview显示的问题,求大神解答附上代码,没报错,一运行就程序异常,求指教。xml文件?xml vers

关于android imageview显示的问题,求大神解答
附上代码,没报错,一运行就程序异常,求指教。
xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/RLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <TextView
        android:id="@+id/lblComments"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入你所在位置和你要去的位置"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        />
    <EditText
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lblComments"
        android:layout_below="@+id/lblComments"
         />
    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text1"
        android:textSize="40px"
        android:layout_centerHorizontal="true"
        android:text="到"
        />
    <EditText
        android:id="@+id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view1"
        />
    <Button
            android:id="@+id/button6"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="路线搜索" 
            android:layout_below="@+id/text2"/>
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button6"
        android:layout_centerHorizontal="true"
        >
        <ImageView
            android:id="@+id/image2"
            android:src= "@drawable/pic13"


            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </FrameLayout>
</RelativeLayout>
activity文件
package com.example.supermarket;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class ditu extends Activity {
EditText textBox1;
EditText textBox2;
private ImageView image;
@Override
public void onCreate(Bundle savedInstanceState)  {
textBox1 = (EditText) findViewById(R.id.text1);
textBox2 = (EditText) findViewById(R.id.text2);
super.onCreate(savedInstanceState);
setContentView(R.layout.chaoshiditu1);
Button btn6 = (Button)findViewById(R.id.button6);
image = (ImageView) findViewById(R.id.image2);
btn6.setOnClickListener(new OnClickListener() {          
     public void onClick(View v) 
    {              
    String str1 = textBox1.getText().toString();
    String str2 = textBox2.getText().toString();
    if (str1=="1"&&str2=="2"||str1=="2"&&str2=="1") 
    {
    image.setBackgroundResource(R.drawable.pic1);
}
    if (str1=="1"&&str2=="3"||str1=="3"&&str2=="1") 
    {
    image.setBackgroundResource(R.drawable.pic2);
}  
    if (str1=="1"&&str2=="3"||str1=="3"&&str2=="1") 
    {
    image.setBackgroundResource(R.drawable.pic3);
}  
    if (str1=="1"&&str2=="4"||str1=="4"&&str2=="1") 
    {
    image.setBackgroundResource(R.drawable.pic4);
}  
    if (str1=="1"&&str2=="5"||str1=="5"&&str2=="1") 
    {
    image.setBackgroundResource(R.drawable.pic5);
}  
    if (str1=="1"&&str2=="6"||str1=="6"&&str2=="1") 
    {
    image.setBackgroundResource(R.drawable.pic6);
}  
    if (str1=="1"&&str2=="7"||str1=="7"&&str2=="1") 
    {
    image.setBackgroundResource(R.drawable.pic7);
}  
    if (str1=="1"&&str2=="8"||str1=="8"&&str2=="1") 
    {
    image.setBackgroundResource(R.drawable.pic8);
}  
    else {
    image.setBackgroundResource(R.drawable.pic13);
}
    } 
});
}
} Android imageview

布局 String textbox
[解决办法]
你是顺序不对啊

setContentView(R.layout.chaoshiditu1);

然后
textBox1 = (EditText) findViewById(R.id.text1);
textBox2 = (EditText) findViewById(R.id.text2);

这样就不会报错了
[解决办法]
引用:
5#的你好。原来是用的str1.equals(1)也不行啊。

用"1".equals(str1);
要把变量写在括号里。因为变量有可能是null。

热点排行