我想在我的Android应用程序中放置2种不同的布局 以及使用共享首选项注册的值 这是我的代码..一旦我点击徽标就停止了
的一部分
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs;
//String register;
prefs = PreferenceManager.getDefaultSharedPreferences(this);
register=prefs.getString("register", "no");
if(register.equalsIgnoreCase("no")) {
setContentView(R.layout.main_activity);
}
else
{
setContentView(R.layout.about);
}
}
LogCat错误
FATAL EXCEPTION: main
Process:com,cpcs,irissystem, PID:31749
java.lang.RuntimeExeption : unable to start activity componentInfo
{......MainActivity }:java.lang.NullPointerException
at android.app.ActivityThread.preformLaunchActivity(ActivityThread.java2328)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java2386)
At android.app.ActivityThread.access$900(ActivityThread.java:169)
At android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
At android.os.Handler.dispatchMeesage(Handler.java:102)
At android.os.looper.loop(LooperLjava:136)
At android.app.ActivityThread.Main(ActivityThread.java:5476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$methodAndArgsCaller.run....:1268)
活动类
public class MainActivity extends Activity {
public static String register;
private static final int CAMERA_REQUEST = 1888;
public static String IrisCode;
String IrisCode2;
ImageView imageView;
private static Bitmap eyeImage;
EditText passwordText;
public static float perc ;
SharedPreferences prefs;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
register=prefs.getString("register", "no");
if(register.equalsIgnoreCase("no")) {
setContentView(R.layout.main_activity);
}
else
{
setContentView(R.layout.about);
}
Toast.makeText(MainActivity.this,"befroe: "+ register, Toast.LENGTH_LONG).show();
Button InstructionButton = (Button) findViewById(R.id.inst);
Button enrollButton = (Button) findViewById(R.id.enroll);
Button verifyButton = (Button) findViewById(R.id.ver);
passwordText = (EditText) findViewById(R.id.pass);
if(register.equalsIgnoreCase("no")){
verifyButton.setEnabled(false);
}
enrollButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//Go to the next page
//if Not registered before go to start to enter data
//else go to setting
Intent i = new Intent();
if(register.equalsIgnoreCase("no")) {
i = new Intent (MainActivity.this,start.class);
startActivity(i);
}
else
{
i = new Intent (MainActivity.this,setting.class);
startActivity(i);
}
}
});
InstructionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//Go to the instruction page
Intent i = new Intent (MainActivity.this,instruction.class);
startActivity(i);
}
});
verifyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if("0000".equals(passwordText.getText().toString())){
String same="aothorizes acess";
Toast.makeText(MainActivity.this, same, Toast.LENGTH_LONG).show();
Intent i = new Intent (MainActivity.this,setting.class);
startActivity(i);
}
}
});
}
清单
<application
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.cpcs.irissystem.MainActivity"
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=".start" />
<activity
android:name=".setting" />
<activity
android:name=".aboutus" />
<activity
android:name=".instruction" />
<activity
android:name=".setting2"/>
<activity
android:name=".ver"/>
</application>
答案 0 :(得分:0)
// Try this way,hope this will help you...
SharedPreferences prefs;
String register;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
register=prefs.getString("register", "no");
if(register.equalsIgnoreCase("no")) {
setContentView(R.layout.activity_main);
}
else
{
setContentView(R.layout.activity_main2);
}
}