2016-04-06 12 views
1

このコードは4.2以降で正常に動作していますが、4.4以上では動作しません。残念なことに、アプリケーションが動作を停止しました。理由はわかりません。私はアンドロイドの新しい人です。廃止されたメソッドのためではないですか?または何 ?music listviewが4.4以上で動作していませんか?

This is my Stacktrace

そして、次のように私のコードです:

package com.example.hamza; 

import android.app.Activity; 
import android.content.ContentResolver; 
import android.content.Context; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

public class MainActivity extends Activity{ 
    ListView lvfs; 
    Cursor cursorfm; 
    int count,middleperson; 
    TextView tv,title; 
    ContentResolver conres; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     initializesomestuff(); 
    } 

    private void initializesomestuff() { 
     // TODO Auto-generated method stub 
     lvfs=(ListView)findViewById(R.id.lview); 
     findViewById(R.id.tv1); 
     String[] projection={MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Audio.Media.ARTIST,}; 
     cursorfm=conres.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, null, null,null); 
     count=cursorfm.getCount(); 
     ////get some data 
     lvfs.setAdapter(new musiclistadapter(getApplicationContext())); 

    } 
    /////// cutomiztion walla area///start 
public class musiclistadapter extends BaseAdapter{ 


private Context mcontext; 
String songname,songartist; 
private LayoutInflater mLayoutInflater; 

    public musiclistadapter(Context applicationContext) { 
    // TODO Auto-generated constructor stub 
     mcontext=applicationContext; 
     //get the layout inflater 
     mLayoutInflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return count; 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public View getView(int position, View arg1, ViewGroup arg2) { 
     // TODO Auto-generated method stub 
     ViewHolder holder; 
     System.gc(); 
     tv = new TextView(mcontext.getApplicationContext()); 
     String id=null; 
     //if(arg1==null){ 
     holder = new ViewHolder(); 
     arg1 = mLayoutInflater.inflate(R.layout.textviewlayout, arg2, false); 
     // get all views you need to handle from the cell and save them in the view holder 
     holder.itemName = (TextView) arg1.findViewById(R.id.tv1); 
     // save the view holder on the cell view to get it back latter 

      middleperson=cursorfm.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME); 
      cursorfm.moveToPosition(position); 
      id=cursorfm.getString(middleperson); 
      middleperson=cursorfm.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST); 
      cursorfm.moveToPosition(position); 
      id=id+cursorfm.getString(middleperson); 
      arg1.setTag(holder); 
      holder.itemName.setText(id); 
      //middleperson=cursorfm.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST); 
      //id=cursorfm.getString(middleperson); 
      //id+="whatever it is"+id+cursorfm.getString(middleperson); 
      //LayoutInflater inflater = getLayoutInflater(); 
      //View row; 
      //row = inflater.inflate(R.layout.textviewlayout, arg2, false); 
      // TextView title; 
      // title = (TextView) row.findViewById(R.id.tv1); 
      // title.setText(id); 
      //tv.setText(id); 
      // return (row); 
    // } 
     //else{ 
      //title = (TextView) arg1; 
      // the getTag returns the viewHolder object set as a tag to the view 
      holder = (ViewHolder)arg1.getTag(); 
     //} 

     //if (id != null) { 
       //set the item name on the TextView 
       //holder.itemName.setText(cursorfm.getString(middleperson)); 
      // } else { 
       // make sure that when you have an if statement that alters the UI, you always have an else that sets a default value back, otherwise you will find that the recycled items will have the same UI changes 
      // holder.itemName.setText("Unknown"); 
      // } 

      //this method must return the view corresponding to the data at the specified position. 
      return arg1; 
     //return (title); 

    } 
} 
private class ViewHolder { 

    protected TextView itemName; 

} 
} 
+0

あなたのlogcatトレースはどこですか? –

+0

あなたは許可の拒否エラーを受けています。アンドロイド6.0(マシュマロ)のためにを追加してください。 – 1234567

答えて

0

私はあなたのLogtraceで見たようあなたからメディア・データを読み取るために、この権限を必要とするとして、あなたは、READ_EXTERNAL_STORAGEの許可を提供していませんでしたデバイス。だから、あなたはmanifest.xmlファイルを開き、applicationタグの前に次の権限を追加する必要があります。メディアの保管場所が異なっているので


そして、いくつかのデバイスで作業し、他では動作しない程度

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
は、それですさまざまなデバイスやアンドロイドのバージョンを超えているため、音楽メディアのデータは一部のデバイスの外部ストレージに保存されているため、許可が必要です。

関連する問題