-4

I want to move on MainActivity to other Activity(torkyActivity).. MainActivity has a tabHost and one of tabspce is torkyActivity...

when I run app, it is erorrs. this is my class code:(torkyActivity)

  public class torkyActivity extends Activity {
   int []ahang=getResources().getIntArray(R.array.ahang_torky);
   String [] name={"ahang1","ahang2","ahang3","ahang4","ahang5","ahang6","ahang7","ahang8","ahang9"
           ,"ahang10","ahang11","ahang12","ahang13"};
   String baseDir=Environment.getExternalStorageDirectory().getAbsolutePath();
    String fileName="file";
    SeekBar seekBar;
    boolean okPlay=false;
    MediaPlayer mp;
    File barname=new File(Environment.getExternalStorageDirectory(),fileName);


    @Override


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);
         ListView list=(ListView) findViewById(R.id.listView1);
        Myadapter adapter=new Myadapter(getApplicationContext());
         seekBar=(SeekBar)findViewById(R.id.seekBar1);
        list.setAdapter(adapter);

    }

    public class Myadapter extends BaseAdapter implements Runnable
    {
        LayoutInflater myInflater;
        public Myadapter(Context context) {
            myInflater=LayoutInflater.from(context);
            // TODO Auto-generated constructor stub
        }



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

        @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(final int position, View convertView, ViewGroup parent) {
            ViewHolder Holder;

            if(convertView==null)
            {
                Holder=new ViewHolder();
                convertView=myInflater.inflate(R.layout.item_layout, null);
                Holder.name=(TextView)convertView.findViewById(R.id.text_name);
                Holder.play=(ImageView)convertView.findViewById(R.id.Imag_play);
                Holder.send=(ImageView)convertView.findViewById(R.id.imag_send);
                Holder.ring=(ImageView)convertView.findViewById(R.id.imag_ring);
                Holder.name.setText(name[position]);
                copy(ahang[position],name[position]);


                //*************bakhshe play kardan


                Holder.play.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub


//****************in ghesmat moshkel dare**************************************



    //*****************************************************************************s
                if(okPlay==true)
                    {
                    mp.stop();
                    play(ahang [position]);
                    }
                else 
                    play(ahang[position]);




                }


            });
               Holder.send.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                //  blutooth(name[position]);





                }
            });
              Holder.ring.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    ringTone(name [position]);
                    Toast.makeText(getBaseContext(), "set shod", Toast.LENGTH_LONG).show();


                }
            });
               convertView.setTag(Holder);




            }
            else
                Holder=(ViewHolder) convertView.getTag();



            // TODO Auto-generated method stub
            return convertView;
        }

  public void run() {
            int currentPosition= 0;
            int total = mp.getDuration();
            while (mp!=null && currentPosition<total) {
                try {
                    Thread.sleep(1000);
                    currentPosition= mp.getCurrentPosition();
                } catch (InterruptedException e) {
                    return;
                } catch (Exception e) {
                    return;
                }            
                seekBar.setProgress(currentPosition);
            }
        }
  protected void play(int ahang) {

      okPlay=true;


            mp=MediaPlayer.create(getApplicationContext(), ahang);
            mp.start();                     
            seekBar.setProgress(0);
            seekBar.setMax(mp.getDuration());
           new Thread(this).start();




            // TODO Auto-generated method stub

        }
}   




    @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            mp.stop();
        }





    //************************************************************  
    public void copy(int ahang,String name) {

      barname.mkdir();

        File trakc=new File(Environment.getExternalStorageDirectory(),fileName + "/"+ name+".mp3");

    InputStream f1=getResources().openRawResource(ahang);
    FileOutputStream f2; 
    try {
        f2=new FileOutputStream(trakc);
        byte[] buffer=new byte[1024*500];
        int i;
        i=f1.read(buffer);
        while (i !=-1) {
        f2.write(buffer);
        i=f1.read(buffer);

        }
        f2.flush();
        f2.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}
public void blutooth(String name)
{
     Intent intent = new Intent();
     intent.setAction(Intent.ACTION_SEND);
     intent.setType("audio/*");
     File trakc=new File(Environment.getExternalStorageDirectory(),fileName + "/"+ name+".mp3");
     intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(trakc));
     startActivity(intent);
}
public void ringTone(String name)
{
    File trakc=new File(Environment.getExternalStorageDirectory(),fileName + "/"+ name+".mp3");
    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, trakc .getAbsolutePath());
    values.put(MediaStore.MediaColumns.TITLE, "Gitar");
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
    values.put(MediaStore.Audio.Media.ARTIST, "ahmadi");
   // values.put(MediaStore.MediaColumns.SIZE, 215454);
    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
    values.put(MediaStore.Audio.Media.IS_ALARM, false);
    values.put(MediaStore.Audio.Media.IS_MUSIC, false);



    Uri uri = MediaStore.Audio.Media.getContentUriForPath(trakc
            .getAbsolutePath());
    getContentResolver().delete(
            uri,
            MediaStore.MediaColumns.DATA + "=\""
                    + trakc .getAbsolutePath() + "\"", null);
    Uri newUri = getContentResolver().insert(uri, values);

    RingtoneManager.setActualDefaultRingtoneUri(
            torkyActivity.this, RingtoneManager.TYPE_NOTIFICATION,
            newUri);

}
public class ViewHolder extends Activity
{
    TextView name;
    ImageView play;
    ImageView send;
    ImageView ring;
}
}

and my logCat is:

09-08 19:34:03.550: D/AndroidRuntime(13917): Shutting down VM
  09-08 19:34:03.550: W/dalvikvm(13917): threadid=1: thread exiting with uncaught exception (group=0x40aa8210)
 09-08 19:34:03.550: E/AndroidRuntime(13917): FATAL EXCEPTION: main
 09-08 19:34:03.550: E/AndroidRuntime(13917): java.lang.RuntimeException: Unable to   start activity   ComponentInfo{com.example.barname_asli/com.example.barname_asli.MainActivity}: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.barname_asli/com.example.barname_asli.torkyActivity}: java.lang.NullPointerException
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at android.app.ActivityThread.access$600(ActivityThread.java:127)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at android.os.Handler.dispatchMessage(Handler.java:99)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at android.os.Looper.loop(Looper.java:137)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at android.app.ActivityThread.main(ActivityThread.java:4448)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at java.lang.reflect.Method.invokeNative(Native Method)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at java.lang.reflect.Method.invoke(Method.java:511)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at dalvik.system.NativeStart.main(Native Method)
 09-08 19:34:03.550: E/AndroidRuntime(13917): Caused by: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.barname_asli/com.example.barname_asli.torkyActivity}: java.lang.NullPointerException
  09-08 19:34:03.550: E/AndroidRuntime(13917):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
   09-08 19:34:03.550: E/AndroidRuntime(13917):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1808)
  09-08 19:34:03.550: E/AndroidRuntime(13917):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
  09-08 19:34:03.550: E/AndroidRuntime(13917):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
  09-08 19:34:03.550: E/AndroidRuntime(13917):  at  android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:737)
  09-08 19:34:03.550: E/AndroidRuntime(13917):  at android.widget.TabHost.setCurrentTab(TabHost.java:401)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at android.widget.TabHost.addTab(TabHost.java:240)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at com.example.barname_asli.MainActivity.onCreate(MainActivity.java:48)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at android.app.Activity.performCreate(Activity.java:4465)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
  09-08 19:34:03.550: E/AndroidRuntime(13917):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
  09-08 19:34:03.550: E/AndroidRuntime(13917):  ... 11 more
  09-08 19:34:03.550: E/AndroidRuntime(13917): Caused by: java.lang.NullPointerException
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at com.example.barname_asli.torkyActivity.<init>(torkyActivity.java:33)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at java.lang.Class.newInstanceImpl(Native Method)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at java.lang.Class.newInstance(Class.java:1319)
09-08 19:34:03.550: E/AndroidRuntime(13917):    at     android.app.Instrumentation.newActivity(Instrumentation.java:1023)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
 09-08 19:34:03.550: E/AndroidRuntime(13917):   ... 21 more

I'm confused . I do not know what the problem is please help me.thanks

anjeli
  • 11
  • 6

1 Answers1

0

It may be a problem of your Intent.

Please add your torkyActivity in your AndroidManifest.xml like this

<activity android:name="com.example.barname_asli.torkyActivity"/>

When you want to make a new activity or make an Intent to a new activity, you should register it in your AndroidManifest.xml like above.

And as commented by ρяσѕρєя, Move ahang=getResources().getIntArray(R.array.ahang_torky); inside onCreate of Activity.

Also you may see this error when you try to execute code outside of a method like trying to do this:

File barname=new File(Environment.getExternalStorageDirectory(),fileName);
public void copy(int ahang,String name) {

  barname.mkdir();
  ........
}

so rather do like this :

public void copy(int ahang,String name) {
  File barname=new File(Environment.getExternalStorageDirectory(),fileName);
  barname.mkdir();
  ........
}

More Info

Also i think you have never used baseDir anywhere in your activity.Then why this line

String baseDir=Environment.getExternalStorageDirectory().getAbsolutePath();

And Just for a quick note, you should never make references to your views before onCreate.Make them inside onCreate only.

If trying above solutions didn't work for you refer java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

Community
  • 1
  • 1
Sash_KP
  • 5,551
  • 2
  • 25
  • 34
  • The actual problem is caused by what ρяσѕρєя K has intimated in the comment on the original question. You can't call `getResources()` in the main body of an `Activity`, i.e., before it has been properly created. That's only the start of the OP's problems though - the rest of the code is broken in so many different places. – Squonk Sep 06 '14 at 16:27
  • You're right.Edited my answer.Added this line _And as commented by ρяσѕρєя, Move ahang=getResources().getIntArray(R.array.ahang_torky); inside onCreate of Activity._ – Sash_KP Sep 06 '14 at 16:38