有没有办法让C更多地了解类型并确保类型安全?
考虑一下:
typedef unsigned cent_t;
typedef unsigned dollar_t;
#define DOLLAR_2_CENT(dollar) ((cent_t)(100*(dollar)))
void calc(cent_t amount) {
// expecting 'amount' to semantically represents cents...
}
int main(int argc, char* argv[]) {
dollar_t amount = 50;
calc(DOLLAR_2_CENT(amount)); // ok
calc(amount); // raise warning
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让上面的代码至少提高gcc的警告?
我知道我可以使用C-structs来包装unsigned
s并获得理想的结果,我只是想知道是否有更优雅的方法来实现它.
可以多一点吗?
如果我使用ActionBarSherlock 3.5.1一切都很好并且工作正常.但如果我想使用4.0 RC1,我就会遇到错误.
[2012-02-25 10:08:19 - SherlockApp] /home/leandros/workspace/ActionBarSherlock40/res/values-v14/abs__styles.xml:6: error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Holo.ActionBar.Solid'.
[2012-02-25 10:08:19 - SherlockApp] /home/leandros/workspace/ActionBarSherlock40/res/values-v14/abs__styles.xml:10: error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Holo.Light.ActionBar.Solid'.
[2012-02-25 10:08:19 - SherlockApp] /home/leandros/workspace/ActionBarSherlock40/res/values-v14/abs__styles.xml:12: error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Holo.Light.ActionBar.Solid.Inverse'.
[2012-02-25 10:08:19 - SherlockApp] /home/leandros/workspace/ActionBarSherlock40/res/values-v14/abs__styles.xml:19: error: Error retrieving parent for item: No resource found that matches the …
Run Code Online (Sandbox Code Playgroud) 我用它NotificationCompat.Builder
来通过Android版本显示我的通知,并使用自定义布局进行通知.
自定义布局在Android 3及更高版本(API级别11)上运行良好,但不会出现在API级别10或更低级别.我在模拟器中测试了2.3和2.2.
继承我的代码:
Builder builder = new NotificationCompat.Builder(getApplicationContext());
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);
contentView.setImageViewResource(R.id.notImage, R.drawable.stat_icon);
contentView.setTextViewText(R.id.notTitle, getResources().getString(R.string.streamPlaying));
contentView.setTextViewText(R.id.notText, StartActivity.streamName + " " + getResources().getString(R.string.playing));
builder
.setContentTitle(getResources().getString(R.string.streamPlaying))
.setContentText(StartActivity.streamName + " " + getResources().getString(R.string.playing))
.setSmallIcon(R.drawable.stat_icon)
.setContentIntent(pendingIntent)
.setOngoing(true)
.setWhen(0)
.setTicker(StartActivity.streamName + " " + getResources().getString(R.string.playing))
.setContent(contentView);
not = builder.build();
Run Code Online (Sandbox Code Playgroud)
真的很基本.布局文件是正确的,它与android.com上的通知教程相同,以确保我没有在那里犯错.;)
记住:3.0及以上工作正常,但不是2.3及更低.
是否在C99中引入或删除了特征/语义,这些特性/语义也可以用C89编写一个定义良好的程序
到目前为止我的调查结果,涉及明显无效的程序:
restrict
,inline
等)//
,现在被视为评论.但是,在生产代码中几乎从未遇到过.微妙的变化,使相同的代码具有不同的语义:
strtod
通过解析0x
或获得了在C99中解析十六进制数的能力0X
我错过了什么?
如果值为true,我想阻止拖动搜索条.这甚至可能吗?我使用搜索栏作为开关.如果我的搜索栏值为0,则按下按钮操作A开始.如果我的搜索栏值为1并且按下按钮,则启动操作B.
我想防止如果操作A或B正在运行,则搜索栏被拖动到另一个位置.
我怎样才能做到这一点?
我知道如何使用不推荐使用的属性来弃用这样的函数:
int old_fn () __attribute__ ((deprecated));
Run Code Online (Sandbox Code Playgroud)
但是如何弃用这样的宏:
#define OLD_MACRO 1
Run Code Online (Sandbox Code Playgroud)
先感谢您.
埃里克
我尝试在手机上将文件下载到SD卡.在Android 2.1,2.2和2.3上,一切都像预期的那样,但在Android 4.0(在模拟器和我的Galaxy Nexus上测试)它会抛出一个FileNotFoundException
.
堆栈跟踪:
02-27 21:49:06.733: W/System.err(27648): java.io.FileNotFoundException: http://www.wdr.de/wdrlive/media/wdr5.m3u
02-27 21:49:06.733: W/System.err(27648): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
02-27 21:49:06.733: W/System.err(27648): at de.arvidg.test.StartActivity$9.run(StartActivity.java:833)
02-27 21:49:06.733: W/System.err(27648): at java.lang.Thread.run(Thread.java:856)
Run Code Online (Sandbox Code Playgroud)
我下载文件的方法:
downloadFile("http://www.wdr.de/wdrlive/media/wdr5.m3u");
public void downloadFile(final String fileUrl) {
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(fileUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
// File cacheDir = appContext.getExternalCacheDir();
// File file = new File("/mnt/sdcard", "/cacheFile.ext");
File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot,"/cacheFile.ext"); …
Run Code Online (Sandbox Code Playgroud) 我应用自定义View
的ActionBar
,像这样的
// Inflate the "Done/Discard" custom ActionBar view.
LayoutInflater inflater = (LayoutInflater) DetailsHost.mActionBar
.getThemedContext().getSystemService(DetailsHost.LAYOUT_INFLATER_SERVICE);
final View customActionBarView = inflater.inflate(
R.layout.actionbar_custom_view_done_discard, null);
// Show the custom ActionBar view and hide the normal Home icon and title.
DetailsHost.mActionBar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_TITLE);
DetailsHost.mActionBar.setCustomView(customActionBarView,
new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
Run Code Online (Sandbox Code Playgroud)
(基于Roman Nuriks代码).
如何恢复初始布局?注意:我使用ActionBarSherlock
我在两个完全不同的情况下遇到了这个问题,这让人很奇怪.
在这两种情况下,按钮仅在稍微延迟后突出显示,约为0.5-1秒.绝对要在正常点击时突出显示视图.它们都可以在模拟器上完美运行,但不能在真实设备上运行(我有一个带有iOS 7.0.4的iPhone 5s).
我尝试了
将zPosition设置为这些视图的最高值(MAXFLOAT),以确保没有任何东西阻止了点击.
明确启用突出显示.
两者显然都没有用.
编辑:仍未解决.
我正在构建一个带有ActionBar
两个Tabs 的应用程序.如果设备/模拟器没有旋转,一切正常.如果旋转,选项卡状态会自动切换到tab1(正常,因为onCreate
被调用)但内容不会更改.如果我在新方向中选择一个选项卡,则会调用onCreateView()
所选方法,Fragment
但视图不会更新(保持不变).有小费吗?
代码.
主要活动:
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab ATab = actionbar.newTab().setText(R.string.player);
ActionBar.Tab BTab = actionbar.newTab().setText(R.string.stations);
Fragment AFragment = new AFragment();
Fragment BFragment = new BFragment();
PlayerTab.setTabListener(new MyTabsListener(AFragment));
StationsTab.setTabListener(new MyTabsListener(BFragment));
actionbar.addTab(ATab);
actionbar.addTab(BTab);
Run Code Online (Sandbox Code Playgroud)
使用相同的选项卡显示简单的文本视图.textview简单说明选择了哪个选项卡.
片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.a, container, false);
}
Run Code Online (Sandbox Code Playgroud)
上面提到的Fragment布局只包含带有硬编码Text的TextView.(仅用于测试目的)
Main布局如下所示.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" …
Run Code Online (Sandbox Code Playgroud)