全局声明Android布局元素

时间:2015-05-15 00:30:04

标签: android layout global-variables elements

我想知道是否还要在全球范围内宣布活动的元素。我尝试使用视图虚拟,如下所示:

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 Manifest文件:

<?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.tcc.ecalc.Activity_PerfilTab}:java.lang.IllegalStateException:在onCreate()之前,活动不可用的系统服务

&#13;
&#13;
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)
&#13;
&#13;
&#13;

活动代码尚未完成:

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) {}
    });
}

}

1 个答案:

答案 0 :(得分:0)

这是你的问题 private view dummy =(View)View.inflate(this,R.layout.activity_perfiltab,null); 这段代码需要在onCreate里面。

&#34;在onCreate()&#34;

之前,活动无法使用系统服务