我想知道是否有在全球范围内声明活动的元素。我尝试使用视图假人,这样:
private final View dummy = (View) View.inflate(this,R.layout.dummy,null);
private final ImageButton imgButton = dummy.findViewById(R.id.imgButton);
但是,该应用程序每次都会使用以下消息崩溃:
Unable to instantiate activity ComponentInfo
由于我需要它们是最终的(我在听众上使用它们),因此我无法在全球声明它们,并在OnCreate方法中实例化。希望你们能提供帮助。
这是Android清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tcc.ecalc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme" >
<activity
android:theme="@style/AppTheme"
android:name=".Activity_PerfilTab">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:theme="@style/AppTheme"
android:name=".Activity_MaterialTab">
</activity>
<activity
android:name=".Activity_Main"
android:label="@string/app_name" >
</activity>
</application>
不确定你们是否需要什么,但是这里是。
e/androidruntime(1282):致命例外:主要 E/Androidruntime(1282):过程:com.tcc.ecalc,pid:1282 E/androidruntime(1282):java.lang.runtimeException:无法实例化活动ComponentInfo {com.tcc.ecalc/com.ecalc/com.ecalc.ecalc.ecalc.activitivitive_perfiltab}:java.lang.lang.illang.illang.illegalstateexception:系统服务不可用的活动:系统服务。
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:5035)
at android.view.LayoutInflater.from(LayoutInflater.java:219)
at android.view.View.inflate(View.java:18531)
at com.tcc.ecalc.Activity_PerfilTab.<init>(Activity_PerfilTab.java:17)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
活动代码尚未完成:
package com.tcc.ecalc;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
public class Activity_PerfilTab<PerfilCursorAdapter> extends Activity {
private View dummy = (View) View.inflate(this, R.layout.activity_perfiltab, null);
final Spinner spnPerfil = (Spinner) dummy.findViewById(R.id.per_spn_perfil);
final Spinner spnTipo = (Spinner) dummy.findViewById(R.id.per_spn_tipo);
final EditText nome = (EditText) dummy.findViewById(R.id.per_frm_nome);
final EditText d0_frm = (EditText) dummy.findViewById(R.id.per_frm_d0);
final EditText d1_frm = (EditText) dummy.findViewById(R.id.per_frm_d1);
final EditText d2_frm = (EditText) dummy.findViewById(R.id.per_frm_d2);
final EditText d3_frm = (EditText) dummy.findViewById(R.id.per_frm_d3);
final EditText momI = (EditText) dummy.findViewById(R.id.per_frm_inercia);
final EditText aSec = (EditText) dummy.findViewById(R.id.per_frm_area);
final TextView d0_lbl = (TextView) dummy.findViewById(R.id.per_lbl_d0);
final TextView d1_lbl = (TextView) dummy.findViewById(R.id.per_lbl_d1);
final TextView d2_lbl = (TextView) dummy.findViewById(R.id.per_lbl_d2);
final TextView d3_lbl = (TextView) dummy.findViewById(R.id.per_lbl_d3);
final ImageButton addMaterial = (ImageButton) dummy.findViewById(R.id.per_btn_add);
final ImageButton excMaterial = (ImageButton) dummy.findViewById(R.id.per_btn_exc);
final ImageButton atMaterial = (ImageButton) findViewById(R.id.per_btn_atualizar);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_perfiltab);
createSpnPerfil(0);
}
public void createSpnPerfil(int position) {
final SQLiteDatabase Estrutura = openOrCreateDatabase("Estrutura.db", getBaseContext().MODE_PRIVATE, null);
final Cursor linha = Estrutura.rawQuery("SELECT * FROM PERFIS", null);
spnPerfil.setAdapter(new ItemAdapter(getBaseContext(),R.layout.spn_img,linha, 1,2,0,"per_icn_"));
spnPerfil.setSelection(position);
spnPerfil.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
linha.moveToPosition(position);
int tipo = linha.getInt(2);
nome.setText(linha.getString(1));
//TODO Arrumar aqui
//spnTipo.setSelection(linha.getInt(2));
momI.setText(linha.getString(4));
aSec.setText(linha.getString(3));
d0_frm.setText(linha.getString(5));
d1_frm.setText(linha.getString(6));
d2_frm.setText(linha.getString(7));
d3_frm.setText(linha.getString(8));
//Atualiza o nome das labels e esconde aquelas que não são necessárias
switch(tipo) {
//Seção geral
case 1: {
d0_lbl.setText(R.string.area_label);
d1_lbl.setText(R.string.inercia_label);
d2_lbl.setVisibility(View.INVISIBLE);
d3_lbl.setVisibility(View.INVISIBLE);
} break;
//Seção circular
case 2: {
d0_lbl.setText(R.string.diametro_label);
} break;
//Seção retangular
case 3: {
d0_lbl.setText(R.string.base_label);
d1_lbl.setText(R.string.altura_label);
} break;
//Seção circular vazada
case 4: {
d0_lbl.setText(R.string.diametro_externo_label);
d1_lbl.setText(R.string.espessura_label);
} break;
//Seção retangular vazada
default : {
d0_lbl.setText(R.string.base_label);
d1_lbl.setText(R.string.altura_label);
d2_lbl.setText(R.string.espessura_alma_label);
d3_lbl.setText(R.string.espessura_mesa_label);
} break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
}
}
这是您的问题私有视图虚拟=(view)view.inflate(this,r.layout.activity_perfiltab,null);此代码需要在内部。
"在ongreate()之前无法使用的系统服务"