2014-11-23 62 views
0

我想创建一个Android的资产管理器,将从文本文件和图像打开文本。但是,当我尝试运行它时,它说它已停止工作。使用logcat的,这是我能找到的错误,我认为可能会导致此问题:Android应用程序抛出错误,它已停止工作,android.widget.ImageView无法投射到android.view.ViewGroup

java.lang.ClassCastException:android.widget.ImageView不能转换到android.view.ViewGroup

这里主活动代码:

package AssetDemo.eoghanalphagame; 

import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.os.Bundle; 
import android.view.Window; 
import android.view.WindowManager; 

public class Asset_Activity extends Activity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Define the type of activity we want created - no title, full screen 
    // and keep the screen on whilst it is visible. This needs to be 
    // completed before any components 
    // are inflated. 
    Window window = getWindow(); 
    window.requestFeature(Window.FEATURE_NO_TITLE); 
    window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

    // Set the content view up to hold a single fragment 
    setContentView(R.layout.fragment_asset_); 

    // Add whatever fragment is appropriate for the derived call - calling 
    // the overloaded createFragment() method to retrieve the fragment. 
    FragmentManager fm = getFragmentManager(); 
    Fragment fragment = fm.findFragmentById(R.id.fragment_asset_ImageView); 

    if (fragment == null) { 
     fragment = createFragment(); 
     fm.beginTransaction().add(R.id.fragment_asset_ImageView, fragment) 
       .commit(); 
    } 
} 

/** 
* Get the fragment for this particular activity 
* 
* @return Fragment for this activity 
*/ 
public Fragment createFragment(){ 
    return new AssetTestFragment(); 
} 
} 

下面是片段代码:

package AssetDemo.eoghanalphagame; 

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 

import android.app.Fragment; 
import android.content.res.AssetManager; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class AssetTestFragment extends Fragment { 


@Override 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) 
{ 


    //inflate the view 
    View view = inflater.inflate(R.layout.fragment_asset_, container, false); 

    //get assest manager for current activity and load in text and image 

    AssetManager assetManager = getActivity().getAssets(); 
    String text = loadText(assetManager, "Text/test.txt"); 
    Bitmap bitmap = loadBitmap(assetManager, "Images/TheVanishingOfEthanCarter_logo_t.png"); 

    //Display the text 
    TextView outputTextView = (TextView) view.findViewById(R.id.fragment_asset_TextView); 
    outputTextView.setText(text != null ? text: "ERROR: Could not open text file."); 

    //Display the bitmap 
    ImageView imageView = (ImageView) view.findViewById(R.id.fragment_asset_ImageView); 
    if(bitmap != null) 
    { 
     imageView.setImageBitmap(bitmap); 
    } 
    return view; 

} 


//Create the loadText method 

private String loadText(AssetManager assetManager, String asset) 
{ 
    String text = null; 
    InputStream inputStream = null; 
    try 
    { 
     //Try to open text file 
     inputStream = assetManager.open(asset); 

     //Load in Text in 4k chunks 
     ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); 
     byte[] chunk = new byte[4096]; 
     int len = 0; 
     while((len = inputStream.read(chunk)) > 0) 
      byteStream.write(chunk, 0, len); 

     //convert and return as a UFT8 String 
     text = new String(byteStream.toByteArray(), "UTF8"); 

    }catch (IOException e) 
    { 

     Log.e(getActivity().getResources().getString(R.string.LOG_TAG), 
       "Error loading text asset: " + e.getMessage()); 
    } finally 
    { 
     if(inputStream != null) 
      try 
     { 
       inputStream.close(); 
     } catch (IOException e) 
     {/*returns a null}*/ 

     } 

    } 

    return text; 
} 

//Load the Bitmap 


private Bitmap loadBitmap(AssetManager assetManager, String asset) 
{ 
    Bitmap bitmap = null; 
    InputStream inputStream = null; 
    try 
    { 
     //Try to open the bitmap 
     inputStream = assetManager.open(asset); 

     //Setup load prefrences 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 

     //Load the Bitmap 
     bitmap = BitmapFactory.decodeStream(inputStream, null, options); 

    } catch (IOException e) 
    { 
     Log.e(getActivity().getResources().getString(R.string.LOG_TAG), 
       "Error Loading Bitmap: " +e.getMessage()); 
    } finally 
    { 
     if(inputStream != null) 
      try 
      { 
       inputStream.close(); 
      } catch (IOException e) 
      {/*returns a null*/ 

      } 
    } 
    return bitmap; 
} 

} 

这里是TextView的和ImageView的中创建的XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
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" 
tools:context="AssetDemo.eoghanalphagame.Asset_Activity$PlaceholderFragment" > 

<ImageView 
    android:id="@+id/fragment_asset_ImageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/fragment_asset_TextView" 
    android:layout_below="@+id/fragment_asset_TextView" 
    android:layout_marginTop="28dp" 
    android:contentDescription="@string/desc" /> 


<TextView 
    android:id="@+id/fragment_asset_TextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="16dp" 
    android:text="@string/hello_world" /> 

</RelativeLayout> 

我想知道的是为什么它不工作,需要改变什么才能使它工作。我创建了所有我引用的文件。我觉得这是简单的我想念的东西,但作为新的android开发我不知道是什么。

谢谢

+0

'FragmentTransaction#add()'方法需要一个ViewGroup的ID,而不是ImageView。另外,你不应该为Activity和Fragment使用相同的布局。 – 2014-11-23 14:59:06

+0

@MikeM。我想你应该发布这个答案,只是我的想法..无论如何.. – Elltz 2014-11-23 15:01:09

+0

@MikeM。我正在使用相同的Layou。我正在使用fragment_asset。这个FragmentTransaction#add()方法在哪里?它不在我的代码中。 – thenthapple 2014-11-23 19:04:11

回答

0

有时一个“干净”后的构建会使事情变好。

因为如果您更改视图的ID,可能会出现提到的异常。

+0

这不起作用 – thenthapple 2014-11-23 19:00:10