我正在尝试为android创建一个资产管理器,它将从文本文件和图像中打开文本。但是,当我尝试运行它时,它说它已经停止工作了。使用LogCat,这是我能找到的错误,我认为可能导致问题:
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 :(得分:0)
有时候,“干净”之后的构建会让事情变得更好。
因为如果您更改了视图的ID,则可能会出现上述异常。