我从Android Studio获得此错误需要帮助。我的堆栈跟踪有错误。
02-20 21:52:39.849 1521-1521/com.example.lenovo_thinkpad.mobilelectureboard E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lenovo_thinkpad.mobilelectureboard, PID: 1521
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lenovo_thinkpad.mobilelectureboard/com.example.lenovo_thinkpad.mobilelectureboard.TpMainActivity}: android.view.InflateException: Binary XML file line #0: Error inflating class com.example.lenovo_thinkpad.mobilelectureboard.ui.PaintView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class com.example.lenovo_thinkpad.mobilelectureboard.ui.PaintView
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1929)
at com.example.lenovo_thinkpad.mobilelectureboard.TpMainActivity.onCreate(TpMainActivity.java:77)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1929)
at com.example.lenovo_thinkpad.mobilelectureboard.TpMainActivity.onCreate(TpMainActivity.java:77)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.lenovo_thinkpad.mobilelectureboard.TpApplication
at com.example.lenovo_thinkpad.mobilelectureboard.ui.PaintRunner.<init>(PaintRunner.java:95)
at com.example.lenovo_thinkpad.mobilelectureboard.ui.PaintView.<init>(PaintView.java:69)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1929)
at com.example.lenovo_thinkpad.mobilelectureboard.TpMainActivity.onCreate(TpMainActivity.java:77)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
我正在尝试对自定义PaintView
进行充气。我不理解堆栈跟踪,所以我尝试了在其他相关问题中看到的所有内容。对不起,我实际上并不知道自己在做什么。我只是继续前进并克隆了一个来自GitHub的项目,试图在进展过程中学习和理解项目。
我正在尝试执行Activity
。堆栈跟踪表明错误位于第77行。那就是
setContentView(R.layout.activity_threadpaint);
以下是具有上述行的TpMainActivity.java
的完整代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_threadpaint);
instance = this;
paintView = (PaintView) findViewById(R.id.view_paint_view);
paintView.setToolButtonAnimator(this);
buttonColor = (Button) findViewById(R.id.btn_tool_color);
buttonBrush = (Button) findViewById(R.id.btn_tool_brush);
buttonMove = (Button) findViewById(R.id.btn_tool_move);
buttonFill = (Button) findViewById(R.id.btn_tool_fill);
buttonErase = (Button) findViewById(R.id.btn_tool_erase);
buttonUndo = (Button) findViewById(R.id.btn_tool_undo);
buttonRedo = (Button) findViewById(R.id.btn_tool_redo);
toolButtons = new ArrayList<View>();
Collections.addAll(toolButtons, buttonColor, buttonBrush, buttonMove, buttonFill, buttonErase, buttonUndo,
buttonRedo);
setSelectedBackground(buttonBrush);
TpPreferencesActivity.addCallbackForPreference(TpMainActivity.this, Preference.LOCKORIENTATION);
TpPreferencesActivity.addCallbackForPreference(TpMainActivity.this, Preference.MOVETHRESHOLD);
}
@Override
public void onDestroy() {
Log.w(TAG, "PaintView destroyed");
TpPreferencesActivity.removeCallback(TpMainActivity.this);
paintView.stopPaintThread();
super.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle b) {
Log.d(TAG, "onSaveInstanceState");
paintView.saveState(b);
}
@Override
protected void onRestoreInstanceState(Bundle b) {
Log.d(TAG, "onRestoreInstanceState");
paintView.restoreState(b);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_save:
showSaveDialog();
return true;
case R.id.menu_load:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, REQ_LOAD);
return true;
case R.id.menu_clear:
paintView.resetCanvas();
return true;
case R.id.menu_prefs:
Intent i = new Intent(this, TpPreferencesActivity.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// Tool Button handler declared in xml.
public void onToolButtonClicked(View button) {
switch (button.getId()) {
case R.id.btn_tool_color:
showColorPickerDialog();
break;
case R.id.btn_tool_brush:
if (paintView.selectedTool() == PaintView.Tool.BRUSH) {
showBrushPickerDialog();
} else {
paintView.selectTool(PaintView.Tool.BRUSH);
setSelectedBackground(buttonBrush);
}
break;
case R.id.btn_tool_move:
paintView.selectTool(PaintView.Tool.MOVE);
setSelectedBackground(buttonMove);
break;
case R.id.btn_tool_fill:
paintView.fillWithPaint();
break;
case R.id.btn_tool_erase:
paintView.selectTool(PaintView.Tool.ERASE);
paintView.setPaintColor(Color.TRANSPARENT);
setSelectedBackground(buttonErase);
break;
case R.id.btn_tool_undo:
paintView.undo();
break;
case R.id.btn_tool_redo:
paintView.redo();
break;
}
}
private void setSelectedBackground(Button button) {
buttonColor.setBackgroundResource(R.drawable.button_tool_color);
buttonBrush.setBackgroundResource(R.drawable.button_tool_brush);
buttonMove.setBackgroundResource(R.drawable.button_tool_move);
buttonFill.setBackgroundResource(R.drawable.button_tool_fill);
buttonErase.setBackgroundResource(R.drawable.button_tool_erase);
switch (button.getId()) {
case R.id.btn_tool_color:
button.setBackgroundResource(R.drawable.button_tool_color_selected);
break;
case R.id.btn_tool_brush:
button.setBackgroundResource(R.drawable.button_tool_brush_selected);
break;
case R.id.btn_tool_move:
button.setBackgroundResource(R.drawable.button_tool_move_selected);
break;
case R.id.btn_tool_fill:
button.setBackgroundResource(R.drawable.button_tool_fill_selected);
break;
case R.id.btn_tool_erase:
button.setBackgroundResource(R.drawable.button_tool_erase_selected);
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_LOAD:
if (resultCode == RESULT_OK) {
Uri imageUri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(imageUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
final File imageFile = new File(cursor.getString(columnIndex));
cursor.close();
String loadMessge = getResources().getString(R.string.dialog_load);
final ProgressDialog load = ProgressDialog.show(TpMainActivity.this, "", loadMessge, true);
Thread thread = new Thread() {
@Override
public void run() {
Bitmap bitmap = Utils.decodeFile(TpMainActivity.this, imageFile);
paintView.setBitmap(bitmap);
load.dismiss();
}
};
thread.start();
}
break;
}
}
@Override
public void fadeOutToolButtons() {
Animation fadeOut = AnimationUtils.loadAnimation(this, R.anim.alpha_out);
animateViews(toolButtons, fadeOut);
}
@Override
public void fadeInToolButtons() {
Animation fadeIn = AnimationUtils.loadAnimation(this, R.anim.alpha_in);
animateViews(toolButtons, fadeIn);
}
/**
* Animate all the views with the speciefied animation.
*
* @param views Views to animate
* @param a Animation to use
*/
private void animateViews(List<View> views, Animation a) {
for (int i = 0; i < views.size(); i++) {
a.reset();
views.get(i).clearAnimation();
views.get(i).startAnimation(a);
}
}
/**
* Instantiates a new ColorPickerDialog if necessary and shows it.
*/
private void showColorPickerDialog() {
if (colorPickerDialog == null) {
colorPickerDialog = new ColorPickerDialog(this, paintView);
}
colorPickerDialog.show();
}
/**
* Instantiates a new BrushPickerDialog if necessary and shows it.
*/
private void showBrushPickerDialog() {
if (brushPickerDialog == null) {
brushPickerDialog = new BrushPickerDialog(this, paintView);
}
brushPickerDialog.show();
}
private void showSaveDialog() {
final SaveFileDialog dialog = new SaveFileDialog(this, paintView.getBitmap());
dialog.show();
}
@Override
public void preferenceChanged(SharedPreferences preferences, String key) {
if (key.equals(Preference.LOCKORIENTATION.key())) {
boolean lock = preferences.getBoolean(key, true);
Log.d(TAG, "lockScreenOrientation " + lock);
Utils.lockScreenOrientation(lock, this);
} else if (key.equals(Preference.MOVETHRESHOLD.key())) {
float f = 1.0f;
try {
f = Float.parseFloat(preferences.getString(key, "1.0f"));
} catch (NumberFormatException e) {
Log.e(TAG, "ERROR ", e);
CharSequence text = getResources().getString(R.string.toast_float_parse_error);
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
Log.d(TAG, "setMoveThreshold " + Float.toString(f));
paintView.setMoveThreshold(f);
}
}
现在这里是我试图膨胀的自定义布局的代码。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.lenovo_thinkpad.mobilelectureboard.ui.PaintView
android:id="@+id/view_paint_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<Button android:id="@+id/btn_tool_color"
style="@style/ToolButton"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:background="@drawable/button_tool_color" />
<Button android:id="@+id/btn_tool_brush"
style="@style/ToolButton"
android:layout_below="@id/btn_tool_color"
android:background="@drawable/button_tool_brush" />
<Button android:id="@+id/btn_tool_move"
style="@style/ToolButton"
android:layout_below="@id/btn_tool_brush"
android:background="@drawable/button_tool_move" />
<Button android:id="@+id/btn_tool_fill"
style="@style/ToolButton"
android:layout_below="@id/btn_tool_move"
android:background="@drawable/button_tool_fill" />
<Button android:id="@+id/btn_tool_erase"
style="@style/ToolButton"
android:layout_below="@id/btn_tool_fill"
android:background="@drawable/button_tool_erase" />
<Button android:id="@+id/btn_tool_undo"
style="@style/ToolButton"
android:layout_below="@id/btn_tool_erase"
android:background="@drawable/button_tool_undo" />
<Button android:id="@+id/btn_tool_redo"
style="@style/ToolButton"
android:layout_below="@id/btn_tool_undo"
android:background="@drawable/button_tool_redo" />
</RelativeLayout>