MainActivity中的findViewById返回null

时间:2015-01-21 17:33:12

标签: android

我有一个问题,从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>

1 个答案:

答案 0 :(得分:0)

我已经测试了您的代码,结果发现更改视图的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);
    }
}