我正在尝试将值从1个活动传递到另一个,该值来自intent方法..但是在将值传递给第二个intent时我得到一个空指针异常..所以我做错了什么? ?在main方法中(url在那里有一个值)
public void passStringToIntent(String url) {
final Intent intent = new Intent(Question_Point_Main.this, AccessPdf.class);
intent.putExtra("URL", url);
startActivity(intent);
}
第二种方法中的(访问值)
Intent intent = getIntent();
String url = intent.getStringExtra("URL");
我的日志猫如下
E/AndroidRuntime(1892): FATAL EXCEPTION: main
E/AndroidRuntime(1892): Process: com.example.testqstn, PID: 1892
E/AndroidRuntime(1892): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testqstn/com.questionpoint.pdf.AccessPdf}: java.lang.NullPointerException
E/AndroidRuntime(1892): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
E/AndroidRuntime(1892): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
E/AndroidRuntime(1892): at android.app.ActivityThread.access$800(ActivityThread.java:135)
E/AndroidRuntime(1892): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
E/AndroidRuntime(1892): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(1892): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(1892): at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime(1892): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1892): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(1892): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime(1892): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/AndroidRuntime(1892): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(1892): Caused by: java.lang.NullPointerException
E/AndroidRuntime(1892): at com.questionpoint.pdf.AccessPdf.<init>(AccessPdf.java:22)
E/AndroidRuntime(1892): at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(1892): at java.lang.Class.newInstance(Class.java:1208)
E/AndroidRuntime(1892): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
E/AndroidRuntime(1892): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
E/AndroidRuntime(1892): ... 11 more
访问pdf .java / 2nd活动
public class AccessPdf extends Activity {
Intent intent = getIntent();
String url = intent.getStringExtra("URL"); <--- line 22
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), url);
try
{
in = assetManager.open(url);
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{
Log.e("tag", e.getMessage());
}
Intent intent = new Intent(this, Pdf.class);
intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, getFilesDir()+"/"+url);
startActivity(intent);
/*
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/ABC.pdf"),
"application/pdf");
startActivity(intent);*/
}
private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}
答案 0 :(得分:7)
此
Intent intent = getIntent();
String url = intent.getStringExtra("URL");<--- line 22
应该进入onCreate
。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = getIntent().getStringExtra("URL");
...// rest of the code