1

我设置了启动活动,几秒钟后开始另一个活动。无论如何,我想再添加一项功能:不仅在 5 秒后开始第二个活动,而且在单击我的启动视图后开始。一旦我设置了下一个:

View v = (View)findViewById(R.layout.splash); 
    v.setOnClickListener(this);
    setContentView(v);

代替

setContentView(R.layout.splash);

我的项目没有运行。哪里有问题?

这是我的代码:

public class SlashC extends Activity implements Runnable, OnClickListener {
Thread timer;
MediaPlayer hTree;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View v = (View)findViewById(R.layout.splash); // problems start here
    v.setOnClickListener(this);
    setContentView(v);

    hTree = MediaPlayer.create(this, R.raw.happy_tree);
    hTree.start();
    timer = new Thread(this);
    timer.start();

}
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent class2 = new Intent("com.roger.calc.MainActivity");
    startActivity(class2);
}
public void run() {
    // TODO Auto-generated method stub
    try {
        timer.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        Intent class2 = new Intent("com.roger.calc.MainActivity");
        startActivity(class2);
    }       
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    hTree.release();
    finish();
}}

和 XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/splash2"
android:clickable="true" >
</LinearLayout>
4

4 回答 4

7

android:clickable="true"通过设置布局属性,android:focusable="true"以及android:focusableInTouchMode="true"从 xml 或setClickable(true)代码中使 LinearLayout 可点击。将 onClickListener 设置为

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/splash"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true"
    android:clickable="true"
    android:focusable="true" 
    android:orientation="vertical"
    android:background="@drawable/splash2"/>

在代码部分:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    ((LinearLayout)findViewById(R.id.splash)).setOnClickListener(this);

    /// YOUR CODE HERE
于 2012-07-28T12:32:30.993 回答
1

可能是你在你的 xml 布局中遗漏了这个

android:id="@+id/splash"
于 2012-07-28T12:25:27.290 回答
0

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/splash2"
android:id="@+id/splash"
android:clickable="true" >
</LinearLayout>

setContentView(R.layout.name);

线性布局 v = (LinearLayout)findViewById(R.layout.splash);

v.setOnClickListener(this);

于 2012-07-28T12:28:16.900 回答
0

您不能在布局充气器之前findViewById(int id)使用set和布局。setContentView()android:clickable="true"android:focusable="true"android:focusableInTouchMode="true"

于 2015-09-11T23:46:57.643 回答