2011-12-02 133 views
0

我有一個關於我的應用程序中的類來顯示一些按鈕,當用戶點擊按鈕時,它跳轉到一個webview活動來查看一些網頁,所以我在about類中定義了urls,然後設置點擊數方法把它們投放到同一網頁視圖活動打開網頁,但我一直運行此活動,我的logcat打印什麼,但需要幫助調試Android中的NullPointerException

9月12日至二日時得到一個奇怪的NullPointerException異常:03:11.815:WARN/ActivityManager(51):活動空閒超時 for HistoryRecord {44d68ea0 com.appkon.hdtvs/.About} 12-02 09:03:17.026:WARN/ActivityManager(51):活動銷燬時間爲 HistoryRecord {44 e37518 com.appkon.hdtvs/.HDtvs}

這裏是我的code.Any幫助將不勝感激,謝謝

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(); 

       } 
       }); 
      } 



} 
} 
+0

如果你的logcat中沒有打印出'NullPointerException',你怎麼知道? – Caner

+0

這是在Variebles軌道 – oratis

+1

我會仔細檢查佈局,如果我是你。您在不同的佈局文件中使用相同的名稱來查看不同類型的視圖:在About中,按鈕是'Button',但在AboutFrame中它們是'ImageButton'。以這種方式獲取運行時很容易,特別是空指針。 –

回答

0

該錯誤意味着您的活動超時。

我不明白你爲什麼叫:

aboutframe.setWebViewClient 

多次。你可以嘗試用if else's更換if's

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

那麼,我在about.java中得到了這個異常,而不是aboutframe.java,我嘗試了你的方式,但它沒有解決。有任何想法嗎?謝謝 – oratis

0

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

之間所有的代碼你最好使用&&代替&。後者是「二進制」,你想使用「邏輯和」。