简体   繁体   English

ActionBar问题

[英]ActionBar problems

I used the code below to create ActionBar under Api-11 and it works fine but when I want to use it in another Api-11 project, force close happens when I run the app on my device. 我使用下面的代码在Api-11下创建ActionBar,它可以正常工作,但是当我想在另一个Api-11项目中使用它时,在设备上运行应用程序时会强制关闭。 Where is the problem? 问题出在哪儿?

When I make a new project and compile it and run the app on an android device it works fine! 当我制作一个新项目并将其编译并在android设备上运行该应用程序时,它工作正常!

package a.pkg;
import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class ActionBarActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ActionBar actionBar=getActionBar();

actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#fc0606")));

LayoutInflater mInflater = LayoutInflater.from(this);

View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
//TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
//mTitleTextView.setText("My Own Title");

final ImageButton imageButton1 = (ImageButton) mCustomView
        .findViewById(R.id.ImageButton1);
imageButton1.setOnClickListener(new View.OnClickListener() {


    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        Toast.makeText(getApplicationContext(), "1",
                Toast.LENGTH_LONG).show();
    }
});

final ImageButton imageButton2 = (ImageButton) mCustomView
.findViewById(R.id.ImageButton2);
imageButton2.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub



Toast.makeText(getApplicationContext(), "2",
        Toast.LENGTH_LONG).show();
}
});

final ImageButton imageButton3 = (ImageButton) mCustomView
.findViewById(R.id.ImageButton3);
imageButton3.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub



Toast.makeText(getApplicationContext(), "3",
        Toast.LENGTH_LONG).show();

}
});


actionBar.setCustomView(mCustomView);
actionBar.setDisplayShowCustomEnabled(true);

    }
}

我发现了这个问题,我不得不在清单中手动更改该代码至此,Eclipse无法将最小Api从8自动更改为11。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM