return 123L;
}
private Globals getApplication() {
// TODO 自動生成されたメソッド・スタブ
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
Log.d(TAG, "onProgressUpdate - " + values[0]);
//dialog.setProgress(values[0]);
}
@Override
protected void onCancelled() {
Log.d(TAG, "onCancelled");
//dialog.dismiss();
}
@Override
protected void onPostExecute(Long result) {
Log.d(TAG, "onPostExecute - " + result);
}
private TextView findViewById(Object textView1) {
// TODO 自動生成されたメソッド・スタブ
return null;
}
@Override
public void onCancel(DialogInterface dialog) {
Log.d(TAG, "Dialog onCancell... calling cancel(true)");
this.cancel(true);
}
}
メインスレッド--------------------------------------------------------------------
package com.example.asynctest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class AsyncTest extends Activity
implements OnClickListener {
private Button btn0,btn1,btn2;
//グローバル変数
Globals globals;
final String TAG = "AsyncTest";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_async_test);
((Button)findViewById(R.id.button1))
.setOnClickListener(this);
btn0 = (Button)findViewById(R.id.button2);
btn0.setOnClickListener(this);
btn1 = (Button)findViewById(R.id.button3);
btn1.setOnClickListener(this);
btn2 = (Button)findViewById(R.id.button4);
btn2.setOnClickListener(this);
//グローバル変数を取得
globals = (Globals) this.getApplication();
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
new AsyncTest2(this).execute("Param1");
break;
case R.id.button2:
btn0.setText("FileActivity");
Intent intent = new Intent(this, FileActivity.class);
startActivityForResult(intent, 0);
break;
case R.id.button3:
btn1.setText("SubThred");
Intent intent1 = new Intent(this, SubThred.class);
startActivityForResult(intent1,0);
break;
case R.id.button4:
btn2.setText("表示");
/*
EditText editText = (EditText)findViewById(R.id.edittext0);
globals = (Globals) this.getApplication();
Editable input=editText.getText();
editText.settext(globals.items);
*/
new AlertDialog.Builder(this)
.setTitle("Select an item !")
.setItems(globals.items, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int i) {
Toast.makeText(AsyncTest.this
, String.format("%s が選択されました。", globals.items[i])
, Toast.LENGTH_LONG).show();
}
})
.show();
}
}
}
これでは配列の”items”をグローバル変数にしてバックグラウンドでファイルリストを”items”に入れた後にメインスレッドでAlertDialogで呼び出しています。
しかしJCIFSでファイルリストを取得するところですでにエラーが出ています。
グローバル変数を使う前はエラーが出ていなかったのと、Logcatを見てみるとグローバル変数を使っているところでsystem.errと出ているのでおそらくグローバル変数の使い方が間違っているのだと思いますがどこが間違っているのか分かりません。
Logcat--------------------------------------------------------------------------------------------------------
java.lang.NullPointerException
at com.example.asynctest.AsyncTest2.doInBackground(AsyncTest2.java:45)
at com.example.asynctest.AsyncTest2.doInBackground(AsyncTest2.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:252)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
at java.lang.Thread.run(Thread.java:1020)
グローバル変数を定義しているクラス----------------------------------------------------------------
package com.example.asynctest;
import jcifs.smb.SmbFile;
import android.app.Application;
public class Globals extends Application {
String[] items;
SmbFile[] files;
public void GlobalsAllInit() {
}
}
ManifestFile-----------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="
http://schemas.android.com/apk/res/android"
package="com.example.asynctest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<library />
<application
android:name="com.example.asynctest.Globals"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name="com.example.asynctest.AsyncTest"
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="com.example.asynctest.FileActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.asynctest.Globals"
android:label="@string/title_activity_globals" >
</activity>
<activity
android:name="com.example.asynctest.SubThred"
android:label="@string/title_activity_sub_thred" >
</activity>
</application>
</manifest>
よろしくお願いします。