2013-03-21 73 views
0

我想在我的Android應用程序中創建某種日誌記錄過程。 我已經設法檢測用戶在屏幕上按下的位置(三次)並從中創建日誌序列。 在端,我要顯示哪些表示被選擇的日誌記錄序列(從我的繪製文件夾)3個圖像爲什麼我不能在自定義對話框中顯示圖像?

我已動態設置可繪製ID和當我把圖像上主要佈局它的工作原理。 但是,如果我將圖像置於自定義對話框中,我會強行關閉。 從logcat中我看到以下內容:顯示java.lang.NullPointerExceptionimage1.setImageResource(iIdSlike);

如果我告訴繪製ID,因爲它是確定,ID是主要和自定義對話框佈局相同的文字。

我得到強制關閉,即使我設置這樣的形象(非動態):

image1.setImageResource(R.drawable.s11); 

爲什麼我不能顯示在定製對話框的圖像?

這是我dijalog.xml(自定義對話框佈局):

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/imgDrugi" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/TextView01" 
     android:layout_below="@+id/TextView01" 
     android:layout_marginRight="27dp" 
     android:layout_marginTop="51dp" 
      /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="Button" /> 

    <TextView 
     android:id="@+id/TextView01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="118dp" 
     android:text="@+id/TextView01" /> 

    </RelativeLayout> 

這是我的代碼:

public class MyWorkLogiranje extends Activity implements OnClickListener 
{ 
    @Override 
    public boolean onTouchEvent(MotionEvent event) 
    { 
     switch(event.getAction()) 
     { 
      case MotionEvent.ACTION_DOWN:     
       . 
       . 
       .    
         //calculations to get string sPrvi 


        sPrvi="s31"; //For testing purposes sPrvi set manually   

        //Image and text on main layout (it works) 
        iIdSlike = getResources().getIdentifier(sPrvi, "drawable", getPackageName()); 
        ImageView image = (ImageView) findViewById(R.id.imgPrvi); 
        image.setImageResource(iIdSlike); 

        TextView text1 = (TextView)findViewById(R.id.txtPrvi); 
        text1.setText("iIdSlike: " + iIdSlike); 

        //Prikaz dijaloga 

        final Dialog dialog = new Dialog(MyWorkLogiranje.this); 
        dialog.setContentView(R.layout.dijalog); 
        dialog.setTitle("This is my custom dialog box"); 
        dialog.setCancelable(true); 
        dialog.getContext(); 

        TextView text = (TextView) dialog.findViewById(R.id.TextView01); 
        text.setText("iIdSlike: " + iIdSlike); 
        ImageView image1 = (ImageView) findViewById(R.id.imgDrugi); 
        image1.setImageResource(R.drawable.iIdSlike); 
        //image1.setImageResource(R.drawable.s11); 

        //Podešavanje dugmeta 
        Button button = (Button) dialog.findViewById(R.id.button1); 
        button.setOnClickListener(new OnClickListener() 
        { 
         @Override 
         public void onClick(View v) 
         { 
          dialog.dismiss(); 
         } 
        }); 
        dialog.show(); 

誰能幫我這個好嗎? 我爲此撓了兩天的腦袋。 謝謝。

+2

使用ImageView image1 =(ImageView)dialog.findViewById(R.id.imgDrugi); – 2013-03-21 09:19:46

+0

試試我的答案.. – Nezam 2013-03-21 09:21:36

+0

是的,是的,是的,它的工作原理。謝謝。 – Amicussamir 2013-03-21 09:29:27

回答

1

您的findViewById失敗。所以你得到NullPointerException。

ImageView image1 = (ImageView) findViewById(R.id.imgDrugi); 

應該

ImageView image1 = (ImageView) dialog.findViewById(R.id.imgDrugi); 
0

試試這個:

image1.setImageDrawable(getResources().getDrawable(iIdSlike)); 
0

在此之前,添加這個在你的對話框代碼:

ImageView image1 = (ImageView) dialog.findViewById(R.id.imgDrugi); 

您可以用這種方式:

Drawable image = ImageOperations(context,ed.toString(),"image.jpg"); 
ImageView imgView = new ImageView(context); 
imgView = (ImageView)findViewById(R.id.image1); 
imgView.setImageDrawable(image); 

setImageDrawable(getResources().getDrawable(R.drawable.icon)); 

這將返回你想通過做訪問...那麼你可以設置圖像中的ImageView提拉的ID以下

int id = getResources().getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null); 
imageview.setImageResource(id); 
+0

我很猶豫在這裏問問題(閱讀兩天後),但現在我發現我錯了。謝謝大家的快速和有用的迴應。你們都搖滾。 – Amicussamir 2013-03-21 09:31:20

相關問題