我必须在Android中测试文件IO,发生未知错误。
如果我禁用InitView和SetOnClickListener函数,它的工作正常
我想知道为什么以及它如何粉碎。然后解决它。
这是代码。
// Last update: 2014/06/02
// Author: David Wu ( Wu,Chen-Hao zh-TW )
// Create date: 2014/06/01
//
// This java file is main program for Android file IO test
// in Android Tablet Nexus 7 ( ver. 2013 )
//
package org.top_wu.fileiotest;
// Java resource package
//
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
// Android resource package
//
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
// Main activity for test program
//
public class Main extends Activity {
//private Context mContext = getApplicationContext();
private Button butnSave;
private Button butnOpen;
private EditText etFilename;
private EditText etContext;
private String filename;
private String context;
private FileOutputStream outputFile;
private FileInputStream inputFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
InitVIew();
/*SetOnClickListener();*/
} // END OF onCreate method in Main class
@Override
protected void onDestroy() {
super.onDestroy();
} // END OF onDestory method in Main class
private void InitVIew() {
butnSave = (Button) findViewById(R.id.butnSave);
butnOpen = (Button) findViewById(R.id.butnOpen);
etFilename = (EditText) findViewById(R.id.etFilename);
etContext = (EditText) findViewById(R.id.etContext);
} // END OF InitVIew in Main class
private void SetOnClickListener() {
butnSave.setOnClickListener(saveFile);
} // END OF SetOnClickListenermethod in Main class
private Button.OnClickListener saveFile = new Button.OnClickListener() {
@Override
public void onClick(View v) {
filename = etFilename.getText().toString();
try {
outputFile = openFileOutput(filename, Context.MODE_PRIVATE);
} // END OF try open output file
catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(
getApplicationContext(),
"ERROR: Open file failure",
Toast.LENGTH_SHORT
).show();
} // END OF catch the open output file error
} // END OF onClick method on button on click listener
}; // END OF saveFile button on click listener
} // END OF Main class
主要布局
<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"
tools:context="${packageName}.${activityClass}" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fileName_label"
android:textSize="24sp" />
<EditText
android:id="@+id/etFilename"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"
android:textSize="24sp" >
<requestFocus />
</EditText>
<Button
android:id="@+id/butnOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/open_label" />
</LinearLayout>
<RelativeLayout
android:id="@+id/fileContextLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/funcButnLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout1" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/context_label"
android:textSize="24sp" />
<EditText
android:id="@+id/etContext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="textMultiLine"
android:scrollbars="vertical" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/funcButnLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="@+id/butnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="@string/save_label"
android:textSize="24sp" />
</RelativeLayout>
</RelativeLayout>
此问题的堆栈跟踪如下:
eclipse.buildId=v22.6.2-1085508
java.version=1.7.0_45
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=zh_TW
Framework arguments: -product com.android.ide.eclipse.adt.package.product
Command-line arguments: -os win32 -ws win32 -arch x86_64 -product com.android.ide.eclipse.adt.package.product
Error
Mon Jun 02 16:29:51 CST 2014
Unhandled event loop exception
java.lang.IllegalArgumentException: Argument not valid
at org.eclipse.swt.SWT.error(SWT.java:4378)
at org.eclipse.swt.SWT.error(SWT.java:4312)
at org.eclipse.swt.SWT.error(SWT.java:4283)
at org.eclipse.swt.widgets.Widget.error(Widget.java:472)
at org.eclipse.swt.widgets.Control.setMenu(Control.java:3477)
at com.android.ddmuilib.logcat.LogCatPanel$20.handleEvent(LogCatPanel.java:1042)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1057)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1081)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1066)
at org.eclipse.swt.widgets.Widget.showMenu(Widget.java:1476)
at org.eclipse.swt.widgets.Widget.wmContextMenu(Widget.java:1578)
at org.eclipse.swt.widgets.Control.WM_CONTEXTMENU(Control.java:4756)
at org.eclipse.swt.widgets.Table.WM_CONTEXTMENU(Table.java:5978)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4615)
at org.eclipse.swt.widgets.Table.windowProc(Table.java:5919)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4990)
at org.eclipse.swt.internal.win32.OS.CallWindowProcW(Native Method)
at org.eclipse.swt.internal.win32.OS.CallWindowProc(OS.java:2443)
at org.eclipse.swt.widgets.Table.callWindowProc(Table.java:564)
at org.eclipse.swt.widgets.Table.callWindowProc(Table.java:430)
at org.eclipse.swt.widgets.Widget.wmRButtonUp(Widget.java:2399)
at org.eclipse.swt.widgets.Control.WM_RBUTTONUP(Control.java:5217)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4679)
at org.eclipse.swt.widgets.Table.windowProc(Table.java:5919)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4990)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2549)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3757)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1113)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:997)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:138)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:610)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:567)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:354)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:181)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:636)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591)
at org.eclipse.equinox.launcher.Main.run(Main.java:1450)
at org.eclipse.equinox.launcher.Main.main(Main.java:1426)