我是 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();
}
}
小说.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"
from转移novel.java
到cordovaExample.java
but in novel.java
,运行应用程序后我进入"unfortunately app has stopped"
模拟器。
这是日志猫
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;
}
}
我该如何解决这个问题?