마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

왜 에러가 나는지 모르겟어요..

0 추천
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.Toast;

import com.example.smartnameplate.R;

public class TabLayoutActivity extends TabActivity {
 EditText LOGid = null;
 EditText LOGpw = null;

 ArrayList<String> arr = new ArrayList<String>();
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.testmain);

  EditText loginpw = (EditText) findViewById(R.id.loginpw);
  EditText idtestplz = (EditText) findViewById(R.id.idtestplz);

  Resources res = getResources();
  TabHost tabHost = getTabHost();
  TabHost.TabSpec spec;

  Intent intent;
  intent = new Intent().setClass(this, TabActivity1.class);
  spec = tabHost.newTabSpec("Tab1");
  spec.setIndicator("Main",
    res.getDrawable(android.R.drawable.ic_menu_agenda));
  spec.setContent(intent);
  tabHost.addTab(spec);
  intent = new Intent().setClass(this, TabActivity2.class);
  spec = tabHost.newTabSpec("Tab2");
  spec.setIndicator("Preview",
    res.getDrawable(android.R.drawable.ic_menu_edit));
  spec.setContent(intent);
  tabHost.addTab(spec);

  intent = new Intent().setClass(this, TabActivity3.class);
  spec = tabHost.newTabSpec("Tab3");
  spec.setIndicator("Update",
    res.getDrawable(android.R.drawable.ic_menu_help));
  spec.setContent(intent);
  tabHost.addTab(spec);

  intent = new Intent().setClass(this, TabActivity4.class);
  spec = tabHost.newTabSpec("Tab4");
  spec.setIndicator("Download",
    res.getDrawable(android.R.drawable.ic_menu_day));
  spec.setContent(intent);
  tabHost.addTab(spec);

  intent = new Intent().setClass(this, TabActivity5.class);
  spec = tabHost.newTabSpec("Tab5");
  spec.setIndicator("Upload",
    res.getDrawable(android.R.drawable.ic_menu_help));
  spec.setContent(intent);
  tabHost.addTab(spec);

  Context mContext = getApplicationContext();
  LayoutInflater inflater = (LayoutInflater) mContext
    .getSystemService(LAYOUT_INFLATER_SERVICE);
  View layout = inflater.inflate(R.layout.custom_dialog,
    (ViewGroup) findViewById(R.id.layout_root));

  AlertDialog.Builder aDialog = new AlertDialog.Builder(
    TabLayoutActivity.this);
  aDialog.setTitle("로그인하시겠습니까?");
  aDialog.setView(layout);

  aDialog.setPositiveButton("로그인", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
    readText();
    EditText LOGid = (EditText) findViewById(R.id.loginid);
    for (int i = 0; i < 5; i++) {
     Log.d("for",
       "******************for성공*****************");
     if (arr.get(i).toString().equalsIgnoreCase(LOGid.getText().toString())) {
     //if(arr.get(i).toString() == LOGid.getText().toString()){
      Log.d("if", "****************if성공**************");
      Toast.makeText(TabLayoutActivity.this, "로그인 되었습니다.",
        Toast.LENGTH_SHORT).show();
     } else {
      Toast.makeText(TabLayoutActivity.this, "로그인 실패하였습니다.",
        Toast.LENGTH_SHORT).show();
     }
    }
   }
  });
  aDialog.setNegativeButton("취소", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
    Toast.makeText(TabLayoutActivity.this, "어플이 종료됩니다.",
      Toast.LENGTH_SHORT).show();
    finish();
   }
  });
  AlertDialog ad = aDialog.create();
  ad.show();

 }

 public void readText() {
  try {
   Log.i("124312", "sfsdfsfasfscfsd cfsearbsd");
   Resources resources = getResources();
   BufferedReader br = new BufferedReader(new InputStreamReader(
     resources.openRawResource(R.raw.mytest2), "UTF-8"));
   String readLine;
   while ((readLine = br.readLine()) != null) {
    arr.add(readLine + "\n");
   }
   br.close();
   // EditText edtFile = (EditText) findViewById(R.id.edtFile);
   // edtFile.setText(arr.get(0));
  } catch (Exception e) {
   Toast.makeText(this, "읽기 오류 : " + e, 4000).show();
  }
 }
}

mytest2

abc
def
ghi
mno
pqr

로그

 

05-22 03:13:52.371: E/Trace(2124): error opening trace file: No such file or directory (2)
05-22 03:13:52.971: D/dalvikvm(2124): GC_FOR_ALLOC freed 56K, 4% free 8285K/8583K, paused 190ms, total 191ms
05-22 03:13:52.982: I/dalvikvm-heap(2124): Grow heap (frag case) to 8.758MB for 649280-byte allocation
05-22 03:13:53.091: D/dalvikvm(2124): GC_CONCURRENT freed <1K, 4% free 8919K/9223K, paused 72ms+9ms, total 107ms
05-22 03:13:53.562: I/Choreographer(2124): Skipped 80 frames!  The application may be doing too much work on its main thread.
05-22 03:13:53.572: D/gralloc_goldfish(2124): Emulator without GPU emulation detected.
05-22 03:13:56.522: I/124312(2124): sfsdfsfasfscfsd cfsearbsd
05-22 03:13:56.533: D/for(2124): ******************for성공*****************
05-22 03:13:56.533: D/AndroidRuntime(2124): Shutting down VM
05-22 03:13:56.533: W/dalvikvm(2124): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
05-22 03:13:56.541: E/AndroidRuntime(2124): FATAL EXCEPTION: main
05-22 03:13:56.541: E/AndroidRuntime(2124): java.lang.NullPointerException
05-22 03:13:56.541: E/AndroidRuntime(2124):  at com.tabactivity.TabLayoutActivity$1.onClick(TabLayoutActivity.java:95)
05-22 03:13:56.541: E/AndroidRuntime(2124):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
05-22 03:13:56.541: E/AndroidRuntime(2124):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 03:13:56.541: E/AndroidRuntime(2124):  at android.os.Looper.loop(Looper.java:137)
05-22 03:13:56.541: E/AndroidRuntime(2124):  at android.app.ActivityThread.main(ActivityThread.java:4745)
05-22 03:13:56.541: E/AndroidRuntime(2124):  at java.lang.reflect.Method.invokeNative(Native Method)
05-22 03:13:56.541: E/AndroidRuntime(2124):  at java.lang.reflect.Method.invoke(Method.java:511)
05-22 03:13:56.541: E/AndroidRuntime(2124):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-22 03:13:56.541: E/AndroidRuntime(2124):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-22 03:13:56.541: E/AndroidRuntime(2124):  at dalvik.system.NativeStart.main(Native Method)
모르겟다zz (650 포인트) 님이 2013년 5월 22일 질문
모르겟다zz님이 2013년 5월 22일 수정

1개의 답변

0 추천
에러 로그가 없기때문에 확신할 수는 없지만

인덱스 아웃 오브 익셉션이 나지 않았을까 생각이 되네요..

 

for문을 돌릴때 조건으로 들어가는 i를 무조건 10번을 돌리고 있는데

그 i를 이용해서 ArrayList의 데이터를 뽑아올때 index로 사용하고 있네요.

 

만약 ArrayList의 size가 5인데 6번째 값을 받아오라고 했다면? 그때 예외가 발생하겠죠...

조건을 ArrayList 객체의 size만큼만 for문을 돌도록 수정을 하셔야 할겁니다.
이라슈 (8,770 포인트) 님이 2013년 5월 22일 답변
size 만큼 수정을햇느데도... 에러가 나네요.. ㅠ 로그를 올려드리는게 나을거같네요 수정해서 올릴게요
TabLayoutActivity.java:93 에서 null포인트 예외가 발생하는것 같은데요 확인 해보시겠어요?
초기화 시켜줫는데.. ㅠ_ㅜ..  안되네요 여기 엑티비티 전체적인 소스를 올려볼게요 ㅠ
EditText LOGid = (EditText) findViewById(R.id.loginid);
이부분이 문제가 되는것 같네요.
loginid가 다이얼로그의 layout에 포함된 것이 아니라면 에러가 나는것이 맞을것 같네요.
onCreate같은 부분에서 전역으로 EditText객체를 초기화해서 한번 사용해보세요.
...