简体   繁体   English

如何修复NullPointerException? (Android SDK,Eclipse)

[英]How to fix NullPointerException? (Android SDK, Eclipse)

I feel like this is a simple problem that I'm somehow missing but I can't for the life of me figure out what's going wrong with this code. 我觉得这是一个简单的问题,我不知何故不见了,但我无法终生弄清楚这段代码出了什么问题。 Every time this class loads I get a NullPointerException 每次加载此类时,我都会收到NullPointerException

11-19 17:40:35.967: E/AndroidRuntime(2144): FATAL EXCEPTION: main
11-19 17:40:35.967: E/AndroidRuntime(2144): Process: john.brian.adlibs, PID: 2144
11-19 17:40:35.967: E/AndroidRuntime(2144): java.lang.RuntimeException: Unable to start            activity ComponentInfo{john.brian.adlibs/john.brian.adlibs.StoryDisplay}:         java.lang.NullPointerException
11-19 17:40:35.967: E/AndroidRuntime(2144):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
11-19 17:40:35.967: E/AndroidRuntime(2144):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
11-19 17:40:35.967: E/AndroidRuntime(2144):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
11-19 17:40:35.967: E/AndroidRuntime(2144):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
11-19 17:40:35.967: E/AndroidRuntime(2144):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-19 17:40:35.967: E/AndroidRuntime(2144):     at android.os.Looper.loop(Looper.java:137)
11-19 17:40:35.967: E/AndroidRuntime(2144):     at    android.app.ActivityThread.main(ActivityThread.java:4998)
11-19 17:40:35.967: E/AndroidRuntime(2144):     at java.lang.reflect.Method.invokeNative(Native Method)
11-19 17:40:35.967: E/AndroidRuntime(2144):     at java.lang.reflect.Method.invoke(Method.java:515)
11-19 17:40:35.967: E/AndroidRuntime(2144):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-19 17:40:35.967: E/AndroidRuntime(2144):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-19 17:40:35.967: E/AndroidRuntime(2144):     at dalvik.system.NativeStart.main(Native Method)
11-19 17:40:35.967: E/AndroidRuntime(2144): Caused by: java.lang.NullPointerException
11-19 17:40:35.967: E/AndroidRuntime(2144):     at john.brian.adlibs.StoryDisplay.onCreate(StoryDisplay.java:25)
11-19 17:40:35.967: E/AndroidRuntime(2144):     at android.app.Activity.performCreate(Activity.java:5243)
11-19 17:40:35.967: E/AndroidRuntime(2144):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-19 17:40:35.967: E/AndroidRuntime(2144):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
11-19 17:40:35.967: E/AndroidRuntime(2144):     ... 11 more

and, after heavy Googling, I'm pretty sure everything is defined. 并且,经过大量的Google搜索,我很确定所有内容都已定义。

public class StoryDisplay extends Activity{ 

TextView storyView;
Button sdBackToStoryList;
ScrollView scrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.story_display);
    setVariables(); 
    String story = getIntent().getExtras().getString("storyFinal");    
    storyView.setText(story);
    sdBackToStoryList.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent viewChooseStory = new Intent(StoryDisplay.this, ChooseStory.class);
            startActivity(viewChooseStory);

        }
    });
    }

private void setVariables() {
    // TODO Auto-generated method stub
    sdBackToStoryList = (Button) findViewById(R.id.bStoryDisplayBackToStoryList);
    storyView = (TextView) findViewById(R.id.tvStoryDisplayStory);
    scrollView = (ScrollView) findViewById(R.id.svStoryDisplay);
}

} }

And the XML code: 和XML代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_height="match_parent"
android:orientation="vertical"

android:padding="0dp" >
 <com.google.ads.AdView  android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     ads:adUnitId="ca-app-pub-3337976893531833/3006951709"
                     ads:adSize="BANNER"
                     ads:testDevices="TEST_EMULATOR, emulator-5554"
                     ads:loadAdOnCreate="true"
                   />
 <LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="10"
android:padding="20dp" >

<ScrollView 
android:layout_width="fill_parent" 
android:id="@+id/svStoryDisplay"   
android:layout_height="350dp">
<TextView
    android:text="@string/app_name"
    android:id="@+id/tvStoryDisplayStory"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left" />
</ScrollView>

    <Button
    android:id="@+id/bStoryDisplayBackToStoryList"
    android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:layout_height="50dp"
    android:textSize="20sp"
    android:text="@string/backToStoryList"
     />

The only thing I can think of is the string that the storyView is set to, which is defined with the same button that starts the StoryDisplay in another class: 我唯一能想到的是将StoryView设置为的字符串,该字符串由在另一个类中启动StoryDisplay的相同按钮定义:

String storyFinal =  new String("String goes here");

            Intent teleport = new Intent(SadLibs.this, StoryDisplay.class);
            teleport.putExtra("storyFinal",storyFinal);
            startActivity(teleport);        

            Intent viewChooseStory = new Intent(SadLibs.this, StoryDisplay.class);
            startActivity(viewChooseStory);

The error appears to be in the line: 错误似乎在该行中:

storyView.setText(story);

Again, I'm sure this is an incredibly simple issue I'm just missing and I feel incredibly stupid for having this issue but any help would be greatly appreciated. 再次,我确定这是我所缺少的一个非常简单的问题,我对此问题感到非常愚蠢,但是任何帮助将不胜感激。 Thank you! 谢谢!

In the code below 在下面的代码中

Intent teleport = new Intent(SadLibs.this, StoryDisplay.class);
teleport.putExtra("storyFinal",storyFinal);
startActivity(teleport);        

Intent viewChooseStory = new Intent(SadLibs.this, StoryDisplay.class);
startActivity(viewChooseStory);

You add extra only once. 您只能添加一次。 If you don't add storyFinal extra, you will get NPE in the line 如果您不添加storyFinal ,您将获得NPE

String story = getIntent().getExtras().getString("storyFinal");
String story = getIntent().getExtras().getString("storyFinal");

You need to check to see if the Intent or the Extras is NULL. 您需要检查Intent或Extras是否为NULL。

if (getIntent() != NULL) {
    if (getIntent().getExtras() != NULL) {
        story = getIntent().getExtras().getString("storyFinal");
    }
}

Check like this : 像这样检查:

  if(storyView!=null)
  {
     storyView.setText(story);
  }

Just implement 只是实施

sdBackToStoryList = (Button) findViewById(R.id.bStoryDisplayBackToStoryList);
storyView = (TextView) findViewById(R.id.tvStoryDisplayStory);
scrollView = (ScrollView) findViewById(R.id.svStoryDisplay); 

in onCreate(). 在onCreate()中。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM