【问题标题】:findViewById in MainActivity returns nullMainActivity 中的 findViewById 返回 null
【发布时间】:2015-01-21 17:33:12
【问题描述】:

我有一个问题,从 MainActivity 调用 findViewById 返回 null。 我尝试从 MainActivity 中的 OnTouchEvent 调用 findViewById,以确保所有内容都已膨胀,但它也返回 null。

activity_main.xml:

<LinearLayout
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"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<com.blockslide.blockslide2.StartBlock
    android:id="@+id/mainstartblock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<com.blockslide.blockslide2.EndBlock
    android:id="@+id/mainendblock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</LinearLayout>

MainActivity.java:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    StartBlock startBlock = (StartBlock)findViewById(R.id.mainstartblock);
    if(startBlock != null) {
        ((ShapeDrawable) startBlock.getBackground()).getPaint().setColor(Color.parseColor("#0000ff"));
    }
    else{
        Toast.makeText(this, "NULL", Toast.LENGTH_SHORT).show();

    }

}
}

startblock.xml:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/start_end_program_bg"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="30dp"
android:paddingRight="30dp">

<TextView
    android:id="@+id/startblock_textview"
    android:textSize="@dimen/startblock_textview_textsize"
    android:textColor="@color/startblock_textview_textcolor"
    android:text="Start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

StartBlock.java:

public class StartBlock extends RelativeLayout implements Block {
private int blockId;
public StartBlock(Context context) {
    super(context);
    init(context);
}

public StartBlock(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public StartBlock(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
}

private void init(Context c){
    LayoutInflater layoutInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layoutInflater.inflate(R.layout.startblock, this);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        blockId = Utils.generateViewId();
    } else {
        blockId = View.generateViewId();
    }
    this.setId(blockId);
}
}

start_end_program_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ff7777"/>
<stroke android:width="3dp" android:color="#ff0000" />
<corners android:radius="100dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp"    android:bottom="0dp" />
</shape>

【问题讨论】:

  • 您确定在activity_main.xml:fragment_main.xml 中有StartBlock 吗???
  • 这里不使用fragment,只有一个activity,在activity_main.xml中
  • 检查R.javaR.java是否有mainstartblock的条目id???
  • 是的,来自 R.java:public static final int mainstartblock=0x7f08003f;
  • 确保你没有为不同的模式使用不同的布局,例如风景/肖像。

标签: android


【解决方案1】:

我测试了您的代码,结果发现更改视图的 id 会阻止 findViewById 找到它(现在很明显)。

改为使用android默认设置的id,如果id不存在则自己设置:

private void init(Context c) {
    LayoutInflater layoutInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layoutInflater.inflate(R.layout.startblock, this, true);

    blockId = getId();
    if (blockId == -1) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            blockId = Utils.generateViewId();
        } else {
            blockId = View.generateViewId();
        }
        setId(blockId);
    }
}

【讨论】:

  • 对!傻我。谢谢
猜你喜欢
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多