我想在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);
}
Run Code Online (Sandbox Code Playgroud)
Layout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.KnapperDev.knapper.jrw.informatie">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/imageView"
android:src="@drawable/overeenkomst"
android:layout_marginBottom="249dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/versieView"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="155dp" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
但是当我在手机上打开活动时,TextView中没有任何内容.我做了一个Toast来检查是否versieVak.setText(versionName);使用过.它只是没有显示任何东西.知道我做错了什么吗?
public void onClick(View v){
switch(v.getId()){
case R.id.bereken:
EditText basis = (EditText)findViewById(R.id.Basis);
String tussenBasis = basis.getText().toString();
Double.valueOf(tussenBasis);
//
EditText hoogte = (EditText)findViewById(R.id.Hoogte);
String tussenHoogte = hoogte.getText().toString();
Double.valueOf(tussenHoogte);
double half = 1 / 2;
//half = 0,5
double einde = half * basis * hoogte;
//eind antwoord formule
((TextView)findViewById(R.id.antwoord)).setText("Het antwoord is: " + einde);
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试为三角形区域制作一个简单的计算器,但我似乎被困在这里.