将参数从一个java类传递到另一个java类(android)时出现问题
嗨,我在android中使用intent将参数从一个java类传递到另一个java类时遇到了麻烦。从主菜单到中文页面 主菜单:将参数从一个java类传递到另一个java类(android)时出现问题,android,parameters,nullpointerexception,android-activity,Android,Parameters,Nullpointerexception,Android Activity,嗨,我在android中使用intent将参数从一个java类传递到另一个java类时遇到了麻烦。从主菜单到中文页面 主菜单: 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
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>
不幸的是,我得到了一个“不幸”。当我点击中文页面按钮时,应用程序已停止
这里有没有我遗漏的东西:\
提前感谢您需要先将版面内容设置为“活动”,然后初始化视图 我猜您有一个
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
是的,你丢失了你的事故记录。请提供他们。。。
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);