oncreate() 関数から showQuestion() メソッドを呼び出そうとすると、null ポインター例外が発生しますが、ボタンを介して呼び出そうとすると機能します。アクティビティが開いたときに 1 つのメッセージが表示されるように、 onCreate() 関数から showQuestion() を呼び出したいと思います。
public class MainActivity extends Activity {
TextView txt;
Button bt;
int i=0;
static InputStream is = null;
static JSONObject jobj = null;
String line,results="";
static String json = "";
static JSONArray msgList = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new asynt().execute();
txt=(TextView) findViewById(R.id.textView1);
bt= (Button) findViewById(R.id.button1);
showQuestion(i);// null pointer exception
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
i++;
showQuestion(i);
}
});
}
private void showQuestion(int i){
try {
JSONObject jsonobject = msgList.getJSONObject(i);
String s = jsonobject.getString("messg");
txt.setText(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
public class asynt extends AsyncTask<Void, Void, JSONArray>{
@Override
protected JSONArray doInBackground(Void...vois) {
JSONArray tha=null;;
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(getAssets().open("questions.txt")));
StringBuilder sb = new StringBuilder();
while( (line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
reader.close();
json=sb.toString();
}
catch(Exception e){
}
try {
jobj = new JSONObject(json);
tha = jobj.getJSONArray("message");
} catch (JSONException e) {
e.printStackTrace();
}
return tha;
}
@Override
protected void onPostExecute(JSONArray result) {
// TODO Auto-generated method stub
//super.onPostExecute(result);
msgList = result;
}
}
}
アップデート:
09-11 21:14:39.751: E/AndroidRuntime(31440): FATAL EXCEPTION: main
09-11 21:14:39.751: E/AndroidRuntime(31440): java.lang.NullPointerException
09-11 21:14:39.751: E/AndroidRuntime(31440): at com.examp.ankurtesting.MainActivity.showQuestion(MainActivity.java:60)
09-11 21:14:39.751: E/AndroidRuntime(31440): at com.examp.ankurtesting.MainActivity.access$0(MainActivity.java:58)
09-11 21:14:39.751: E/AndroidRuntime(31440): at com.examp.ankurtesting.MainActivity$1.onClick(MainActivity.java:53)
09-11 21:14:39.751: E/AndroidRuntime(31440): at android.view.View.performClick(View.java:4129)
09-11 21:14:39.751: E/AndroidRuntime(31440): at android.view.View$PerformClick.run(View.java:17143)
09-11 21:14:39.751: E/AndroidRuntime(31440): at android.os.Handler.handleCallback(Handler.java:615)
09-11 21:14:39.751: E/AndroidRuntime(31440): at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 21:14:39.751: E/AndroidRuntime(31440): at android.os.Looper.loop(Looper.java:137)
09-11 21:14:39.751: E/AndroidRuntime(31440): at android.app.ActivityThread.main(ActivityThread.java:4802)
09-11 21:14:39.751: E/AndroidRuntime(31440): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 21:14:39.751: E/AndroidRuntime(31440): at java.lang.reflect.Method.invoke(Method.java:511)
09-11 21:14:39.751: E/AndroidRuntime(31440): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:813)
09-11 21:14:39.751: E/AndroidRuntime(31440): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:580)
09-11 21:14:39.751: E/AndroidRuntime(31440): at dalvik.system.NativeStart.main(Native Method)