将参数从一个java类传递到另一个java的问题(android)

时间:2014-01-28 08:22:07

标签: android parameters nullpointerexception android-activity

您好我在使用intent将android中的参数从一个java类传递到另一个java。从主菜单到中文页面

的MainMenu:

package com.example.myfirstapp;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainMenu extends Activity {

    public MediaPlayer mpSplash;

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

        //final MediaPlayer mpSplash = MediaPlayer.create(this, R.raw.kalimba);
        //mpSplash.start();

        //set up button sound
        final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.button_sound);

        //WESTERN PAGE
        Button bWestern = (Button) findViewById(R.id.western);
        bWestern.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.example.myfirstapp.WESTERNPAGE"));
                mpButtonClick.start();
            }
        });

        //CHINESE PAGE
                Button bChinese = (Button) findViewById(R.id.chinese);
                bChinese.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent ("com.example.myfirstapp.CHINESEPAGE");
                        intent.putExtra("STRING_PASS", "this is a value passed from mainmenu class");
                        startActivity(intent);
                        mpButtonClick.start();
                    }
                });

        //JAPANESE PAGE
        Button bJapanese = (Button) findViewById(R.id.japanese);
        bJapanese.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.example.myfirstapp.JAPANESEPAGE"));
                mpButtonClick.start();
            }
        }); 

        //ORIENTAL PAGE
        Button bOriental = (Button) findViewById(R.id.oriental);
        bOriental.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.example.myfirstapp.ORIENTALPAGE"));
                mpButtonClick.start();
            }
        }); 

        //FAVOURITES PAGE
        Button bFavourites = (Button) findViewById(R.id.favourites);
        bFavourites.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.example.myfirstapp.FAVOURITESPAGE"));
                mpButtonClick.start();
            }
        }); 

        //OtherS PAGE
        Button bOthers = (Button) findViewById(R.id.others);
        bOthers.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.example.myfirstapp.OTHERSPAGE"));
                mpButtonClick.start();
            }
        }); 

    }

}

到中文页面:

package com.example.myfirstapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class ChinesePage extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        Intent intent = getIntent();
        String s = intent.getExtras().getString("STRING_PASS");
        TextView description = (TextView) findViewById(R.id.description);
        description.setText(s);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chinesepage);
    }

}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher_cook"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar">
        <activity
            android:name="com.example.myfirstapp.SplashScreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.myfirstapp.MainMenu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.myfirstapp.CLEARSCREEN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.myfirstapp.WesternPage"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.myfirstapp.WESTERNPAGE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.myfirstapp.ChinesePage"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.myfirstapp.CHINESEPAGE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.myfirstapp.JapanesePage"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.myfirstapp.JAPANESEPAGE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.myfirstapp.OrientalPage"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.myfirstapp.ORIENTALPAGE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.myfirstapp.FavouritesPage"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.myfirstapp.FAVOURITESPAGE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.myfirstapp.OthersPage"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.myfirstapp.OTHERSPAGE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>


    </application>

</manifest>

我的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView 
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"/>

</LinearLayout>

我很遗憾。当我点击中文页面按钮时,应用程序已停止。

我在这里缺少一些东西:\

提前致谢

1 个答案:

答案 0 :(得分:0)

您需要先将布局内容设置为活动,然后初始化视图。

我猜你有NUllPointerException。还要确保在清单中声明了ChinesePage活动。

Intent intent = new Intent(MainMenu.this,Chinsepage.class) 
intent.putExtra("STRING_PASS", "this is a value passed from mainmenu class");
startActivity(intent);
// similarly for others

并在清单中

<activity
        android:name=".ChinesePage"
        android:label="@string/app_name">
</activity>          
// similar for other activities  

并阅读有关显式和隐式意图@

的内容

http://developer.android.com/guide/components/intents-filters.html

所以在ChinesePage更改如下

super.onCreate(savedInstanceState);
setContentView(R.layout.chinesepage); // first
TextView description = (TextView) findViewById(R.id.description); 
Intent intent = getIntent();
String s = intent.getExtras().getString("STRING_PASS");
description.setText(s);