在Android中调试NullPointerException需要帮助

时间:2011-12-02 09:10:26

标签: android debugging pointers

我在我的应用程序中有一个类来显示一些按钮,当用户点击按钮时,它会跳转到webview活动以查看某个网页,因此我在about类中定义了url,然后设置onlick方法将它们传递给用于打开网页的相同webview活动,但是在运行此活动时我不断得到一个好奇的NullPointerException,而我的logcat只打印

  

12-02 09:03:11.815:WARN / ActivityManager(51):活动空闲超时   for HistoryRecord {44d68ea0 com.appkon.hdtvs / .About} 12-02   09:03:17.026:WARN / ActivityManager(51):活动销毁超时   HistoryRecord {44e37518 com.appkon.hdtvs / .HDtvs}

这是我的代码。非常感谢,谢谢

About.java

public class About extends Activity{

    private Button backbutton;
    private Button likebutton;
    private ImageView versionlogo;
    private ImageButton faq;
    private ImageButton forum;
    private ImageButton feedback;
    private ImageButton rate;
    private String likepath ="http://appkon.com/hdtvs/share.html";
    private String likename = "分享";
    private String lpath="" ;
    private String lname="" ;
    private String faqpath ="http://appkon.com/hdtvs/faq.html";
    private String faqname ="常见问题";
    private String forumpath= "http://appkon.com/forum/" ;
    private String forumname = "APP论坛";
    private String fqpath="";
    private String fqname="";
    private String frpath="";
    private String frname="";
    private String ratepath ="http://appkon.com/hdtvs/";
    private String ratename="评价";
    private String rpath=""; 
    private String rname="";
    private String feedbackpath ="http://appkon.com/hdtvs/feedback.html";
    private String feedbackname="反馈问题";
    private String fdname="";
    private String fdpath="";

    @Override    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        backbutton=(Button) findViewById(R.id.back);
        likebutton=(Button) findViewById(R.id.share);
        faq =(ImageButton)findViewById(R.id.faqbutton);
        forum =(ImageButton)findViewById(R.id.forumbutton);
        feedback =(ImageButton)findViewById(R.id.feedbackbutton);
        rate =(ImageButton)findViewById(R.id.ratebutton);

        backbutton.setOnClickListener(new View.OnClickListener()
        {
          public void onClick(View v)
          {
            Intent intent = new Intent();
            intent.setClass(About.this, HDtvs.class);
            startActivity(intent);
            About.this.finish();

          }
        });

        likebutton.setOnClickListener(new View.OnClickListener()
        {
          public void onClick(View v)
          {
            Intent intent = new Intent();
            intent.setClass(About.this, Renrenframe.class);
            startActivity(intent);
            About.this.finish();

          }
        });

        faq.setOnClickListener(new View.OnClickListener()
        {
          public void onClick(View v)
          {
            Intent intent = new Intent();
            intent.setClass(About.this, Aboutframe.class);
            Bundle bundle = new Bundle();
            bundle.putString("fqpath",faqpath);
            bundle.putString("fqname",faqname);
            intent.putExtras(bundle);
            startActivity(intent);
            About.this.finish();

          }
        });

        feedback.setOnClickListener(new View.OnClickListener()
        {
          public void onClick(View v)
          {
            Intent intent = new Intent();
            intent.setClass(About.this, Aboutframe.class);
            Bundle bundle = new Bundle();
            bundle.putString("fdpath",feedbackpath);
            bundle.putString("fdname",feedbackname);
            intent.putExtras(bundle);
            startActivity(intent);
            About.this.finish();

          }
        });

        rate.setOnClickListener(new View.OnClickListener()
        {
          public void onClick(View v)
          {
            Intent intent = new Intent();
            intent.setClass(About.this, Aboutframe.class);
            Bundle bundle = new Bundle();
            bundle.putString("rpath",ratepath);
            bundle.putString("rname",ratename);
            intent.putExtras(bundle);
            startActivity(intent);
            About.this.finish();

          }
        });

        forum.setOnClickListener(new View.OnClickListener()
        {
          public void onClick(View v)
          {
            Intent intent = new Intent();
            intent.setClass(About.this, Aboutframe.class);
            Bundle bundle = new Bundle();
            bundle.putString("frpath",forumpath);
            bundle.putString("frname",forumname);
            intent.putExtras(bundle);
            startActivity(intent);
            About.this.finish();

          }
        });

        }
}

Aboutframe.java

public class Aboutframe extends Activity{

      private TextView namebar;
      private ImageButton likebutton;
      private ImageButton backbutton;
      private WebView aboutframe;
      private String lpath ;
      private String lname ;
      private String fqpath;
      private String fqname;
      private String frpath;
      private String frname;
      private String rpath; 
      private String rname;
      private String fdname;
      private String fdpath;


      @Override    
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.aboutframe);
          this.requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar

          backbutton=(ImageButton) findViewById(R.id.back);
          likebutton=(ImageButton) findViewById(R.id.share);
          aboutframe =(WebView)findViewById(R.id.aboutframe);

          Intent intent=this.getIntent();
          Bundle bunde = intent.getExtras();

          lname = bunde.getString("lname");
          lpath = bunde.getString("lpath");
          fqname = bunde.getString("fqname");
          fqpath = bunde.getString("fqpath");
          frname = bunde.getString("frname");
          frpath = bunde.getString("frpath");
          rname = bunde.getString("rname");
          rpath = bunde.getString("rpath");
          fdname = bunde.getString("fdname");
          fdpath = bunde.getString("fdpath");

          if(lname != null&lpath!= null){
              namebar.setText(lname);
              aboutframe.setWebViewClient(new WebViewClient(){  
                  public boolean shouldOverrideUrlLoading(WebView view, String url) {  
                      view.loadUrl(lpath);  
                      return true; 
                  }
                  });  
          }
          if(fqname != null&fqpath!= null){
              namebar.setText(fqname);
              aboutframe.setWebViewClient(new WebViewClient(){  
                  public boolean shouldOverrideUrlLoading(WebView view, String url) {  
                      view.loadUrl(fqpath);  
                      return true; 
                  }
                  });  
          }
          if(frname != null&frpath!= null){
              namebar.setText(frname);
              aboutframe.setWebViewClient(new WebViewClient(){  
                  public boolean shouldOverrideUrlLoading(WebView view, String url) {  
                      view.loadUrl(frpath);  
                      return true; 
                  }
                  });  
          }
          if(rname != null&rpath!= null){
              namebar.setText(rname);
              aboutframe.setWebViewClient(new WebViewClient(){  
                  public boolean shouldOverrideUrlLoading(WebView view, String url) {  
                      view.loadUrl(rpath);  
                      return true; 
                  }
                  });  
          }
          if(fdname != null&fdpath!= null){
              namebar.setText(fdname);
              aboutframe.setWebViewClient(new WebViewClient(){  
                  public boolean shouldOverrideUrlLoading(WebView view, String url) {  
                      view.loadUrl(fdpath);  
                      return true; 
                  }
                  });  

              backbutton.setOnClickListener(new View.OnClickListener()
              {
                public void onClick(View v)
                {
                  Intent intent = new Intent();
                  intent.setClass(Aboutframe.this, About.class);
                  startActivity(intent);
                  Aboutframe.this.finish();

                }
              });

              likebutton.setOnClickListener(new View.OnClickListener()
              {
                public void onClick(View v)
                {
                  Intent intent = new Intent();
                  intent.setClass(Aboutframe.this, Renrenframe.class);
                  startActivity(intent);
                  Aboutframe.this.finish();

                }
              });
          }



}
}

3 个答案:

答案 0 :(得分:0)

该错误意味着您的活动超时。

我不明白你打电话的原因:

aboutframe.setWebViewClient

多次。您可以尝试将if's替换为if else's

/*...*/
else if(frname != null&frpath!= null){
/*...*/
else if(rname != null&rpath!= null){
/*...*/

答案 1 :(得分:0)

将所有代码放在

之间
try
{
your Code here...
}
catch(NullPointerException e)
{
e.printStackTrace();
}

答案 2 :(得分:0)

您最好使用&&代替&。后者是“二进制”,你想使用“逻辑和”。