gpt4 book ai didi

java - 在Android中将数据从一个Java文件传递到另一个

转载 作者:行者123 更新时间:2023-11-30 11:42:07 25 4
gpt4 key购买 nike

我是 Android 开发的新手。我的项目中有两个 Java 文件。一个文件包含我的主要 Activity ,因此我想将数据从一个 Java 文件传输到另一个包含主要 Activity 的文件。

cordovaExample.java:

package org.apache.cordova.example;

import android.content.Context;
import android.content.Intent;
import android.telephony.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import org.apache.cordova.*;
public class cordovaExample extends DroidGap
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");

Bundle extras = getIntent().getExtras();
String phno = extras.getString("novel.PhoneNumber");
Toast.makeText(getApplicationContext(),phno,Toast.LENGTH_LONG).show();



}
}

小说.java

package org.apache.cordova.example;

import android.content.Context;
import android.content.Intent;
import android.telephony.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class novel extends Activity
{
public String PhoneNumber;
Context mcontext;
public String getNumber()
{



TelephonyManager tMgr=(TelephonyManager)mcontext.getSystemService(Context.TELEPHONY_SERVICE);

Intent intent = new Intent(this, cordovaExample.class);
intent.putExtra("novel.PhoneNumber", PhoneNumber);
startActivity(intent);
return PhoneNumber = tMgr.getLine1Number();
}

}

所以我想将 "PhoneNumber"novel.java 转移到 cordovaExample.java 但在 novel.java,运行应用程序后,我在模拟器中收到“不幸的是应用程序已停止”

这是日志

08-12 15:49:37.657: E/AndroidRuntime(779): FATAL EXCEPTION: main
08-12 15:49:37.657: E/AndroidRuntime(779): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.apache.cordova.example/org.apache.cordova.example.cordovaExample}: java.lang.NullPointerException
08-12 15:49:37.657: E/AndroidRuntime(779): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-12 15:49:37.657: E/AndroidRuntime(779): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-12 15:49:37.657: E/AndroidRuntime(779): at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-12 15:49:37.657: E/AndroidRuntime(779): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-12 15:49:37.657: E/AndroidRuntime(779): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 15:49:37.657: E/AndroidRuntime(779): at android.os.Looper.loop(Looper.java:137)
08-12 15:49:37.657: E/AndroidRuntime(779): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-12 15:49:37.657: E/AndroidRuntime(779): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 15:49:37.657: E/AndroidRuntime(779): at java.lang.reflect.Method.invoke(Method.java:511)
08-12 15:49:37.657: E/AndroidRuntime(779): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-12 15:49:37.657: E/AndroidRuntime(779): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-12 15:49:37.657: E/AndroidRuntime(779): at dalvik.system.NativeStart.main(Native Method)
08-12 15:49:37.657: E/AndroidRuntime(779): Caused by: java.lang.NullPointerException
08-12 15:49:37.657: E/AndroidRuntime(779): at org.apache.cordova.example.cordovaExample.onCreate(cordovaExample.java:20)
08-12 15:49:37.657: E/AndroidRuntime(779): at android.app.Activity.performCreate(Activity.java:4465)
08-12 15:49:37.657: E/AndroidRuntime(779): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-12 15:49:37.657: E/AndroidRuntime(779): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-12 15:49:37.657: E/AndroidRuntime(779): ... 11 more

此外,如果我尝试简单的程序,我会得到相同的结果:

cordovaExample.java

package org.apache.cordova.example;

import android.content.Context;
import android.content.Intent;
import android.telephony.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import org.apache.cordova.*;
public class cordovaExample extends DroidGap
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");

novel c=new novel()
int a=c.getNumber();
Toast.makeText(getApplicationContext(),a,Toast.LENGTH_LONG).show();



}
}

小说.java

package org.apache.cordova.example;

import android.content.Context;
import android.content.Intent;
import android.telephony.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class novel extends Activity
{
public int a=0;
public int getNumber()
{

a=5;

return a;
}

}

我该如何解决?

最佳答案

你在 return block 之后使用了 intent 语句

返回电话号码 = tMgr.getLine1Number();

Intent myIntent = new Intent(novel.this, cordovaExample.class);

在返回 block 之后,任何代码都显示无法访问的错误。

传入intent参数后即可调用return。

   TelephonyManager tMgr=(TelephonyManager)mcontext.getSystemService(Context.TELEPHONY_SERVICE); 

Intent intent = new Intent(this, cordovaExample.class);
intent.putExtra("novel.PhoneNumber", PhoneNumber);
startActivity(intent);
return PhoneNumber = tMgr.getLine1Number();

关于java - 在Android中将数据从一个Java文件传递到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11921082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com