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

DB 연동때문에 질문드립니다.

0 추천
//////////

public class Food_Info extends Activity {

 private DBManager dbm;
 int temp = 0;
 String name, info, ingredients, calorie, time, process, tip;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.food_info);
  
  LinearLayout layout = (LinearLayout)findViewById(R.id.foods);
  try{
   
  dbm = new DBManager(this);
  SQLiteDatabase db = dbm.getWritableDatabase();

     Intent it = getIntent();
        Bundle bundle = it.getExtras();
     int position = bundle.getInt("food_position");
  
  Cursor cursor = db.rawQuery("select Name, Info, Ingredients, Calorie, Time, Process, Tip from FoodDataBase", null);
  
  
  while (cursor.moveToNext()) {
            //선택한 아이템과 커서의 위치가 일치할때 해당 _id값을  temp로 가져옴(아 이거땜에 디지는줄아랐네)
   if(cursor.getPosition() == position) {
             
  
  name = cursor.getString(cursor.getColumnIndex("Name"));  
  info = cursor.getString(cursor.getColumnIndex("Info"));
  ingredients = cursor.getString(cursor.getColumnIndex("Ingredients"));
  calorie = cursor.getString(cursor.getColumnIndex("Calorie"));
  time = cursor.getString(cursor.getColumnIndex("Time"));
  process = cursor.getString(cursor.getColumnIndex("Process"));
  tip = cursor.getString(cursor.getColumnIndex("Tip"));
  
   }
  }
  TextView food_info1 = new TextView(this);
  food_info1.append(name);
  food_info1.setTextSize(80);
  food_info1.setTextColor(Color.rgb(255, 255, 0));
  layout.addView(food_info1);
  
  TextView food_info2 = new TextView(this);
  food_info2.append("설명 : " + info +"\n");
  food_info2.append("재료 : " + ingredients +"\n");
  food_info2.append("칼로리 : " + calorie +"\n");
  food_info2.append("시간 : " + time +"\n");
  food_info2.append("요리과정 : " + process +"\n");
  food_info2.append("팁 : " + tip +"\n");
  food_info2.setTextSize(20);
  layout.addView(food_info2);
  
  cursor.close();
  dbm.close();
  
 }catch(SQLiteException e) {
  }
 }

//////////////////////////////////////////////////////
디비

package com.andro;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBManager extends SQLiteOpenHelper{
 
 private static String DB_NAME = "FoodDataBase";
 
 //DBManager 클래스의 객체가 만들어질 때 실행됨(생성자)
 public DBManager(Context context){
  //DB를 생섬함(이미 생성된 경우는 생성되지 않음)
  super(context, DB_NAME, null, 1);
 }
 
 @Override
 public void onCreate(SQLiteDatabase db){
  //테이블을 생성함(이미 생성된 경우는 생성되지 않음)
  db.execSQL("create table FoodDataBase (Number integer primary key, Name varchar, Info text, Ingredients varchar," +
       "Calorie varchar, Time varchar, Process text, Tip varchar);");
 }
 
 //존재하는 DB와 버전이 다른 경우
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
     
    }
}

//////////////////////////////////////////
리스트 아이템 선택시.

       list.setOnItemClickListener(new OnItemClickListener() {
               public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
             Intent intent =  new Intent(ListActivity.this, Food_Info.class);
             intent.putExtra("food_position", position);
             startActivity(intent);
             
        }
       });
}

 

코드상에서는 오류가 없다고 나옵니다. 그런데 실행만 하면 Food_Info클래스가 작동이 중지되었다고 안되네요ㅜㅜ

몇시간째 잡고 있는데 도와주세요.

eucharis7 (140 포인트) 님이 2013년 12월 12일 질문
12-12 00:30:26.332: W/dalvikvm(7573): threadid=1: thread exiting with uncaught exception (group=0x415a6b90)
12-12 00:30:26.332: E/AndroidRuntime(7573): FATAL EXCEPTION: main
12-12 00:30:26.332: E/AndroidRuntime(7573): Process: com.andro, PID: 7573
12-12 00:30:26.332: E/AndroidRuntime(7573): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andro/com.andro.Food_Info}: java.lang.NullPointerException
12-12 00:30:26.332: E/AndroidRuntime(7573):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at android.os.Looper.loop(Looper.java:137)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at android.app.ActivityThread.main(ActivityThread.java:4998)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at java.lang.reflect.Method.invokeNative(Native Method)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at java.lang.reflect.Method.invoke(Method.java:515)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at dalvik.system.NativeStart.main(Native Method)
12-12 00:30:26.332: E/AndroidRuntime(7573): Caused by: java.lang.NullPointerException
12-12 00:30:26.332: E/AndroidRuntime(7573):     at android.widget.TextView.append(TextView.java:3395)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at com.andro.Food_Info.onCreate(Food_Info.java:55)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at android.app.Activity.performCreate(Activity.java:5243)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-12 00:30:26.332: E/AndroidRuntime(7573):     ... 11 more

오류정보는 이러합니다.

1개의 답변

0 추천

오류에 NullPointerException이 발생했네요.

 콜스택에 append 함수에서 오류가 발생했습니다.

 

12-12 00:30:26.332: E/AndroidRuntime(7573): Caused by: java.lang.NullPointerException
12-12 00:30:26.332: E/AndroidRuntime(7573):     at android.widget.TextView.append(TextView.java:3395)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at com.andro.Food_Info.onCreate(Food_Info.java:55)
12-12 00:30:26.332: E/AndroidRuntime(7573):     at android.app.Activity.performCreate(Activity.java:5243)

 아래의 소스에서 name이 null 인지 체크해보세요.

name = cursor.getString(cursor.getColumnIndex("Name"));  

  
익명사용자 님이 2013년 12월 12일 답변
TextView food_info1 = new TextView(this);
  food_info1.append("이름=" + name);

 따위로 고치면 죽지는 않겠네요.
...