我在两个不同的Android设备上运行我的应用程序。第一个是Arnova 9 G2平板电脑,第二个是Sony Xperia P.在Arnova平板电脑中我的应用程序执行成功,但在Sony Xperia P手机中,LogCat显示以下错误:
/data/data/com.example.androidjnetpcaptest/interfaces:open failed: EACCES(许可被拒绝)致命例外:主要 java.lang.NullPointerException at com.example.androidjnetpcaptest.InterfacesFragment.onCreateView(InterfacesFragment.java:35)
我的清单XML文件是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidjnetpcaptest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.pemission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SplashScreenActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.Default" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".InformationsActivity"
android:theme="@android:style/Theme.Holo"
android:label="@string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.Default" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
InterfacesFragment类是:
public class InterfacesFragment extends Fragment
{
private Scanner inputStream = null;
private TextView interfacesTextView = null;
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
try
{
inputStream = new Scanner( new FileInputStream( "/data/data/com.example.androidjnetpcaptest/interfaces" ) );
}
catch ( FileNotFoundException e )
{
Log.e( "InterfacesFragment_ERROR: ", e.getMessage() );
}
String lines = "";
while ( inputStream.hasNextLine() )
{
lines = lines + inputStream.nextLine() + "\n";
}
inputStream.close();
final ProgressDialog ringProgressDialog = ProgressDialog.show( getActivity(), "Please wait ...", "Finding interfaces ...", true );
ringProgressDialog.setCancelable( true );
new Thread(new Runnable()
{
@Override
public void run()
{
try
{
Thread.sleep( 4000 );
}
catch ( Exception e )
{
}
ringProgressDialog.dismiss();
}
}).start();
View rootView = inflater.inflate(R.layout.interfaces, container, false);
interfacesTextView = ( TextView ) rootView.findViewById( R.id.interfacesTextView );
interfacesTextView.setText( lines );
interfacesTextView.setMovementMethod( new ScrollingMovementMethod() );
return rootView;
}
}
答案 0 :(得分:1)
麻烦在于使用绝对路径/data/data/com.example. androidjnetpcaptest/interfaces
。您不应该假设这是应用程序的数据位置。应用程序可以移动到外部存储,因为ICS系统支持多个用户(人),其中应用程序为每个用户提供不同的存储空间。相反,使用Context.getFilesDir()
来获取可以创建和存储文件的应用程序私有位置。
您无需任何特殊权限即可读取/写入此空间。