2013-11-26 2 views
0

그래서 내 앱에 변경 내역을 포함하려고합니다. 이 코드를 내 활동의 onCreate 메소드에 넣으면 모든 것이 잘 작동하지만 원하는 것은 아닙니다. 사용자가 환경 설정 클래스에서 "changelog"환경 설정을 클릭하면 작동하도록하고 싶습니다. 여기 생성자가 정의되지 않았습니다.

내가

//Launch change log dialog 
    ChangeLogDialog _ChangelogDialog = new ChangeLogDialog(this); 
    _ChangelogDialog.show(); 

을 사용하고있는 코드입니다하지만 클릭에 내 취향에 있음을 넣을 때이 오류

을 얻을 "생성자 ChangeLogDialog (새 Preference.OnPreferenceClickListener는() {})입니다 "정의되지 않은 다음

import java.io.IOException; 

import org.xmlpull.v1.XmlPullParser; 
import org.xmlpull.v1.XmlPullParserException; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.content.pm.PackageInfo; 
import android.content.pm.PackageManager.NameNotFoundException; 
import android.content.res.Resources; 
import android.content.res.XmlResourceParser; 
import android.util.Log; 
import android.webkit.WebView; 
import android.widget.Toast; 

/* 
* Class to show a changelog dialog 
* (c) 2012 Martin van Zuilekom (http://martin.cubeactive.com) 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
* 
*/ 

public class ChangeLogDialog { 
    static final private String TAG = "ChangeLogDialog"; 

    static final private String TITLE_CHANGELOG = "title_changelog"; 
    static final private String CHANGELOG_XML = "changelog"; 

    private Activity fActivity; 

    public ChangeLogDialog(Activity context) { 
     fActivity = context; 
    } 

    //Get the current app version 
    private String GetAppVersion(){ 
     try { 
      PackageInfo _info = fActivity.getPackageManager().getPackageInfo(fActivity.getPackageName(), 0); 
      return _info.versionName; 
     } catch (NameNotFoundException e) { 
      e.printStackTrace(); 
      return ""; 
     } 
    } 

    //Parse a the release tag and return html code 
    private String ParseReleaseTag(XmlResourceParser aXml) throws XmlPullParserException, IOException { 
     String _Result = "<h1>Release: " + aXml.getAttributeValue(null, "version") + "</h1><ul>"; 
     int eventType = aXml.getEventType(); 
     while ((eventType != XmlPullParser.END_TAG) || (aXml.getName().equals("change"))) { 
      if ((eventType == XmlPullParser.START_TAG) &&(aXml.getName().equals("change"))){ 
       eventType = aXml.next(); 
       _Result = _Result + "<li>" + aXml.getText() + "</li>"; 
      } 
      eventType = aXml.next(); 
     }  
     _Result = _Result + "</ul>"; 
     return _Result; 
    } 

    //CSS style for the html 
    private String GetStyle() { 
     return 
       "<style type=\"text/css\">" 
       + "h1 { margin-left: 0px; font-size: 12pt; }" 
       + "li { margin-left: 0px; font-size: 9pt;}" 
       + "ul { padding-left: 30px;}" 
       + "</style>"; 
    } 

    //Get the changelog in html code, this will be shown in the dialog's webview 
    private String GetHTMLChangelog(int aResourceId, Resources aResource) { 
     String _Result = "<html><head>" + GetStyle() + "</head><body>"; 
     XmlResourceParser _xml = aResource.getXml(aResourceId); 
     try 
     { 
      int eventType = _xml.getEventType(); 
      while (eventType != XmlPullParser.END_DOCUMENT) { 
       if ((eventType == XmlPullParser.START_TAG) &&(_xml.getName().equals("release"))){ 
        _Result = _Result + ParseReleaseTag(_xml); 

       } 
       eventType = _xml.next(); 
      } 
     } 
     catch (XmlPullParserException e) 
     { 
      Log.e(TAG, e.getMessage(), e); 
     } 
     catch (IOException e) 
     { 
      Log.e(TAG, e.getMessage(), e); 

     }   
     finally 
     {   
      _xml.close(); 
     }  
     _Result = _Result + "</body></html>"; 
     return _Result; 
    } 

    //Call to show the changelog dialog 
    public void show() { 
     //Get resources 
     String _PackageName = fActivity.getPackageName(); 
     Resources _Resource; 
     try { 
      _Resource = fActivity.getPackageManager().getResourcesForApplication(_PackageName); 
     } catch (NameNotFoundException e) { 
      e.printStackTrace(); 
      return; 
     } 

     //Get dialog title    
     int _resID = _Resource.getIdentifier(TITLE_CHANGELOG , "string", _PackageName); 
     String _Title = _Resource.getString(_resID); 
     _Title = _Title + " v" + GetAppVersion(); 

     //Get Changelog xml resource id 
     _resID = _Resource.getIdentifier(CHANGELOG_XML, "xml", _PackageName); 
     //Create html change log 
     String _HTML = GetHTMLChangelog(_resID, _Resource); 

     //Get button strings 
     String _Close = _Resource.getString(R.string.changelog_close); 

     //Check for empty changelog 
     if (_HTML.equals("") == true) 
     { 
      //Could not load change log, message user and exit void 
      Toast.makeText(fActivity, "Could not load change log", Toast.LENGTH_SHORT).show(); 
      return; 
     } 

     //Create webview and load html 
     WebView _WebView = new WebView(fActivity); 
     _WebView.loadData(_HTML, "text/html", "utf-8"); 
     AlertDialog.Builder builder = new AlertDialog.Builder(fActivity) 
       .setTitle(_Title) 
       .setView(_WebView) 
       .setPositiveButton(_Close, new Dialog.OnClickListener() { 
        public void onClick(DialogInterface dialogInterface, int i) { 
         dialogInterface.dismiss(); 
        } 
       }); 
     builder.create().show(); 
    } 

} 
+3

다음과 같아야합니다 :'ChangeLogDialog _ChangelogDialog = new ChangeLogDialog (YourActivity.isis); ' –

+0

'OnPreferenceClickListener' 객체를 받아들이는'ChangeLogDialog' 클래스에 생성자가 없기 때문에 거기에서 클릭을 구현할 수 없습니다 – JoelFernandes

+0

믿을 수 없네요. 감사! @ZouZou 만약 당신이 대답을 투표를 할 수 있도록 당신이 대답을 할 수있는 그 정답은 좋을 것입니다. – Fernando

답변

3

그것은 SHO 변경 로그 클래스를의 수 ULD : 귀하의 경우 this

ChangeLogDialog _ChangelogDialog = new ChangeLogDialog(YourActivity.this); 

은 익명의 내부 클래스 OnPreferenceClickListener이 아닌 활동의 인스턴스를 나타냅니다.

관련 문제