Java RuntimeException:无法实例化活动组件信息小部件
我在SOF上到处找了找,没有找到任何有用的东西 首先,我要说发生了什么。当我运行我的小部件时,一切正常,直到我选中listView项上的复选框并运行以下代码:Java RuntimeException:无法实例化活动组件信息小部件,java,android,runtime-error,instantiationexception,Java,Android,Runtime Error,Instantiationexception,我在SOF上到处找了找,没有找到任何有用的东西 首先,我要说发生了什么。当我运行我的小部件时,一切正常,直到我选中listView项上的复选框并运行以下代码: @Override public void onClick(View v) { if (addCheckbox.isChecked()) { System.out.println("Checked");
@Override
public void onClick(View v) {
if (addCheckbox.isChecked()) {
System.out.println("Checked");
PackageManager pm = mContext.getPackageManager();
final int DEST_IMAGE_WIDTH = 100;
final int DEST_IMAGE_HEIGHT = 100;
ApplicationInfo appInfo = mContext.getApplicationInfo();
Drawable appIcon = pm.getApplicationIcon(appInfo);
Bitmap appBmp = Bitmap.createBitmap(DEST_IMAGE_WIDTH, DEST_IMAGE_HEIGHT, Config.ARGB_8888);
// Creates a new canvas based on the image specification
// created just above.
Canvas canvas = new Canvas(appBmp);
// (optional) Fills the entire canvas
canvas.drawColor(Color.WHITE);
// You need to set bounds otherwise a 0,0 sized image would be drawn.
appIcon.setBounds(0, 0, DEST_IMAGE_WIDTH, DEST_IMAGE_HEIGHT);
appIcon.draw(canvas);
/// Let's save to a .jpg file ...
File file = new File(mContext.getFilesDir().getAbsolutePath() + "/test2.jpg");
FileOutputStream out;
try
{
file.createNewFile();
out = mContext.getApplicationContext().openFileOutput("BitmapImage", Context.MODE_PRIVATE);
appBmp.compress(Bitmap.CompressFormat.JPEG, 80, out);
Log.i("AppInfoAdapter", "the icon(s) have been saved");
out.close();
// Load back the image file to confirm it works
// Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath() );
// ImageView imageV = (ImageView)findViewById(R.id.);
// imageV.setImageBitmap(bitmap);
}
catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
catch (IOException e2)
{
e2.printStackTrace();
}
Intent intent = new Intent (v.getContext(), GridViewAdapter.class);
v.getContext().startActivity(intent);
Log.i("AppInfoAdapter", "New intent started to send icon bitmap");
} else {
System.out.println("Un-Checked");
}
一切都很好,我甚至收到日志消息“New intent开始发送图标位图”。但是,一旦完成此操作,我就会出现以下错误:
12-14 13:47:54.413: E/AndroidRuntime(1785): FATAL EXCEPTION: main
12-14 13:47:54.413: E/AndroidRuntime(1785): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.awesomefilebuilderwidget/com.example.awesomefilebuilderwidget.GridViewAdapter}: java.lang.InstantiationException: com.example.awesomefilebuilderwidget.GridViewAdapter
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1746)
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1854)
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.app.ActivityThread.access$1500(ActivityThread.java:135)
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1041)
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.os.Looper.loop(Looper.java:150)
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.app.ActivityThread.main(ActivityThread.java:4333)
12-14 13:47:54.413: E/AndroidRuntime(1785): at java.lang.reflect.Method.invokeNative(Native Method)
12-14 13:47:54.413: E/AndroidRuntime(1785): at java.lang.reflect.Method.invoke(Method.java:507)
12-14 13:47:54.413: E/AndroidRuntime(1785): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-14 13:47:54.413: E/AndroidRuntime(1785): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-14 13:47:54.413: E/AndroidRuntime(1785): at dalvik.system.NativeStart.main(Native Method)
12-14 13:47:54.413: E/AndroidRuntime(1785): Caused by: java.lang.InstantiationException: com.example.awesomefilebuilderwidget.GridViewAdapter
12-14 13:47:54.413: E/AndroidRuntime(1785): at java.lang.Class.newInstanceImpl(Native Method)
12-14 13:47:54.413: E/AndroidRuntime(1785): at java.lang.Class.newInstance(Class.java:1409)
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.app.Instrumentation.newActivity(Instrumentation.java:1040)
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1738)
12-14 13:47:54.413: E/AndroidRuntime(1785): ... 11 more
12-14 12:57:04.047: E/AndroidRuntime(708): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.awesomefilebuilderwidget/com.example.awesomefilebuilderwidget.GridViewAdapter}; have you declared this activity in your AndroidManifest.xml?
这是所讨论的课程:
package com.example.awesomefilebuilderwidget;
IMPORTS
public class GridViewAdapter extends BaseAdapter {
private Context Context;
// Keep all Images in array list
public ArrayList<Integer> drawables = new ArrayList<Integer>();
CheckBox mCheckBox=null;
// Constructor
public GridViewAdapter(Context c){
Context = c;
Log.d("GridViewAdapter", "Constructor is set");
drawables.add(R.drawable.pattern1);
Log.d("GridViewAdapter", "pattern1 added");
drawables.add(R.drawable.pattern2);
Log.d("GridViewAdapter", "pattern2 added");
drawables.add(R.drawable.trashcan);
Log.d("GridViewAdapter", "trashcan added");
drawables.add(R.drawable.ic_launcher);
Log.d("GridViewAdapter", "ic_launcher added");
}
public void setCheckBox(CheckBox checkbox){
mCheckBox=checkbox;
}
@Override
// How many items are in the data set represented by this Adapter
public int getCount() {
return drawables.size();
}
@Override
// Get the data item associated with the specified position in the
// data set
public Object getItem(int position) {
return drawables.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public boolean isSdReadable() {
boolean mExternalStorageAvailable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = true;
Log.i("isSdReadable", "External storage card is readable.");
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
Log.i("isSdReadable", "External storage card is readable.");
mExternalStorageAvailable = true;
} else {
// Something else is wrong. It may be one of many other
// states, but all we need to know is we can neither read nor write
mExternalStorageAvailable = false;
}
return mExternalStorageAvailable;
}
public Bitmap getThumbnail() {
final String APP_PATH_SD_CARD = "/TEST/";
final String APP_THUMBNAIL_PATH_SD_CARD = "thumbnails";
String filename = "AFBWIcon.png";
String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + APP_PATH_SD_CARD + APP_THUMBNAIL_PATH_SD_CARD;
Bitmap thumbnail = null;
// Look for the file on the external storage
try {
if (isSdReadable() == true) {
thumbnail = BitmapFactory.decodeFile(fullPath + "/" + filename);
}
} catch (Exception e) {
Log.e("getThumbnail() on external storage", e.getMessage());
}
// If no file on external storage, look in internal storage
if (thumbnail == null) {
try {
File filePath = Context.getFileStreamPath(filename);
FileInputStream fi = new FileInputStream(filePath);
thumbnail = BitmapFactory.decodeStream(fi);
} catch (Exception ex) {
Log.e("getThumbnail() on internal storage", ex.getMessage());
}
}
return thumbnail;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Try to reuse the views
ImageView view = (ImageView) convertView;
boolean checked = (mCheckBox==null)?false:(((CheckBox) mCheckBox).isChecked());
// if convert view is null then create a new instance else reuse it
if (view == null) {
view = new ImageView(Context);
Log.d("GridViewAdapter", "new imageView added");
}
if(checked == true){
isSdReadable();
try {
Log.i("GridViewAdapter", "checkbox is checked");
FileInputStream in = Context.openFileInput("BitmapImage");
// Load back the image file to confirm it works
Bitmap bitmap = BitmapFactory.decodeStream(in);
view.setImageBitmap(bitmap);
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// getThumbnail();
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Log.e("GridView", "Icons not for use/checkbox not checked");
}
view.setImageResource(drawables.get(position));
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
view.setTag(String.valueOf(position));
return view;
}
}
我似乎在选择是否上课之间左右为难
到目前为止,我还没有遇到这个问题,我已经尝试清理我的项目
请帮忙
有人告诉我,因为它是一个适配器,而不是一个活动,所以我应该从清单中删除它
对
但是,当我这样做时,我会得到以下错误:
12-14 13:47:54.413: E/AndroidRuntime(1785): FATAL EXCEPTION: main
12-14 13:47:54.413: E/AndroidRuntime(1785): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.awesomefilebuilderwidget/com.example.awesomefilebuilderwidget.GridViewAdapter}: java.lang.InstantiationException: com.example.awesomefilebuilderwidget.GridViewAdapter
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1746)
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1854)
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.app.ActivityThread.access$1500(ActivityThread.java:135)
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1041)
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.os.Looper.loop(Looper.java:150)
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.app.ActivityThread.main(ActivityThread.java:4333)
12-14 13:47:54.413: E/AndroidRuntime(1785): at java.lang.reflect.Method.invokeNative(Native Method)
12-14 13:47:54.413: E/AndroidRuntime(1785): at java.lang.reflect.Method.invoke(Method.java:507)
12-14 13:47:54.413: E/AndroidRuntime(1785): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-14 13:47:54.413: E/AndroidRuntime(1785): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-14 13:47:54.413: E/AndroidRuntime(1785): at dalvik.system.NativeStart.main(Native Method)
12-14 13:47:54.413: E/AndroidRuntime(1785): Caused by: java.lang.InstantiationException: com.example.awesomefilebuilderwidget.GridViewAdapter
12-14 13:47:54.413: E/AndroidRuntime(1785): at java.lang.Class.newInstanceImpl(Native Method)
12-14 13:47:54.413: E/AndroidRuntime(1785): at java.lang.Class.newInstance(Class.java:1409)
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.app.Instrumentation.newActivity(Instrumentation.java:1040)
12-14 13:47:54.413: E/AndroidRuntime(1785): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1738)
12-14 13:47:54.413: E/AndroidRuntime(1785): ... 11 more
12-14 12:57:04.047: E/AndroidRuntime(708): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.awesomefilebuilderwidget/com.example.awesomefilebuilderwidget.GridViewAdapter}; have you declared this activity in your AndroidManifest.xml?
这是因为在上面显示的代码中,您错误地调用了startActivity()
,试图启动GridViewAdapter
作为一项活动<代码>网格iewAdapter不是一项活动。如果要启动活动,请编写活动,将其添加到清单中,然后为新编写的活动调用startActivity()
您可以在中了解更多有关活动的信息。因此,使用
startActivity()
,我可以做什么**请检查添加的section@user2909006:这在技术上可行吗?对我认为这是一个好的设计吗?否。自定义视图
,IMHO不应调用startActivity()
。它应该引发一个事件,由宿主活动或片段通过侦听器拾取。活动或片段应该确定如何处理事件,这当然可能涉及启动另一个活动。