传递值时出错 - android

时间:2014-02-28 19:33:09

标签: android

这是第一项活动:

public static final String level="";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pass);




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.pass, menu);
    return true;
}


 public void onClick(View view) {

        switch (view.getId()) {
        case R.id.button:
            Intent i = new Intent(this, MainActivity.class);
           i.putExtra("level","2");

        }

} }

这是第二项活动:

    SeekBar seek_bar;
    Button play_button, pause_button , reapet_button , reapet_button1;
    MediaPlayer player;
    TextView text_shown;
    String s;
    Handler seekHandler = new Handler();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = getIntent();
        if (null != intent) {
            s = intent.getStringExtra("level");
        }
        getInit();
        seekUpdation();
    }



    public void getInit() {
        seek_bar = (SeekBar) findViewById(R.id.seek_bar);
        play_button = (Button) findViewById(R.id.play_button);
        pause_button = (Button) findViewById(R.id.pause_button);
        text_shown = (TextView) findViewById(R.id.text_shown);
        reapet_button = (Button) findViewById(R.id.reapet_button);
        reapet_button1 = (Button) findViewById(R.id.reapet_button1);
        play_button.setOnClickListener(this);
        pause_button.setOnClickListener(this);
        reapet_button.setOnClickListener(this);
        reapet_button1.setOnClickListener(this);


        int resId = getResources().getIdentifier("sound"+s, "raw", "com.r");
        player = MediaPlayer.create(this, resId);


        seek_bar.setMax(player.getDuration());

    }

    Runnable run = new Runnable() {

        @Override
        public void run() {
            seekUpdation();
        }
    };

    public void seekUpdation() {

        seek_bar.setProgress(player.getCurrentPosition());
        seekHandler.postDelayed(run, 1000);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
        case R.id.play_button:
            text_shown.setText("Playing...");
            player.start();
            break;
        case R.id.reapet_button:
            player.setLooping(true);
                break;
        case R.id.reapet_button1:
            player.setLooping(false);
                break;    
        case R.id.pause_button:
            player.pause();
            text_shown.setText("Paused...");
            break;



        }

    }
}

当用户按下按钮时,我想将字符串的值传递给另一个活动,但是当我运行应用程序时,它对我不起作用。这是logcat:

02-28 21:37:54.979: D/AndroidRuntime(545): Shutting down VM
02-28 21:37:54.979: W/dalvikvm(545): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-28 21:37:54.999: E/AndroidRuntime(545): FATAL EXCEPTION: main
02-28 21:37:54.999: E/AndroidRuntime(545): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rawan/com.rawan.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
02-28 21:37:54.999: E/AndroidRuntime(545):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-28 21:37:54.999: E/AndroidRuntime(545):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-28 21:37:54.999: E/AndroidRuntime(545):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-28 21:37:54.999: E/AndroidRuntime(545):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-28 21:37:54.999: E/AndroidRuntime(545):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 21:37:54.999: E/AndroidRuntime(545):  at android.os.Looper.loop(Looper.java:123)
02-28 21:37:54.999: E/AndroidRuntime(545):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-28 21:37:54.999: E/AndroidRuntime(545):  at java.lang.reflect.Method.invokeNative(Native Method)
02-28 21:37:54.999: E/AndroidRuntime(545):  at java.lang.reflect.Method.invoke(Method.java:521)
02-28 21:37:54.999: E/AndroidRuntime(545):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-28 21:37:54.999: E/AndroidRuntime(545):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-28 21:37:54.999: E/AndroidRuntime(545):  at dalvik.system.NativeStart.main(Native Method)
02-28 21:37:54.999: E/AndroidRuntime(545): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
02-28 21:37:54.999: E/AndroidRuntime(545):  at android.content.res.Resources.getValue(Resources.java:892)
02-28 21:37:54.999: E/AndroidRuntime(545):  at android.content.res.Resources.openRawResourceFd(Resources.java:854)
02-28 21:37:54.999: E/AndroidRuntime(545):  at android.media.MediaPlayer.create(MediaPlayer.java:641)
02-28 21:37:54.999: E/AndroidRuntime(545):  at com.rawan.MainActivity.getInit(MainActivity.java:66)
02-28 21:37:54.999: E/AndroidRuntime(545):  at com.rawan.MainActivity.onCreate(MainActivity.java:41)
02-28 21:37:54.999: E/AndroidRuntime(545):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-28 21:37:54.999: E/AndroidRuntime(545):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-28 21:37:54.999: E/AndroidRuntime(545):  ... 11 more
02-28 22:23:31.238: W/ResourceType(603): No package identifier when getting value for resource number 0x00000000
02-28 22:23:31.238: D/AndroidRuntime(603): Shutting down VM
02-28 22:23:31.248: W/dalvikvm(603): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-28 22:23:31.269: E/AndroidRuntime(603): FATAL EXCEPTION: main
02-28 22:23:31.269: E/AndroidRuntime(603): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rawan/com.rawan.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
02-28 22:23:31.269: E/AndroidRuntime(603):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-28 22:23:31.269: E/AndroidRuntime(603):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-28 22:23:31.269: E/AndroidRuntime(603):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-28 22:23:31.269: E/AndroidRuntime(603):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-28 22:23:31.269: E/AndroidRuntime(603):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 22:23:31.269: E/AndroidRuntime(603):  at android.os.Looper.loop(Looper.java:123)
02-28 22:23:31.269: E/AndroidRuntime(603):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-28 22:23:31.269: E/AndroidRuntime(603):  at java.lang.reflect.Method.invokeNative(Native Method)
02-28 22:23:31.269: E/AndroidRuntime(603):  at java.lang.reflect.Method.invoke(Method.java:521)
02-28 22:23:31.269: E/AndroidRuntime(603):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-28 22:23:31.269: E/AndroidRuntime(603):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-28 22:23:31.269: E/AndroidRuntime(603):  at dalvik.system.NativeStart.main(Native Method)
02-28 22:23:31.269: E/AndroidRuntime(603): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
02-28 22:23:31.269: E/AndroidRuntime(603):  at android.content.res.Resources.getValue(Resources.java:892)
02-28 22:23:31.269: E/AndroidRuntime(603):  at android.content.res.Resources.openRawResourceFd(Resources.java:854)
02-28 22:23:31.269: E/AndroidRuntime(603):  at android.media.MediaPlayer.create(MediaPlayer.java:641)
02-28 22:23:31.269: E/AndroidRuntime(603):  at com.rawan.MainActivity.getInit(MainActivity.java:60)
02-28 22:23:31.269: E/AndroidRuntime(603):  at com.rawan.MainActivity.onCreate(MainActivity.java:39)
02-28 22:23:31.269: E/AndroidRuntime(603):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-28 22:23:31.269: E/AndroidRuntime(603):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-28 22:23:31.269: E/AndroidRuntime(603):  ... 11 more
02-28 22:27:18.398: W/ResourceType(632): No package identifier when getting value for resource number 0x00000000
02-28 22:27:18.408: D/AndroidRuntime(632): Shutting down VM
02-28 22:27:18.408: W/dalvikvm(632): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-28 22:27:18.428: E/AndroidRuntime(632): FATAL EXCEPTION: main
02-28 22:27:18.428: E/AndroidRuntime(632): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rawan/com.rawan.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
02-28 22:27:18.428: E/AndroidRuntime(632):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-28 22:27:18.428: E/AndroidRuntime(632):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-28 22:27:18.428: E/AndroidRuntime(632):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-28 22:27:18.428: E/AndroidRuntime(632):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-28 22:27:18.428: E/AndroidRuntime(632):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 22:27:18.428: E/AndroidRuntime(632):  at android.os.Looper.loop(Looper.java:123)
02-28 22:27:18.428: E/AndroidRuntime(632):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-28 22:27:18.428: E/AndroidRuntime(632):  at java.lang.reflect.Method.invokeNative(Native Method)
02-28 22:27:18.428: E/AndroidRuntime(632):  at java.lang.reflect.Method.invoke(Method.java:521)
02-28 22:27:18.428: E/AndroidRuntime(632):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-28 22:27:18.428: E/AndroidRuntime(632):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-28 22:27:18.428: E/AndroidRuntime(632):  at dalvik.system.NativeStart.main(Native Method)
02-28 22:27:18.428: E/AndroidRuntime(632): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
02-28 22:27:18.428: E/AndroidRuntime(632):  at android.content.res.Resources.getValue(Resources.java:892)
02-28 22:27:18.428: E/AndroidRuntime(632):  at android.content.res.Resources.openRawResourceFd(Resources.java:854)
02-28 22:27:18.428: E/AndroidRuntime(632):  at android.media.MediaPlayer.create(MediaPlayer.java:641)
02-28 22:27:18.428: E/AndroidRuntime(632):  at com.rawan.MainActivity.getInit(MainActivity.java:59)
02-28 22:27:18.428: E/AndroidRuntime(632):  at com.rawan.MainActivity.onCreate(MainActivity.java:38)
02-28 22:27:18.428: E/AndroidRuntime(632):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-28 22:27:18.428: E/AndroidRuntime(632):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-28 22:27:18.428: E/AndroidRuntime(632):  ... 11 more
02-28 22:32:18.473: I/Process(632): Sending signal. PID: 632 SIG: 9

3 个答案:

答案 0 :(得分:4)

我认为问题在于设置文本,

您是否曾尝试使用这些方法将文本转换为字符串,我不确定它是否能解决您的问题,但您应该尝试一下

   i.putExtra("level",""+2);

i.putExtra("level","2".toString());

如下所示开始活动后跟

startActivity(i);

答案 1 :(得分:1)

引起:android.content.res.Resources $ NotFoundException:资源ID#0x0

试试这个

int resId = getResources().getIdentifier("sound"+s, "raw", this.getPackageName());

答案 2 :(得分:0)

Java代码无法找到XML中指定的内容。