我正在开发一个简单的移动应用程序,用户登录并注销。当用户登录时,他可以访问多个页面(活动)。 以下是我的mainactivity.java:
package org.hos.view;
import java.io.Serializable;
import org.hos.application.LocalDatabaseConnection;
import org.hos.application.LoggedInUser;
import org.hos.controller.LogInController;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnTouchListener, OnClickListener, Serializable
{
SQLiteDatabase db;
LogInController lc=new LogInController();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
View layout=findViewById(R.id.layout);
layout.setOnTouchListener(this);
Button login = (Button) findViewById(R.id.login);
login.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
InputMethodManager input = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
return false;
}
@SuppressWarnings("static-access")
@Override
public void onClick(View v)
{
EditText id=(EditText) findViewById(R.id.editText1);
EditText password=(EditText) findViewById(R.id.editText2);
String name = lc.login(id.getText().toString(), password.getText().toString(), this);
//String name = conn.getUsername(id.getText().toString(), password.getText().toString());
if(name!=null)
{
LoggedInUser user=new LoggedInUser();
user.setUserId(name);
user.setPrivilege(1);
Intent intent = new Intent(this,LoggedInActivity.class);
intent.putExtra("LoggedInUser",user);
startActivity(intent);
overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
}
else
Toast.makeText(this, "Login invalid", Toast.LENGTH_SHORT).show();
}
}
正如您可以看到onclick方法,具有loggedinuser对象的intent被传递给第二个activity。这个目标在下一个活动中收到。这是我的loggedinactivity.java:
package org.hos.view;
import org.hos.application.LoggedInUser;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class LoggedInActivity extends ActionBarActivity implements OnClickListener {
Button logout;
LoggedInUser user;
@SuppressWarnings("static-access")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logged_in);
user = (LoggedInUser) getIntent().getExtras().getSerializable("LoggedInUser");
setTitle("Hello "+user.getUserId());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.logged_in, menu);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
logout=(Button) findViewById(R.id.logout);
logout.setOnClickListener(this);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
else if(id == R.id.view_graph)
{
Intent intent = new Intent(this,ViewGraphActivity.class);
intent.putExtra("LoggedInUser", user);
startActivity(intent);
overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
}
return super.onOptionsItemSelected(item);
}
public void onClick(View v)
{
if(v.equals(logout))
{
Intent intent=new Intent(this,MainActivity.class);
intent.putExtra("LoggedInUSer", user);
startActivity(intent);
overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
}
}
}
因此,此活动再次连接了第三个活动,再次发送登录的用户对象。收到的方式是一样的。继承人我的观点graph.java:
package org.hos.view;
import org.hos.application.LoggedInUser;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class ViewGraphActivity extends ActionBarActivity
{
LoggedInUser user;
@SuppressWarnings("static-access")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_graph);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
user = (LoggedInUser) getIntent().getExtras().getSerializable("LoggedInUser");
setTitle("Hello "+user.getUserId());
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.view_graph, menu);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
else if(id == android.R.id.home)
{
Intent intent=new Intent(this,LoggedInActivity.class);
intent.putExtra("LoggedInUser", user);
overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
}
return super.onOptionsItemSelected(item);
}
}
我的问题是第一次用户登录时,loggedinactivity.java会保存对象并传递给viewgraphactivity.java类。但是当从viewgraphactivity返回到loggedinactivity时,我得到以下异常:
11-07 13:53:39.258:E / AndroidRuntime(6165):致命异常:主要 11-07 13:53:39.258:E / AndroidRuntime(6165):java.lang.RuntimeException:无法启动>活动ComponentInfo {org.hos.view / org.hos.view.LoggedInActivity}:> java.lang .NullPointerException
为什么不能再使用viewgraphactivity发送的对象来记录活动?
谁能告诉我为什么会遇到这个问题?如果有任何解决方案。 在此先感谢。
答案 0 :(得分:0)
The only way to pass Custom object though intent is making it Serializable. Check the below link for more deatils
Person mPerson = new Person();
mPerson.setName("Leon");
mPerson.setAge(25);
Intent mIntent = new Intent(this,ObjectTranDemo1.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable(SER_KEY,mPerson);
mIntent.putExtras(mBundle);
More details : http://www.easyinfogeek.com/2014/01/android-tutorial-two-methods-of-passing.html