将变量数据发送到其他活动android [英] Send variable data to other activity android

查看:79
本文介绍了将变量数据发送到其他活动android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测验应用。测验完成后,将显示结果活动。它包含一个得分变量。必须将分数发送到分数活动,其中显示最后一个测验的分数。一切正常但是当点击主活动上的分数按钮时会发生错误。



这是点击分数按钮时的logcat:

  09   -   21   13  19  52  680   4413  -4413 / app.mobiledevicesecurity E / AndroidRuntime:FATAL EXCEPTION:main 
流程:app.mobiledevicesecurity,PID: 4413
java.lang.RuntimeException:无法启动活动ComponentInfo {app.mobiledevicesecurity / app.mobiledevicesecurity.Scores}:java.lang.NullPointerException:尝试调用虚方法' java.lang.String android.content.Context.getPackageName()'对空对象引用
在android.app.Activi tyThread.performLaunchActivity(ActivityThread.java: 2325
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2390
在android.app.ActivityThread.access $ 800(ActivityThread.java: 151
在android.app。 ActivityThread $ H.handleMessage(ActivityThread.java: 1303
在android.os.Handler.dispatchMessage(Handler.java: 102
在android.os.Looper.loop(Looper.java: 135
在android.app .ActivityThread.main(ActivityThread.java: 5257
at java.lang.reflect.Method.invoke(Native Method)
at java。 lang.reflect.Method.invoke(Method.java: 372
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller .run(ZygoteInit.java: 903
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 698
引起:java.lang.NullPointerException:尝试调用虚方法' java.lang.String android.content.Context.getPackageName()'对空对象引用
在android.content.ContextWrapper.getPackageName(ContextWrapper.java: 132
在android.content.ComponentName。< init>(ComponentName.java: 77
在android.content.Intent。< init>(Intent.java: 4160
at app.mobiledevicesecurity.Result.getScore(Result.java:< span class =code-digit> 33 )
at app.mobiledevicesecurity.Scores.onCreate(Scores.java: 17 )$ b在andro的$ b id.app.Activity.performCreate(Activity.java: 5990
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1106
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2278
在android .app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2390
在android.app.ActivityThread.access $ 800(ActivityThread.java: 151
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java: 1303
在android.os.Handler.dispatchMessage(Handler.java: 102
在android.os.Looper.loop(Looper.java: 135
在android.app.ActivityThread.main(ActivityThread.java: 5257 )
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java: 372
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java: 903
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 698





MainActivity.java

  package  app.mobiledevicesecurity; 

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
DatabaseHelper myDb;
private static 按钮readbtn;
private static 按钮quizbtn;
private static 按钮得分btn;
private static 按钮settingsbtn;
private static 按钮helpbtn;
@ Override

protected void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper( this );
myDb.insertData();

OnClickReadButtonListener();
OnClickQuizButtonListener();
OnClickScoresButtonListener();
OnClickSettingsButtonListener();
OnClickHelpButtonListener();


}

public void OnClickReadButtonListener(){
readbtn =(Button)findViewById(R.id.readbutton);
readbtn.setOnClickListener(
new View.OnClickListener(){
@ Override
public void onClick(查看v){
意图意图= new 意图( app.mobiledevicesecurity.Read_Category);
startActivity(intent);
}
}
);
}

public void OnClickQuizButtonListener(){
quizbtn =(Button)findViewById(R.id.quizbutton);
quizbtn.setOnClickListener(
new View.OnClickListener(){
@ Override
public void onClick(查看v){
意图意图= new 意图( app.mobiledevicesecurity.Quiz);
startActivity(intent);
}
}
);
}

public void OnClickScoresButtonListener(){
scoresbtn =(Button)findViewById(R.id.scoresbutton);
Bundle extras = getIntent()。getExtras();
scoresbtn.setOnClickListener(
new View.OnClickListener(){
@ Override
public void onClick(查看v){
意图意图= new 意图( app.mobiledevicesecurity.Scores);
startActivity(intent);
}
}
);

}

public void OnClickSettingsButtonListener() {
settingsbtn =(Button)findViewById(R.id.settingsbutton);
settingsbtn.setOnClickListener(
new View.OnClickListener(){
@ Override
public void onClick(查看v){
意图意图= new 意图( app.mobiledevicesecurity.Settings);
startActivity(intent);
}
}
);
}

public void OnClickHelpButtonListener(){
helpbtn =(Button)findViewById(R.id.helpbutton);
helpbtn.setOnClickListener(
new View.OnClickListener(){
@ Override
public void onClick(查看v){
意图意图= new 意图( app.mobiledevicesecurity.Help);
startActivity(intent);
}
}
);


}

@覆盖
public boolean onCreateOptionsMenu(菜单菜单){
// 给菜单充气;如果项目存在,则会将项目添加到操作栏。
getMenuInflater()。inflate(R.menu.menu_main,menu);
返回 true;
}

@覆盖
public boolean onOptionsItemSelected(MenuItem item){
// Handle操作栏项目点击此处。操作栏将
// 自动处理Home / Up按钮上的点击,
// 在AndroidManifest.xml中指定父活动。
int id = item.getItemId();

// noinspection SimplifiableIfStatement
if (id == R.id.action_settings){
return true;
}

return super .onOptionsItemSelected(item);
}
}



Result.java

  package  app.mobiledevicesecurity; 

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

public class 结果 extends 活动{

私人 静态按钮播放;
private static 按钮menubutton;
int 得分;

@ Override
protected void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
OnClickPlayButtonListener();
OnClickMenuButtonListener();
TextView textResult =(TextView)findViewById(R.id.textResult);
Bundle b = getIntent()。getExtras();
得分= b.getInt( 得分);
textResult.setText( 你得分 + +得分+ for测验。);


}

public void getScore()
{

Intent intent2 = new 意图(结果。 this
分数。 class );
Bundle bun = new Bundle();
bun.putInt( 得分,得分);
intent2.putExtras(bun);
startActivity(intent2);
完成();
}
public void OnClickPlayButtonListener(){
playbtn =(按钮)findViewById(R.id.btn);
playbtn.setOnClickListener(
new View.OnClickListener(){
@ Override
public void onClick(查看v){
意图意图= new 意图( app.mobiledevicesecurity.Quiz);
startActivity(intent);
}
}
);
}

public void OnClickMenuButtonListener(){
menubutton =(Button)findViewById(R.id.menubtn);
menubutton.setOnClickListener(
new View.OnClickListener(){
@ Override
public void onClick(查看v){
意图意图= new Intent(getApplicationContext(),MainActivity。 class );
startActivity(intent);
}
}
);
}
}



Scores.java

  package  app.mobiledevicesecurity; 

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.content.Intent;

public class 分数 extends ActionBarActivity {

@ Override
protected < span class =code-keyword> void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_scores);
结果res = new Result();
res.getScore();
TextView txtScore1 =(TextView)findViewById(R.id.txtScore1);
txtScore1.setText( 最后的测验得分: + + res.score + );
}

@覆盖
public boolean onCreateOptionsMenu(菜单菜单){
// 充气菜单;这会将项目添加到操作栏(如果存在)。
getMenuInflater()。inflate(R.menu.menu_scores,menu);
返回 true;
}

@覆盖
public boolean onOptionsItemSelected(MenuItem item){
// Handle操作栏项目点击此处。操作栏将
// 自动处理Home / Up按钮上的点击,
// 在AndroidManifest.xml中指定父活动。
int id = item.getItemId();

// noinspection SimplifiableIfStatement
if (id == R.id.action_settings){
return true;
}
return super .onOptionsItemSelected(item);
}
}

解决方案

800(ActivityThread.java: 151
在android.app.ActivityThread


H.handleMessage(ActivityThread.java: 1303
在android.os.Handler.dispatchMessage(Handler.java: 102
在android.os.Looper.loop(Looper.java) : 135
在android.app.ActivityThread.main(ActivityThread.java: 5257
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java: 372
at com.android.internal.os.ZygoteInit


MethodAndArgsCaller.run(ZygoteInit.java: 903 )com.android.internal.os.ZygoteInit.main(ZygoteInit.j)
ava: 698
引起:java.lang.NullPointerException:尝试调用虚方法' java.lang.String android.content.Context.getPackageName()'在空对象上引用
在android.content.ContextWrapper.getPackageName(ContextWrapper。 java: 132
在android.content.ComponentName。< init>(ComponentName.java: 77
在android.content.Intent。< init>(Intent.java: 4160
at app.mobiledevicesecurity.Result .getScore(Result.java: 33
at app.mobiledevicesecurity.Scores.onCreate(Scores.java: 17
在android.app.Activity.performCreate(Activity.java: 5990
在android.app.Instrumentation。 callActivityOnCreate(Instrumentation.java: 1106
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2278
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2390
在android.app.ActivityThread.access

I have a quiz app. When the quiz is completed the result activity appears. It contains a score variable. The score must be sent to the scores activity where the score for the last quiz is shown. It all works fine but when clicking on the scores button on the main activity an error occur.

Here is the logcat when clicking on the Scores button:

09-21 13:19:52.680    4413-4413/app.mobiledevicesecurity E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: app.mobiledevicesecurity, PID: 4413
    java.lang.RuntimeException: Unable to start activity ComponentInfo{app.mobiledevicesecurity/app.mobiledevicesecurity.Scores}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
            at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132)
            at android.content.ComponentName.<init>(ComponentName.java:77)
            at android.content.Intent.<init>(Intent.java:4160)
            at app.mobiledevicesecurity.Result.getScore(Result.java:33)
            at app.mobiledevicesecurity.Scores.onCreate(Scores.java:17)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)



MainActivity.java

package app.mobiledevicesecurity;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
DatabaseHelper myDb;
private static Button readbtn;
private static Button quizbtn;
private static Button scoresbtn;
private static Button settingsbtn;
private static Button helpbtn;
@Override

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myDb = new DatabaseHelper(this);
    myDb.insertData();

     OnClickReadButtonListener();
     OnClickQuizButtonListener();
     OnClickScoresButtonListener();
     OnClickSettingsButtonListener();
     OnClickHelpButtonListener();


}

public void OnClickReadButtonListener() {
    readbtn = (Button) findViewById(R.id.readbutton);
    readbtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("app.mobiledevicesecurity.Read_Category");
                    startActivity(intent);
                }
            }
    );
}

public void OnClickQuizButtonListener() {
    quizbtn = (Button) findViewById(R.id.quizbutton);
    quizbtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                    startActivity(intent);
                }
            }
    );
}

public void OnClickScoresButtonListener() {
    scoresbtn = (Button) findViewById(R.id.scoresbutton);
    Bundle extras = getIntent().getExtras();
    scoresbtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("app.mobiledevicesecurity.Scores");
                    startActivity(intent);
                }
            }
    );

}

public void OnClickSettingsButtonListener() {
    settingsbtn = (Button) findViewById(R.id.settingsbutton);
    settingsbtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("app.mobiledevicesecurity.Settings");
                    startActivity(intent);
                }
            }
    );
}

public void OnClickHelpButtonListener() {
    helpbtn = (Button) findViewById(R.id.helpbutton);
    helpbtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("app.mobiledevicesecurity.Help");
                    startActivity(intent);
                }
            }
    );


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}


Result.java

package app.mobiledevicesecurity;

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

public class Result extends Activity {

private static Button playbtn;
private static Button menubutton;
int score;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
    OnClickPlayButtonListener();
    OnClickMenuButtonListener();
    TextView textResult = (TextView) findViewById(R.id.textResult);
    Bundle b = getIntent().getExtras();
    score = b.getInt("score");
    textResult.setText("You scored" + " " + score + " for the quiz.");


}

public void getScore()
{

    Intent intent2 = new Intent(Result.this,
            Scores.class);
    Bundle bun = new Bundle();
    bun.putInt("score", score);
    intent2.putExtras(bun);
    startActivity(intent2);
    finish();
}
public void OnClickPlayButtonListener() {
    playbtn = (Button) findViewById(R.id.btn);
    playbtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                    startActivity(intent);
                }
            }
    );
}

public void OnClickMenuButtonListener() {
    menubutton = (Button) findViewById(R.id.menubtn);
    menubutton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(intent);
                }
            }
    );
}
}


Scores.java

 package app.mobiledevicesecurity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.content.Intent;

public class Scores extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scores);
        Result res = new Result();
        res.getScore();
        TextView txtScore1 = (TextView) findViewById(R.id.txtScore1);
        txtScore1.setText("Last quiz score:" + " " + res.score + ".");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_scores, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    } 

解决方案

800(ActivityThread.java:151) at android.app.ActivityThread


H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5257) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit


MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132) at android.content.ComponentName.<init>(ComponentName.java:77) at android.content.Intent.<init>(Intent.java:4160) at app.mobiledevicesecurity.Result.getScore(Result.java:33) at app.mobiledevicesecurity.Scores.onCreate(Scores.java:17) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) at android.app.ActivityThread.access


这篇关于将变量数据发送到其他活动android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆