2013-10-25 3 views
0

나는 무슨 일이 일어나고 있는지 모르겠다. 두 개의 다른 프로젝트에서이 두 프로젝트를 만들었다. 둘 다 동일한 mysql 테이블에서 데이터를 가져온다. 그런 다음 문제가 발생하면 두 개의 프로젝트를 하나의 Java 파일로 혼합합니다. F1 누군가 제발 할 수 있어요 ??java.lang.NullPointerException OnItemClickListener

로그 캣

10-25 03:04:51.024: E/AndroidRuntime(2227): FATAL EXCEPTION: main 
10-25 03:04:51.024: E/AndroidRuntime(2227): java.lang.NullPointerException 
10-25 03:04:51.024: E/AndroidRuntime(2227):  at com.hadafsoft.smartaddis.Music$3.onItemClick(Music.java:133) 
10-25 03:04:51.024: E/AndroidRuntime(2227):  at android.widget.AdapterView.performItemClick(AdapterView.java:298) 
10-25 03:04:51.024: E/AndroidRuntime(2227):  at android.widget.AbsListView.performItemClick(AbsListView.java:1100) 
10-25 03:04:51.024: E/AndroidRuntime(2227):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788) 
10-25 03:04:51.024: E/AndroidRuntime(2227):  at android.widget.AbsListView$1.run(AbsListView.java:3463) 
10-25 03:04:51.024: E/AndroidRuntime(2227):  at android.os.Handler.handleCallback(Handler.java:730) 
10-25 03:04:51.024: E/AndroidRuntime(2227):  at android.os.Handler.dispatchMessage(Handler.java:92) 
10-25 03:04:51.024: E/AndroidRuntime(2227):  at android.os.Looper.loop(Looper.java:137) 
10-25 03:04:51.024: E/AndroidRuntime(2227):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
10-25 03:04:51.024: E/AndroidRuntime(2227):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-25 03:04:51.024: E/AndroidRuntime(2227):  at java.lang.reflect.Method.invoke(Method.java:525) 
10-25 03:04:51.024: E/AndroidRuntime(2227):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
10-25 03:04:51.024: E/AndroidRuntime(2227):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
10-25 03:04:51.024: E/AndroidRuntime(2227):  at dalvik.system.NativeStart.main(Native Method) 

Music.java Music.java 라인 133

package com.hadafsoft.smartaddis; 

//all the necessary imports are imported 

public class Music extends Activity { 

    private String SDCardPath = "/mnt/sdcard/"; 

    public static final int DIALOG_DOWNLOAD_PROGRESS = 0; 
    public ProgressDialog mProgressDialog, dialog; 
    public String songSender, tempSong, tempSinger; 
    public String SongServerPath, SongName; 

    private ListView lstView; 
    private ImageAdapter imageAdapter; 

    public int currentPage = 1; 
    public int TotalPage = 0; 

    public Button btnNext; 
    public Button btnPre; 

    ArrayList<HashMap<String, Object>> MyArrList = new ArrayList<HashMap<String, Object>>(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.music); 

     // Bets orientation 
     onConfigurationChanged(null); 

     // ListView and imageAdapter 

     lstView = (ListView) findViewById(R.id.listView1); 
     lstView.setClipToPadding(false); 

     imageAdapter = new ImageAdapter(getApplicationContext()); 
     lstView.setAdapter(imageAdapter); 

     // Next 
     btnNext = (Button) findViewById(R.id.btnNext); 
     // Perform action on click 
     btnNext.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       currentPage = currentPage + 1; 
       ShowData(); 
      } 
     }); 

     // Previous 
     btnPre = (Button) findViewById(R.id.btnPre); 
     // Perform action on click 
     btnPre.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       currentPage = currentPage - 1; 
       ShowData(); 
      } 
     }); 

     // Show first load 
     ShowData(); 

     // listView1 

     // OnClick 
     lstView.setOnItemClickListener(new OnItemClickListener() { 
      @SuppressLint("NewApi") 
      public void onItemClick(AdapterView<?> parent, View v, 
        int position, long id) { 

       // *** Check file exists in SDCard ***/ 
       SongServerPath = MyArrList.get(position).get("path").toString(); 
       String fileName = SongServerPath.substring(
         SongServerPath.lastIndexOf('/') + 1, 
         SongServerPath.length()); 
       String strPath = SDCardPath + fileName; // file in sdcard Eg :/mnt/sdcard/Mary.mp3          

       File file = new File(strPath); 

       SongName = MyArrList.get(position).get("song_en").toString(); 

       tempSong = MyArrList.get(position).get("song_en").toString(); 
       tempSinger = MyArrList.get(position).get("singer_en").toString(); 
       songSender = "D - "+tempSinger+" - "+tempSong; 

       /*** if file exists (View for Download) ***/ 
       if (file.exists()) { 
        ViewMusicFileSDCard(strPath, SongName); 
       } else { 
        showYesOrCancelDialog(); 
       } 

      } 
     }); 

     Log.i(TAG, "onCreate"); 

    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     Configuration c = getResources().getConfiguration(); 

     if (c.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      Toast.makeText(Music.this, "ይህ ገዕ ላንድስኬፕ አቀምመጥ የለውም", Toast.LENGTH_LONG).show(); 
     } 
    } 

    class LoadContentFromServer extends AsyncTask<Object, Integer, Object> { 

     @Override 
     protected Object doInBackground(Object... params) { 

      String url = "http://10.0.2.2/android/smartaddis/music/getMusic.php"; 

      JSONArray data; 
      try { 
       data = new JSONArray(getJSONUrl(url)); 

       MyArrList = new ArrayList<HashMap<String, Object>>(); 
       HashMap<String, Object> map; 

       int displayPerPage = 7; // Per Page 
       int TotalRows = data.length(); 
       int indexRowStart = ((displayPerPage * currentPage) - displayPerPage); 

       if (TotalRows <= displayPerPage) { 
        TotalPage = 1; 
       } else if ((TotalRows % displayPerPage) == 0) { 
        TotalPage = (TotalRows/displayPerPage); 
       } else { 
        TotalPage = (TotalRows/displayPerPage) + 1; 
        TotalPage = (int) TotalPage; 
       } 
       int indexRowEnd = displayPerPage * currentPage; 
       if (indexRowEnd > TotalRows) { 
        indexRowEnd = TotalRows; 
       } 

       for (int i = indexRowStart; i < indexRowEnd; i++) { 
        JSONObject c = data.getJSONObject(i); 
        map = new HashMap<String, Object>(); 
        map.put("id", (String) c.getString("id")); 
        map.put("singer_en", (String) c.getString("singer_en")); 
        map.put("song_en", (String) c.getString("song_en")); 

        map.put("length", (String) c.getString("length")); 
        map.put("releaseddate", (String) c.getString("releaseddate")); 

        // Thumbnail Get ImageBitmap To Object 
        map.put("imagepath", (String) c.getString("imagepath")); 
        Bitmap newBitmap = loadBitmap(c.getString("imagepath")); 
        map.put("ImagePathBitmap", newBitmap); 

        MyArrList.add(map); 

        publishProgress(i); 
       } 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     @Override 
     public void onProgressUpdate(Integer... progress) { 
      imageAdapter.notifyDataSetChanged(); 
     } 

     @Override 
     protected void onPostExecute(Object result) { 

      if (currentPage >= TotalPage) { 
       btnNext.setEnabled(false); 
      } else { 
       btnNext.setEnabled(true); 
      } 

      if (currentPage <= 1) { 
       btnPre.setEnabled(false); 
      } else { 
       btnPre.setEnabled(true); 
      } 

      if (dialog != null) 
       dialog.dismiss(); 
     } 
    } 

    class ImageAdapter extends BaseAdapter { 

     private Context mContext; 

     public ImageAdapter(Context context) { 
      mContext = context; 
     } 

     public int getCount() { 
      return MyArrList.size(); 
     } 

     public Object getItem(int position) { 
      return MyArrList.get(position); 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 

      LayoutInflater inflater = (LayoutInflater) mContext 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      if (convertView == null) { 
       convertView = inflater.inflate(R.layout.music_column, null); 
      } 

      // ImagePath 
      ImageView imageView = (ImageView) convertView 
        .findViewById(R.id.music_thumb); 
      imageView.getLayoutParams().height = 120; 
      imageView.getLayoutParams().width = 120; 
      imageView.setPadding(10, 10, 0, 10); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      try { 
       imageView.setImageBitmap((Bitmap) MyArrList.get(position).get(
         "ImagePathBitmap")); 
      } catch (Exception e) { 
       imageView 
         .setImageResource(android.R.drawable.ic_menu_report_image); 
      } 

      // ID, Song, Singer, Length and Released-Date 
      TextView txtSong = (TextView) convertView 
        .findViewById(R.id.song); 
      txtSong.setText(MyArrList.get(position).get("song_en").toString()); 
      txtSong.setPadding(10, 0, 0, 0); 

      TextView txtSinger = (TextView) convertView 
        .findViewById(R.id.singer); 
      txtSinger.setText(MyArrList.get(position).get("singer_en") 
        .toString()); 
      txtSinger.setPadding(10, 0, 0, 0); 

      TextView txtLength = (TextView) convertView 
        .findViewById(R.id.length); 
      txtLength.setText(MyArrList.get(position).get("length").toString()); 
      txtLength.setPadding(10, 0, 0, 0); 

      TextView txtDate = (TextView) convertView.findViewById(R.id.releaseddate); 
      txtDate.setText(MyArrList.get(position).get("releaseddate").toString()); 
      txtDate.setPadding(10, 0, 0, 0); 


      return convertView; 

     } 

    } 

    /*** Get JSON Code from URL ***/ 
    public String getJSONUrl(String url) { 
     StringBuilder str = new StringBuilder(); 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(url); 
     try { 
      HttpResponse response = client.execute(httpGet); 
      StatusLine statusLine = response.getStatusLine(); 
      int statusCode = statusLine.getStatusCode(); 
      if (statusCode == 200) { // Download OK 
       HttpEntity entity = response.getEntity(); 
       InputStream content = entity.getContent(); 
       BufferedReader reader = new BufferedReader(
         new InputStreamReader(content)); 
       String line; 
       while ((line = reader.readLine()) != null) { 
        str.append(line); 
       } 
      } else { 
       Log.e("Log", "Failed to download file.."); 
      } 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return str.toString(); 
    } 

    /***** Get Image Resource from URL (Start) *****/ 
    private static final String TAG = "Image"; 
    private static final int IO_BUFFER_SIZE = 4 * 1024; 

    public static Bitmap loadBitmap(String url) { 
     Bitmap bitmap = null; 
     InputStream in = null; 
     BufferedOutputStream out = null; 

     try { 
      in = new BufferedInputStream(new URL(url).openStream(), 
        IO_BUFFER_SIZE); 

      final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); 
      out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE); 
      copy(in, out); 
      out.flush(); 

      final byte[] data = dataStream.toByteArray(); 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      // options.inSampleSize = 1; 

      bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, 
        options); 
     } catch (IOException e) { 
      Log.e(TAG, "Could not load Bitmap from: " + url); 
     } finally { 
      closeStream(in); 
      closeStream(out); 
     } 

     return bitmap; 
    } 

    private static void closeStream(Closeable stream) { 
     if (stream != null) { 
      try { 
       stream.close(); 
      } catch (IOException e) { 
       android.util.Log.e(TAG, "Could not close stream", e); 
      } 
     } 
    } 

    private static void copy(InputStream in, OutputStream out) 
      throws IOException { 
     byte[] b = new byte[IO_BUFFER_SIZE]; 
     int read; 
     while ((read = in.read(b)) != -1) { 
      out.write(b, 0, read); 
     } 
    } 

    /***** Get Image Resource from URL (End) *****/ 

    @Override 
    public void finish() { 
     super.finish(); 
     overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     // TODO Auto-generated method stub 
     super.onSaveInstanceState(outState); 
     // webview.saveState(outState); 
     Log.i(TAG, "onSaveInstanceState"); 
    } 

    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onRestoreInstanceState(savedInstanceState); 
     // webview.restoreState(savedInstanceState); 
     // lstView.restoreState(savedInstanceState); 
     Log.i(TAG, "onRestoreInstanceState"); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.items, menu); 

     return super.onCreateOptionsMenu(menu); 
    } 

} 
+2

어떤 라인이 133입니까? – Josh

+0

com.hadafsoft.smartaddis.Music $ 3.onItemClick (Music.java:133) ** ** ** logcat **에서 ** java.lang.NullPointerException 행을 식별하고이 행을 두 번 클릭하면 오류 문과이 질문에 해당 진술을 강조 표시하십시오. – Ajeesh

+0

줄 133 SongServerPath = MyArrList.get (위치) .get ("경로"). toString(); – EthioDroid

답변

0

, 당신은 "클리핑"키 필드를 액세스한다.

SongServerPath = MyArrList.get(position).get("path").toString(); 

하지만이 키의 값을 설정 한 곳이 없습니다. "이미지 경로"를 설정했지만 "경로"는 설정하지 않았습니다.

의미가 있는지 확인하십시오.