在 TextView 中显示 App 版本
Posted
技术标签:
【中文标题】在 TextView 中显示 App 版本【英文标题】:Displaying the App version in a TextView 【发布时间】:2015-06-23 01:27:51 【问题描述】:我想在 TextView 中显示我当前的应用程序版本,Textview 所在的活动不是我的 MainACtivity。 我使用了以下代码:
public class informatie extends ActionBarActivity
public String versionName;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_informatie);
try
versionName = getApplicationContext().getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), 0).versionName;
catch (PackageManager.NameNotFoundException e)
e.printStackTrace();
Typeface impact = Typeface.createFromAsset(getAssets(), "fonts/Impact.ttf");
TextView versieVak = (TextView) findViewById(R.id.versieView);
versieVak.setText(versionName);
Toast.makeText(getApplicationContext(), "KLIK", Toast.LENGTH_LONG).show();
versieVak.setTypeface(impact);
versieVak.setTextColor(getResources().getColor(R.color.antraciet));
versieVak.setTextSize(10, TypedValue.COMPLEX_UNIT_SP);
Layout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context="com.KnapperDev.knapper.jrw.informatie">
<ImageView
android:layout_
android:layout_
android:id="@+id/imageView"
android:src="@drawable/overeenkomst"
android:layout_marginBottom="249dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_
android:layout_
android:id="@+id/versieView"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="155dp" />
</RelativeLayout>
但是当我在手机上打开活动时,TextView 中没有任何内容。
我做了一个 Toast 来检查是否使用了 versieVak.setText(versionName);
。
它只是不显示任何内容。知道我做错了什么吗?
【问题讨论】:
如果您使用的是 Gradle,则可以使用BuildConfig.VERSION_NAME
大大简化此操作。
Toast 确实火了?
@Samurai 是的
如果您在布局中为该电视设置文本,您会看到吗?如果是这样,请在您的代码中尝试不同的 textColor 和 textSize。
@Samurai 即使我删除了代码的所有样式部分,它仍然没有显示,所以这应该不是问题。
【参考方案1】:
我尝试使用您的代码。它工作得很好。只需删除与字体相关的代码并简单地尝试即可。检查它是否正常工作。
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String versionName = "";
try
versionName = getApplicationContext().getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), 0).versionName;
catch (PackageManager.NameNotFoundException e)
e.printStackTrace();
TextView versionname = (TextView) findViewById(R.id.textName);
versionname.setText(versionName);
还要确保您已为您的文本视图分配了唯一的 ID。 如果可能,您能分享一下您的布局吗?
【讨论】:
对我不起作用。我刚刚得到: 致命异常:主进程:com.KnapperDev.knapper.jrw,PID:18174 java.lang.RuntimeException:无法启动活动 ComponentInfocom.KnapperDev.knapper.jrw/com.KnapperDev.knapper.jrw.informatie : java.lang.NullPointerException @MalumAtire832,您说文本视图所在的活动不是您的主要活动。 TextView 所在的活动,你在显示吗?因为,除非 android 调用此活动的 onCreate,否则您的 TextView 所在的位置,此代码不会被执行。【参考方案2】:试试这个:
// Import the BuildConfig at the top of the file
import yourpackagename.BuildConfig
...
// in your method, just use:
TextView versionName = findViewById(R.id.textName);
versionName.setText("version : " + BuildConfig.VERSION_NAME);
【讨论】:
智能便捷。【参考方案3】:这是给 Kotlin 的-
来自活动:
var versionName = applicationContext.packageManager.getPackageInfo(applicationContext.packageName, 0).versionName
yourTextView.text = "Version $versionName"
来自片段:
var versionName = requireActivity().applicationContext.packageManager.getPackageInfo(requireActivity().applicationContext.packageName, 0).versionName
yourTextView.text = "Version $versionName"
【讨论】:
【参考方案4】:试试:
textView.setText("App version: " + BuildConfig.VERSION_NAME + "("+BuildConfig.VERSION_CODE+")");
【讨论】:
以上是关于在 TextView 中显示 App 版本的主要内容,如果未能解决你的问题,请参考以下文章
使用 Objective C iOS App 在 textview 中显示 JSON 值