我是Android开发的新手。我的项目中有两个Java文件。一个文件包含我的主要活动,因此我想将数据从一个Java文件传输到另一个包含主要活动的文件。
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();
}
}
novel.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
中,运行应用程序后,我在模拟器中获得"unfortunately app has stopped"
。< / p>
这是logcat
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();
}
}
novel.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;
}
}
我该如何解决这个问题?
答案 0 :(得分:1)
您在返回块后使用了intent语句
return PhoneNumber = tMgr.getLine1Number();
Intent myIntent = new Intent(novel.this,cordovaExample.class);
返回阻止后,任何代码都显示无法访问的错误。
传递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();
答案 1 :(得分:0)
Android API指南包含有关how to pass data between between Activities/Services within a single application的最终常见问题解答项目。
试试这个,
在小说中这样设置电话号码
Intent myIntent = new Intent(novel.this, cordovaExample.class);
myIntent.putExtra("phone", 989898989);
startActivity(myIntent);
在cordovaExample文件中执行以下操作以获取phoneNumber
Bundle b = this.getIntent().getExtras();
String i = b.getString("phone");
如果您无法传递数据,则替代方法是声明公共方法并传递它们,请参阅以下示例,
novel.java
private static String phoneNumber = "";
public void onCreate()
{
...
phoneNumber = "9898989898";
...
}
public static String getPhoneNumber()
{
return phoneNumber;
}
cordovaExample.java
private static String phoneNumber = "";
phoneNumber = novel.getPhoneNumber();
答案 2 :(得分:0)
将您的代码更改为:您在cordovaExample.java
中获得了NPE,因为在novel
中您将null
电话号码放入了额外内容。
TelephonyManager tMgr=(TelephonyManager)mcontext.getSystemService(Context.TELEPHONY_SERVICE);
Intent intent = new Intent(this, cordovaExample.class);
PhoneNumber = tMgr.getLine1Number(); // Check PhoneNumber not null here before send to cordovaExample
if (PhoneNumber !=null ){
intent.putExtra("novel.PhoneNumber", PhoneNumber);
startActivity(intent);}
}